[Scummvm-git-logs] scummvm master -> dca72722f39185b316447317701cea8d2a10bf22
aquadran
noreply at scummvm.org
Sun Nov 1 21:18:21 UTC 2026
This automated email contains information about 18 new commits which have been
pushed to the 'scummvm' repo located at https://api.github.com/repos/scummvm/scummvm .
Summary:
9f6a02b6e2 GRIM: SMUSH: Rewrite the majority of the subsystem
5a746dccd1 GRIM: SMUSH: Match the disasm's Blocky16 buffers and frame skipping
733532ae3b GRIM: SMUSH: Match the disasm's loop restart frame pacing
e7826e1d12 GRIM: SMUSH: Remove temp workaround during rewrite
0a15cfbfe2 GRIM: SMUSH: Implement original demo codepath
ab8a5921e3 GRIM: SMUSH: Clean-up unused code and re-wire up Codec48
50b0d7e66e GRIM: SMUSH: Fix video speed for remastered
821d07da11 GRIM: SMUSH: Fix out of bounds read of the FLHD audio header
7d3474257a GRIM: SMUSH: Remove unused timer and Wave module code
1f04abaee6 GRIM: SMUSH: Remove unused animator code
a11b8d3050 GRIM: SMUSH: Remove unused SOU code
9146a2248f GRIM: SMUSH: Remove unused mixer code
a3300abb9f GRIM: SMUSH: Remove unused demo animator code
6f273f11c0 GRIM: SMUSH: Remove unused demo player code
f3bc5a22c4 GRIM: SMUSH: Remove unused codec 48 code
834f76ff35 GRIM: SMUSH: Address PR comments and remove most magic numbers
440d459872 GRIM: SMUSH: Improve some magic numbers labeling
dca72722f3 GRIM: SMUSH: Remove unused animator flags
Commit: 9f6a02b6e2164b70c5389d659736b162c79bb9b1
https://github.com/scummvm/scummvm/commit/9f6a02b6e2164b70c5389d659736b162c79bb9b1
Author: AndywinXp (andywinxp at gmail.com)
Date: 2026-11-01T22:18:07+01:00
Commit Message:
GRIM: SMUSH: Rewrite the majority of the subsystem
This brings several features from the disasm, including:
* Proper audio enqueueing and ring out
* Adaptive frame pacing
* Proper handling of previously unhandled tags
Changed paths:
A engines/grim/movie/smush_anim.cpp
A engines/grim/movie/smush_anim.h
A engines/grim/movie/smush_animator.cpp
A engines/grim/movie/smush_animator.h
A engines/grim/movie/smush_mixer.cpp
A engines/grim/movie/smush_mixer.h
A engines/grim/movie/smush_sound.cpp
A engines/grim/movie/smush_sound.h
A engines/grim/movie/smush_timer.cpp
A engines/grim/movie/smush_timer.h
A engines/grim/movie/smush_wave.cpp
A engines/grim/movie/smush_wave.h
engines/grim/module.mk
engines/grim/movie/codecs/smush_decoder.cpp
engines/grim/movie/codecs/smush_decoder.h
engines/grim/movie/movie.cpp
engines/grim/movie/movie.h
engines/grim/movie/smush.cpp
engines/grim/movie/smush.h
diff --git a/engines/grim/module.mk b/engines/grim/module.mk
index 39e75c6eeaa..290824df7ca 100644
--- a/engines/grim/module.mk
+++ b/engines/grim/module.mk
@@ -92,6 +92,12 @@ MODULE_OBJS := \
movie/movie.o \
movie/quicktime.o \
movie/smush.o \
+ movie/smush_anim.o \
+ movie/smush_animator.o \
+ movie/smush_mixer.o \
+ movie/smush_sound.o \
+ movie/smush_timer.o \
+ movie/smush_wave.o \
remastered/commentary.o \
remastered/hotspot.o \
remastered/lua_remastered.o \
diff --git a/engines/grim/movie/codecs/smush_decoder.cpp b/engines/grim/movie/codecs/smush_decoder.cpp
index 0864e950d46..d9090343e6c 100644
--- a/engines/grim/movie/codecs/smush_decoder.cpp
+++ b/engines/grim/movie/codecs/smush_decoder.cpp
@@ -37,6 +37,7 @@
#include "engines/grim/movie/codecs/blocky16.h"
#include "engines/grim/movie/codecs/smush_decoder.h"
#include "engines/grim/movie/codecs/vima.h"
+#include "engines/grim/movie/smush_wave.h"
namespace Grim {
@@ -57,6 +58,9 @@ SmushDecoder::SmushDecoder() {
_videoTrack = nullptr;
_audioTrack = nullptr;
+ _wave = nullptr;
+ _ownsAudioTrack = false;
+ _blocky16Frames = 0;
_videoPause = false;
}
@@ -118,6 +122,10 @@ void SmushDecoder::initFrames() {
void SmushDecoder::close() {
VideoDecoder::close();
+ if (_ownsAudioTrack)
+ delete _audioTrack;
+
+ _ownsAudioTrack = false;
_audioTrack = nullptr;
_videoTrack = nullptr;
_videoLooping = false;
@@ -237,6 +245,9 @@ bool SmushDecoder::handleFramesHeader() {
} else if (READ_BE_UINT32(f_header + pos) == MKTAG('W', 'a', 'v', 'e')) {
freq = READ_LE_UINT32(f_header + pos + 8);
channels = READ_LE_UINT32(f_header + pos + 12);
+ if (_wave)
+ _wave->flobdecDecodeHeader(f_header + pos + 8);
+
pos += 20;
} else {
error("SmushDecoder::handleFramesHeader() unknown tag");
@@ -245,7 +256,11 @@ bool SmushDecoder::handleFramesHeader() {
delete[] f_header;
_audioTrack = new SmushAudioTrack(getSoundType(), true, freq, channels);
- addTrack(_audioTrack);
+ if (_wave)
+ _ownsAudioTrack = true;
+ else
+ addTrack(_audioTrack);
+
return true;
}
@@ -353,9 +368,13 @@ void SmushDecoder::handleFRME(Common::SeekableReadStream *stream, uint32 size) {
// Retail only:
case MKTAG('B', 'l', '1', '6'):
_videoTrack->handleBlocky16(memStream, subSize);
+ _blocky16Frames++;
break;
case MKTAG('W', 'a', 'v', 'e'):
- _audioTrack->handleVIMA(memStream, blockSize);
+ if (_wave)
+ _wave->flobdecDecodeBody(block + subPos);
+ else
+ _audioTrack->handleVIMA(memStream, blockSize);
break;
// Demo only:
case MKTAG('F', 'O', 'B', 'J'):
@@ -377,7 +396,43 @@ void SmushDecoder::handleFRME(Common::SeekableReadStream *stream, uint32 size) {
delete[] block;
}
+void SmushDecoder::decodeFrameBlock(const byte *block, uint32 size) {
+ Common::MemoryReadStream *memStream = new Common::MemoryReadStream(block, size, DisposeAfterUse::NO);
+
+ int remaining = size;
+ while (remaining > 4) {
+ uint32 subType = memStream->readUint32BE();
+ uint32 subSize = memStream->readUint32BE();
+ uint32 subPos = memStream->pos();
+
+ switch (subType) {
+ case MKTAG('B', 'l', '1', '6'):
+ _videoTrack->handleBlocky16(memStream, subSize);
+ _blocky16Frames++;
+ break;
+ case MKTAG('W', 'a', 'v', 'e'):
+ _wave->flobdecDecodeBody(block + subPos);
+ break;
+ default:
+ break;
+ }
+
+ remaining -= subSize + 8 + (subSize & 1);
+ memStream->seek(subPos + subSize + (subSize & 1), SEEK_SET);
+ }
+
+ delete memStream;
+ _videoTrack->finishFrame();
+}
+
+const Graphics::Surface *SmushDecoder::getFrameSurface() {
+ return _videoTrack->decodeNextFrame();
+}
+
bool SmushDecoder::rewind() {
+ if (_wave)
+ _wave->flobdecReset();
+
return seekToFrame(0);
}
diff --git a/engines/grim/movie/codecs/smush_decoder.h b/engines/grim/movie/codecs/smush_decoder.h
index 74739d88ca8..49586fa6af2 100644
--- a/engines/grim/movie/codecs/smush_decoder.h
+++ b/engines/grim/movie/codecs/smush_decoder.h
@@ -37,6 +37,7 @@ namespace Grim {
class Codec48Decoder;
class Blocky8;
class Blocky16;
+class SmushWave;
class SmushDecoder : public Video::VideoDecoder {
public:
@@ -46,6 +47,12 @@ public:
int getX() const { return _videoTrack->_x; }
int getY() const { return _videoTrack->_y; }
void setLooping(bool l);
+ void setWave(SmushWave *wave) { _wave = wave; }
+ Common::SeekableReadStream *getFile() { return _file; }
+ void decodeFrameBlock(const byte *block, uint32 size);
+ const Graphics::Surface *getFrameSurface();
+ void resetBlocky16Frames() { _blocky16Frames = 0; }
+ int getBlocky16Frames() const { return _blocky16Frames; }
bool isRewindable() const override { return true; }
bool isSeekable() const override { return true; }
bool rewind() override;
@@ -135,6 +142,9 @@ private:
SmushAudioTrack *_audioTrack;
SmushVideoTrack *_videoTrack;
+ SmushWave *_wave;
+ bool _ownsAudioTrack;
+ int _blocky16Frames;
Common::SeekableReadStream *_file;
diff --git a/engines/grim/movie/movie.cpp b/engines/grim/movie/movie.cpp
index cce21ff2bce..dfe6add4ee8 100644
--- a/engines/grim/movie/movie.cpp
+++ b/engines/grim/movie/movie.cpp
@@ -81,6 +81,7 @@ void MoviePlayer::stop() {
void MoviePlayer::timerCallback(void *instance) {
MoviePlayer *movie = static_cast<MoviePlayer *>(instance);
Common::StackLock lock(movie->_frameMutex);
+ movie->handleTimer();
if (movie->prepareFrame())
movie->postHandleFrame();
}
diff --git a/engines/grim/movie/movie.h b/engines/grim/movie/movie.h
index 940a3fd9f27..e7008516f58 100644
--- a/engines/grim/movie/movie.h
+++ b/engines/grim/movie/movie.h
@@ -114,6 +114,12 @@ protected:
*/
virtual void handleFrame() {};
+ /**
+ * Called by the timer thread on every tick, with _frameMutex locked,
+ * before the frame is prepared.
+ */
+ virtual void handleTimer() {}
+
/**
* Frame-handling function.
*
diff --git a/engines/grim/movie/smush.cpp b/engines/grim/movie/smush.cpp
index 41b269345d4..97e76c8ccbf 100644
--- a/engines/grim/movie/smush.cpp
+++ b/engines/grim/movie/smush.cpp
@@ -21,6 +21,11 @@
#include "engines/grim/movie/codecs/smush_decoder.h"
#include "engines/grim/movie/smush.h"
+#include "engines/grim/movie/smush_animator.h"
+#include "engines/grim/movie/smush_mixer.h"
+#include "engines/grim/movie/smush_sound.h"
+#include "engines/grim/movie/smush_timer.h"
+#include "engines/grim/movie/smush_wave.h"
#include "engines/grim/resource.h"
#include "engines/grim/grim.h"
@@ -34,12 +39,64 @@ MoviePlayer *CreateSmushPlayer(bool demo) {
}
SmushPlayer::~SmushPlayer() {
+ {
+ Common::StackLock lock(_frameMutex);
+ SmushAnimPlay *anim = _smushAnim;
+ SmushAnimator *animator = _smushAnimator;
+ SmushWave *wave = _smushWave;
+ SmushSound *sound = _smushSound;
+ SmushDriver *driver = _smushDriver;
+ SmushTimer *timer = _smushTimer;
+ _smushAnim = nullptr;
+ _smushAnimator = nullptr;
+ _smushWave = nullptr;
+ _smushSound = nullptr;
+ _smushDriver = nullptr;
+ _smushTimer = nullptr;
+
+ delete anim;
+ delete animator;
+ if (wave)
+ wave->moduleShutdown();
+ if (sound)
+ sound->moduleShutdown();
+ if (timer)
+ timer->moduleShutdown();
+
+ delete wave;
+ delete sound;
+ delete driver;
+ delete timer;
+ }
+
delete _theoraDecoder;
}
SmushPlayer::SmushPlayer(bool demo) : MoviePlayer(), _demo(demo) {
_smushDecoder = new SmushDecoder();
_videoDecoder = _smushDecoder;
+
+ _smushTimer = nullptr;
+ _smushDriver = nullptr;
+ _smushSound = nullptr;
+ _smushWave = nullptr;
+ _smushAnim = nullptr;
+ _smushAnimator = nullptr;
+
+ if (!_demo) {
+ _smushTimer = new SmushTimer();
+ _smushDriver = new SmushDriver(_smushTimer);
+ _smushSound = new SmushSound(_smushTimer, _smushDriver);
+ _smushWave = new SmushWave(_smushSound);
+ _smushAnim = new SmushAnimPlay(_smushTimer, _smushSound);
+ _smushAnimator = new SmushAnimator(_smushDecoder);
+
+ _smushTimer->moduleStartup();
+ _smushSound->moduleStartup();
+ _smushWave->moduleStartup();
+ _smushDecoder->setWave(_smushWave);
+ }
+
#if defined (USE_THEORADEC)
_theoraDecoder = new Video::TheoraDecoder();
#else
@@ -90,6 +147,76 @@ void SmushPlayer::init() {
}
}
MoviePlayer::init();
+
+ if (_smushAnim && !_currentVideoIsTheora) {
+ _smushAnim->anmStop();
+ _smushAnim->anmStart(15, 24589);
+ _smushAnimator->animatorNewAnim(24589, -1, -1);
+ }
+}
+
+bool SmushPlayer::prepareFrame() {
+ if (_demo || _currentVideoIsTheora || !_smushAnim)
+ return MoviePlayer::prepareFrame();
+
+ if (_videoPause)
+ return false;
+
+ if (_videoFinished) {
+ if (g_grim->getMode() == GrimEngine::SmushMode)
+ g_grim->setMode(GrimEngine::NormalMode);
+
+ _videoPause = true;
+ return false;
+ }
+
+ int state = _smushAnim->anmRender(this);
+ if (state == 6)
+ return false;
+
+ if (state < 0 || state == 4) {
+ _smushAnim->anmStop();
+
+ if (_videoLooping) {
+ _smushDecoder->rewind();
+ _smushDecoder->start();
+ _smushAnim->anmStart(15, 24589);
+ _smushAnimator->animatorNewAnim(24589, -1, -1);
+ } else {
+ _videoFinished = true;
+ }
+ }
+
+ return true;
+}
+
+int SmushPlayer::animatorRender(int flags) {
+ int state = _smushAnimator->animatorRender(flags);
+ if (state < 0 || state == 5)
+ return state;
+
+ _internalSurface = _smushDecoder->getFrameSurface();
+ _internalPalette = _smushDecoder->getPalette();
+
+ int currentFrame = _smushDecoder->getCurFrame();
+ if (!(flags & 0x8000)) {
+ if (_frame != currentFrame)
+ _updateNeeded = true;
+
+ _frame = currentFrame;
+ }
+
+ _movieTime = _smushDecoder->getTime();
+
+ return state;
+}
+
+void SmushPlayer::resetFrameCounter() {
+ _smushDecoder->resetBlocky16Frames();
+}
+
+int SmushPlayer::getFrameCounter() {
+ return _smushDecoder->getBlocky16Frames();
}
void SmushPlayer::handleFrame() {
@@ -110,6 +237,24 @@ void SmushPlayer::handleFrame() {
}
}
+void SmushPlayer::handleTimer() {
+ if (_smushTimer)
+ _smushTimer->timerThread();
+}
+
+void SmushPlayer::deinit() {
+ // The disasm stops the sound only if the movie is cut, not when it ends by itself
+ bool wasPlaying = !_videoFinished;
+
+ if (_smushAnim)
+ _smushAnim->anmStop();
+
+ MoviePlayer::deinit();
+
+ if (wasPlaying && _smushSound)
+ _smushSound->souEngineStopAll();
+}
+
void SmushPlayer::postHandleFrame() {
if (_demo && !_currentVideoIsTheora) {
_x = _smushDecoder->getX();
@@ -119,7 +264,13 @@ void SmushPlayer::postHandleFrame() {
void SmushPlayer::restore(SaveGame *state) {
if (isPlaying() && !_currentVideoIsTheora) {
- _smushDecoder->seek((uint32)_movieTime);
+ if (_smushAnim) {
+ _frame = -1;
+ _movieTime = 0;
+ } else {
+ _smushDecoder->seek((uint32)_movieTime);
+ }
+
_smushDecoder->start();
timerCallback(this);
}
diff --git a/engines/grim/movie/smush.h b/engines/grim/movie/smush.h
index 604177f1726..e999316ff36 100644
--- a/engines/grim/movie/smush.h
+++ b/engines/grim/movie/smush.h
@@ -23,14 +23,20 @@
#define GRIM_SMUSH_PLAYER_H
#include "engines/grim/movie/movie.h"
+#include "engines/grim/movie/smush_anim.h"
#include "video/video_decoder.h"
namespace Grim {
class SmushDecoder;
+class SmushAnimator;
+class SmushTimer;
+class SmushDriver;
+class SmushSound;
+class SmushWave;
-class SmushPlayer : public MoviePlayer {
+class SmushPlayer : public MoviePlayer, public SmushAnimClient {
public:
SmushPlayer(bool demo);
virtual ~SmushPlayer();
@@ -38,12 +44,24 @@ public:
private:
bool loadFile(const Common::String &filename) override;
+ bool prepareFrame() override;
+ int animatorRender(int flags) override;
+ void resetFrameCounter() override;
+ int getFrameCounter() override;
void handleFrame() override;
+ void handleTimer() override;
void postHandleFrame() override;
void init() override;
+ void deinit() override;
bool _demo;
bool _currentVideoIsTheora = false;
SmushDecoder *_smushDecoder;
+ SmushTimer *_smushTimer;
+ SmushDriver *_smushDriver;
+ SmushSound *_smushSound;
+ SmushWave *_smushWave;
+ SmushAnimPlay *_smushAnim;
+ SmushAnimator *_smushAnimator;
Video::VideoDecoder *_theoraDecoder; // HACK for now, move to other class later?
};
diff --git a/engines/grim/movie/smush_anim.cpp b/engines/grim/movie/smush_anim.cpp
new file mode 100644
index 00000000000..6552e1429a4
--- /dev/null
+++ b/engines/grim/movie/smush_anim.cpp
@@ -0,0 +1,213 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include "common/scummsys.h"
+
+#include "engines/grim/movie/smush_anim.h"
+#include "engines/grim/movie/smush_sound.h"
+#include "engines/grim/movie/smush_timer.h"
+
+namespace Grim {
+
+SmushAnimPlay::SmushAnimPlay(SmushTimer *timer, SmushSound *sound) {
+ _timer = timer;
+ _sound = sound;
+ _frameTimer = 0;
+ _syncTimer = 0;
+ _flags = 0;
+ _finished = 0;
+ _framesDue = 0;
+ _lateTicks = 0;
+ _nominalDelay = 0;
+ _currentDelay = 0;
+ _skipFlag = 0;
+ _state = 0;
+ _started = 0;
+ _syncPaused = 0;
+}
+
+SmushAnimPlay::~SmushAnimPlay() {
+ anmStop();
+}
+
+int SmushAnimPlay::anmStart(int fps, int flags) {
+ _flags = flags;
+ _lateTicks = 0;
+ _framesDue = 0;
+ _skipFlag = 0;
+ _started = 0;
+ _state = 0;
+ _nominalDelay = 1193180 / fps;
+ _currentDelay = 1193180 / fps;
+
+ _frameTimer = _timer->sysAlloc(frameTimer, 1, SYS_TIMER_ONE_SHOT, this);
+ if (!_frameTimer)
+ return -1;
+
+ _syncTimer = _timer->sysAlloc(syncTimer, _nominalDelay, SYS_TIMER_EXT_SYNC, this);
+ if (!_syncTimer) {
+ _timer->sysFree(_frameTimer);
+ _frameTimer = 0;
+ return -1;
+ }
+
+ _finished = 0;
+ return 0;
+}
+
+void SmushAnimPlay::anmStop() {
+ if (!_frameTimer && !_syncTimer)
+ return;
+
+ _timer->sysFree(_frameTimer);
+ _timer->sysFree(_syncTimer);
+ _frameTimer = 0;
+ _syncTimer = 0;
+
+ if (_syncPaused)
+ _sound->souEngineResume();
+}
+
+int SmushAnimPlay::anmRender(SmushAnimClient *client) {
+ if (_finished)
+ return -1;
+
+ return anmRenderBody(client);
+}
+
+int SmushAnimPlay::anmRenderBody(SmushAnimClient *client) {
+ if (_state == 5) {
+ int result = client->animatorRender(_flags);
+ _state = result;
+ if (result < 0)
+ _finished = 1;
+
+ return result;
+ }
+
+ if (!_framesDue)
+ return 6;
+
+ if (!_started)
+ client->resetFrameCounter();
+
+ int drift = (int)(int64)(((double)_sound->souEngineQueryTime(0) - (double)client->getFrameCounter() * 66.66666666667) * 0.0149999999999925);
+ _lateTicks--;
+ _framesDue = 0;
+
+ if (drift > 0 || _state == 2 || _state == 3 || _state == 4) {
+ if (_currentDelay > _nominalDelay)
+ _currentDelay = _nominalDelay;
+
+ int step = (drift == 1) ? _nominalDelay / 16 : _nominalDelay / 8;
+ _currentDelay -= step;
+ if (_currentDelay < 1)
+ _currentDelay = 1;
+
+ int minimum = 3 * _nominalDelay / 4;
+ if (_currentDelay < minimum)
+ _currentDelay = minimum;
+ } else if (drift < 0) {
+ if (_currentDelay < _nominalDelay)
+ _currentDelay = _nominalDelay;
+
+ int step = (drift == -1) ? _nominalDelay / 16 : _nominalDelay / 8;
+ _currentDelay += step;
+
+ int maximum = 4 * _nominalDelay / 3;
+ if (_currentDelay > maximum)
+ _currentDelay = maximum;
+ }
+
+ _timer->sysReset(_frameTimer, _currentDelay);
+
+ if (drift <= 2) {
+ if (drift <= 0)
+ _skipFlag = 0;
+ } else {
+ _skipFlag = 1;
+ }
+
+ int flags = _flags;
+ if (_skipFlag && !(flags & 2))
+ flags |= 0x8000;
+
+ _state = client->animatorRender(flags);
+ if (_state < 0)
+ _finished = 1;
+ if (_state == 1)
+ _started = 1;
+
+ return _state;
+}
+
+void SmushAnimPlay::anmSetRate(int fps) {
+ _nominalDelay = 1193180 / fps;
+ _currentDelay = 1193180 / fps;
+ _timer->sysReset(_frameTimer, 1193180 / fps);
+ _timer->sysReset(_syncTimer, _nominalDelay);
+}
+
+void SmushAnimPlay::anmSyncUp() {
+ _lateTicks = 0;
+ _timer->sysReset(_frameTimer, _currentDelay);
+}
+
+bool SmushAnimPlay::anmRenderQuery() {
+ return _state == 5 || _framesDue;
+}
+
+int SmushAnimPlay::anmGetTime() {
+ return 1000 * _currentDelay / 1193180;
+}
+
+void SmushAnimPlay::frameTimer(int timer, void *context) {
+ ((SmushAnimPlay *)context)->_framesDue++;
+}
+
+void SmushAnimPlay::syncTimer(int timer, void *context) {
+ SmushAnimPlay *anim = (SmushAnimPlay *)context;
+
+ if (!anim->_started)
+ return;
+
+ if (anim->_flags & 1) {
+ if (!anim->_syncPaused && anim->_lateTicks >= 7) {
+ anim->_syncPaused = 1;
+ anim->_sound->souEnginePause();
+ } else if (anim->_syncPaused) {
+ if (anim->_lateTicks < 0) {
+ anim->_syncPaused = 0;
+ anim->_sound->souEngineResume();
+ }
+ }
+ } else if (anim->_syncPaused) {
+ if (anim->_lateTicks < 0) {
+ anim->_syncPaused = 0;
+ anim->_sound->souEngineResume();
+ }
+ }
+
+ if (anim->_lateTicks < 20 && !anim->_syncPaused)
+ anim->_lateTicks++;
+}
+
+} // End of namespace Grim
diff --git a/engines/grim/movie/smush_anim.h b/engines/grim/movie/smush_anim.h
new file mode 100644
index 00000000000..a50ab11e0a5
--- /dev/null
+++ b/engines/grim/movie/smush_anim.h
@@ -0,0 +1,79 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifndef GRIM_MOVIE_SMUSH_ANIM_H
+#define GRIM_MOVIE_SMUSH_ANIM_H
+
+#include "common/scummsys.h"
+
+namespace Grim {
+
+class SmushTimer;
+class SmushSound;
+
+class SmushAnimClient {
+public:
+ virtual ~SmushAnimClient() {}
+
+ virtual int animatorRender(int flags) = 0;
+ virtual void resetFrameCounter() = 0;
+ virtual int getFrameCounter() = 0;
+};
+
+class SmushAnimPlay {
+
+private:
+ SmushTimer *_timer;
+ SmushSound *_sound;
+ int _frameTimer;
+ int _syncTimer;
+ int _flags;
+ int _finished;
+ int _framesDue;
+ int _lateTicks;
+ int _nominalDelay;
+ int _currentDelay;
+ int _skipFlag;
+ int _state;
+ int _started;
+ int _syncPaused;
+
+ static void frameTimer(int timer, void *context);
+ static void syncTimer(int timer, void *context);
+
+ int anmRenderBody(SmushAnimClient *client);
+
+public:
+ SmushAnimPlay(SmushTimer *timer, SmushSound *sound);
+ ~SmushAnimPlay();
+
+ int anmStart(int fps, int flags);
+ void anmStop();
+ int anmRender(SmushAnimClient *client);
+ void anmSetRate(int fps);
+ void anmSyncUp();
+ bool anmRenderQuery();
+ int anmGetTime();
+
+};
+
+} // End of namespace Grim
+#endif
diff --git a/engines/grim/movie/smush_animator.cpp b/engines/grim/movie/smush_animator.cpp
new file mode 100644
index 00000000000..c796bcba59b
--- /dev/null
+++ b/engines/grim/movie/smush_animator.cpp
@@ -0,0 +1,258 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include "common/endian.h"
+#include "common/stream.h"
+
+#include "engines/grim/movie/codecs/smush_decoder.h"
+#include "engines/grim/movie/smush_animator.h"
+
+namespace Grim {
+
+#define SMUSH_TAG_NULL MKTAG('n', 'u', 'l', 'l')
+#define SMUSH_TAG_END MKTAG('e', 'n', 'd', ' ')
+#define SMUSH_TAG_PAUSE MKTAG('p', 'a', 'u', 's')
+#define SMUSH_TAG_WAIT MKTAG('w', 'a', 'i', 't')
+
+SmushAnimator::SmushAnimator(SmushDecoder *decoder) {
+ _decoder = decoder;
+ _buffer = nullptr;
+ _bufferSize = 0;
+ _block = nullptr;
+ _blockStatus = 0;
+
+ animatorInitialize();
+}
+
+SmushAnimator::~SmushAnimator() {
+ free(_buffer);
+}
+
+int SmushAnimator::animatorInitialize() {
+ _flags = 0;
+ _frame = 0;
+ _blockSize = 0;
+ _tag = SMUSH_TAG_NULL;
+ _frameCount = 1;
+ _fileMode = 0;
+ _quit = 0;
+ _pause = 0;
+ _seeking = 0;
+ _skipping = 0;
+ _skipStart = -1;
+ _skipBase = 0;
+ _skipEnd = -1;
+ return 0;
+}
+
+int SmushAnimator::animatorNewAnim(int flags, int skipLength, int skipUntil) {
+ animatorInitialize();
+
+ _frameCount = _decoder->getFrameCount();
+ _seeking = 0;
+ _flags = flags;
+ _fileMode = 1;
+
+ _skipStart = skipUntil;
+ _skipBase = _frame;
+ _skipEnd = skipLength;
+ return 0;
+}
+
+int SmushAnimator::animatorQuery() {
+ return _fileMode;
+}
+
+void SmushAnimator::animatorQuit() {
+ _quit = 1;
+}
+
+int SmushAnimator::animatorPause(int mode) {
+ if (mode == 2) {
+ _pause = (_pause == 0);
+ return _pause;
+ }
+
+ if (mode == 4) {
+ if (_pause) {
+ _pause = 3;
+ return _pause;
+ }
+ } else if (mode != 5) {
+ _pause = mode;
+ }
+
+ return _pause;
+}
+
+void SmushAnimator::readBlock(int size) {
+ Common::SeekableReadStream *file = _decoder->getFile();
+
+ if (!file || file->pos() >= file->size()) {
+ _block = nullptr;
+ _blockStatus = 0;
+ _frame = -1;
+ _fileMode = 0;
+ return;
+ }
+
+ if (file->size() - file->pos() < size) {
+ _block = nullptr;
+ _blockStatus = -2;
+ return;
+ }
+
+ if (_bufferSize < (uint32)size) {
+ _buffer = (byte *)realloc(_buffer, size);
+ _bufferSize = size;
+ }
+
+ file->read(_buffer, size);
+ _block = _buffer;
+ _blockStatus = 1;
+}
+
+int SmushAnimator::checkSkip() {
+ if (!_skipping) {
+ if (_fileMode) {
+ if (_skipEnd >= 0 && _frame > _skipEnd + _skipBase)
+ return 1;
+ } else if (_skipStart >= 0) {
+ if (_frame < _skipStart)
+ return 1;
+
+ _skipStart = -1;
+ }
+ }
+
+ return 0;
+}
+
+void SmushAnimator::decodeBlocks() {
+ _decoder->decodeFrameBlock(_block, _blockSize);
+}
+
+bool SmushAnimator::moreData() {
+ Common::SeekableReadStream *file = _decoder->getFile();
+ return file && file->pos() < file->size();
+}
+
+int SmushAnimator::animatorRender(int flags) {
+ if (_quit) {
+ _quit = 0;
+ return -1;
+ }
+
+ while (true) {
+ switch (_tag) {
+ case MKTAG('A', 'N', 'N', 'O'):
+ readBlock(_blockSize);
+ if (_blockStatus == -2)
+ return -2;
+ if (!_block)
+ return -2;
+
+ _tag = SMUSH_TAG_NULL;
+ break;
+ case MKTAG('F', 'R', 'M', 'E'):
+ readBlock(_blockSize);
+ if (_blockStatus == -2)
+ return -2;
+
+ _tag = SMUSH_TAG_NULL;
+ if (_block) {
+ _frame++;
+ if (_skipping || !checkSkip()) {
+ decodeBlocks();
+
+ if (_frame < _frameCount - 1 || _skipping) {
+ if (_pause == 1)
+ _tag = SMUSH_TAG_PAUSE;
+ else if (_pause == 3)
+ _pause = 1;
+ } else if (moreData() || _seeking) {
+ _tag = SMUSH_TAG_NULL;
+ } else {
+ _tag = SMUSH_TAG_END;
+ }
+
+ if (_skipping)
+ return 2;
+
+ // The disasm never renders the last frame
+ if (_frame >= 10)
+ return _frame < _frameCount - 1 ? 0 : 4;
+
+ return 1;
+ }
+
+ if (_frame >= _frameCount - 1) {
+ if (moreData() || _seeking)
+ _tag = SMUSH_TAG_NULL;
+ else
+ _tag = SMUSH_TAG_END;
+ }
+ } else {
+ return -2;
+ }
+ break;
+ case SMUSH_TAG_NULL:
+ if (_seeking) {
+ _seeking = 0;
+ _skipping = 1;
+ }
+
+ readBlock(8);
+ if (_blockStatus == -2 || !_block)
+ return -2;
+
+ _tag = READ_BE_UINT32(_block);
+ _blockSize = (READ_BE_UINT32(_block + 4) + 1) & ~1;
+ break;
+ case SMUSH_TAG_END:
+ _frame++;
+ checkSkip();
+
+ if (moreData() || _seeking)
+ _tag = SMUSH_TAG_NULL;
+
+ return 4;
+ case SMUSH_TAG_PAUSE:
+ if (_pause != 1) {
+ if (_pause == 3)
+ _pause = 1;
+
+ _tag = SMUSH_TAG_NULL;
+ }
+
+ return 3;
+ case SMUSH_TAG_WAIT:
+ if (moreData() || _seeking)
+ _tag = SMUSH_TAG_NULL;
+
+ return 5;
+ default:
+ return -2;
+ }
+ }
+}
+
+} // End of namespace Grim
diff --git a/engines/grim/movie/smush_animator.h b/engines/grim/movie/smush_animator.h
new file mode 100644
index 00000000000..87372f511ae
--- /dev/null
+++ b/engines/grim/movie/smush_animator.h
@@ -0,0 +1,72 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifndef GRIM_MOVIE_SMUSH_ANIMATOR_H
+#define GRIM_MOVIE_SMUSH_ANIMATOR_H
+
+#include "common/scummsys.h"
+
+namespace Grim {
+
+class SmushDecoder;
+
+class SmushAnimator {
+
+private:
+ SmushDecoder *_decoder;
+ byte *_buffer;
+ uint32 _bufferSize;
+ byte *_block;
+ int _blockStatus;
+ int _frame;
+ int _frameCount;
+ int _blockSize;
+ uint32 _tag;
+ int _fileMode;
+ int _flags;
+ int _seeking;
+ int _skipping;
+ int _skipStart;
+ int _skipBase;
+ int _skipEnd;
+ int _quit;
+ int _pause;
+
+ void readBlock(int size);
+ int checkSkip();
+ void decodeBlocks();
+ bool moreData();
+
+public:
+ SmushAnimator(SmushDecoder *decoder);
+ ~SmushAnimator();
+
+ int animatorInitialize();
+ int animatorNewAnim(int flags, int skipLength, int skipUntil);
+ int animatorRender(int flags);
+ int animatorQuery();
+ void animatorQuit();
+ int animatorPause(int mode);
+
+};
+
+} // End of namespace Grim
+#endif
diff --git a/engines/grim/movie/smush_mixer.cpp b/engines/grim/movie/smush_mixer.cpp
new file mode 100644
index 00000000000..80ddaaba27f
--- /dev/null
+++ b/engines/grim/movie/smush_mixer.cpp
@@ -0,0 +1,849 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include "common/scummsys.h"
+#include "common/endian.h"
+#include "common/system.h"
+#include "common/util.h"
+
+#include "engines/grim/movie/smush_mixer.h"
+#include "engines/grim/movie/smush_sound.h"
+#include "engines/grim/movie/smush_timer.h"
+
+namespace Grim {
+
+SmushMixer::SmushMixer() {
+ _mixBuf = nullptr;
+ _mixBufSize = 0;
+ _outChannelCount = 0;
+ memset(_mixerTable, 0, sizeof(_mixerTable));
+}
+
+SmushMixer::~SmushMixer() {
+ souMixerTerminate();
+}
+
+int SmushMixer::souMixerInitialize(int outChannelCount, int mixBufSize) {
+ for (int i = 0; i < 17; i++) {
+ for (int j = 0; j < 17; j++)
+ _mixerTable[17 * i + j] = (uint8)(int)((sin((double)j * 3.1415 * 0.03125) * (double)i + 0.5) * 8.0);
+ }
+
+ _outChannelCount = outChannelCount;
+ _mixBufSize = 4 * mixBufSize;
+ _mixBuf = (int32 *)malloc(_mixBufSize);
+ if (!_mixBuf)
+ return -1;
+
+ return 0;
+}
+
+int SmushMixer::souMixerTerminate() {
+ free(_mixBuf);
+ _mixBuf = nullptr;
+ return 0;
+}
+
+int SmushMixer::souMixerStart() {
+ memset(_mixBuf, 0, _mixBufSize);
+ return 0;
+}
+
+void SmushMixer::souMixerMix(const uint8 *srcBuf, int32 inFrameCount, int wordSize, int channelCount, uint32 feedSize, int32 mixBufStartIndex, int volume, int pan, int signedFlag, int swapEndiannessFlag) {
+ // The original could divide by zero here, let's avoid that...
+ if (!_mixBuf || !srcBuf || !inFrameCount || !feedSize)
+ return;
+
+ if (channelCount == 1) {
+ uint32 volumeIndex = volume >> 3;
+ if (volume)
+ volumeIndex++;
+ if (volumeIndex >= 17)
+ volumeIndex = 16;
+
+ int panOffset = (pan >> 3) - 8;
+ if (pan > 64)
+ panOffset++;
+
+ int leftAmp = _mixerTable[17 * volumeIndex + 8 - panOffset];
+ int rightAmp = _mixerTable[17 * volumeIndex + 8 + panOffset];
+
+ if (wordSize == 8) {
+ mixMono8(srcBuf, inFrameCount, feedSize, mixBufStartIndex, leftAmp, rightAmp);
+ } else if (signedFlag) {
+ if (swapEndiannessFlag)
+ mixMono16Swap((const uint16 *)srcBuf, inFrameCount, feedSize, mixBufStartIndex, leftAmp, rightAmp);
+ else
+ mixMono16((const int16 *)srcBuf, inFrameCount, feedSize, mixBufStartIndex, leftAmp, rightAmp);
+ } else {
+ mixMono16Unsigned((const uint16 *)srcBuf, inFrameCount, feedSize, mixBufStartIndex, leftAmp, rightAmp);
+ }
+ } else if (wordSize == 8) {
+ mixStereo8(srcBuf, inFrameCount, feedSize, mixBufStartIndex, volume + 1);
+ } else if (signedFlag) {
+ if (swapEndiannessFlag)
+ mixStereo16Swap((const uint16 *)srcBuf, inFrameCount, feedSize, mixBufStartIndex, volume + 1);
+ else
+ mixStereo16((const int16 *)srcBuf, inFrameCount, feedSize, mixBufStartIndex, volume + 1);
+ } else {
+ mixStereo16Unsigned((const uint16 *)srcBuf, inFrameCount, feedSize, mixBufStartIndex, volume + 1);
+ }
+}
+
+int SmushMixer::souMixerEnd(int16 *destBuf, int frameCount) {
+ if (!_mixBuf || !destBuf || !frameCount)
+ return -1;
+
+ int sampleCount = frameCount;
+ if (_outChannelCount == 2)
+ sampleCount = 2 * frameCount;
+
+ for (int i = 0; i < sampleCount; i++) {
+ int32 sample = _mixBuf[i] >> 7;
+ if (sample < -32768)
+ sample = -32768;
+ if (sample > 32767)
+ sample = 32767;
+
+ destBuf[i] = (int16)sample;
+ }
+
+ return 0;
+}
+
+void SmushMixer::mixMono8(const uint8 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int leftAmp, int rightAmp) {
+ int32 *mixBufCurCell = &_mixBuf[2 * mixBufStartIndex];
+ uint32 ratio = ((uint32)inFrameCount << 16) / feedSize;
+ uint32 position = 0;
+ int32 sample, interpolated;
+
+ switch (ratio) {
+ case 0x8000:
+ for (int i = 0; i < inFrameCount - 1; i++) {
+ sample = (srcBuf[0] ^ 0x80) << 8;
+ // The disasm interpolates with the sample two positions ahead, without sign extension
+ interpolated = ((srcBuf[0] + srcBuf[2]) << 7) ^ 0x8000;
+
+ mixBufCurCell[0] += sample * leftAmp;
+ mixBufCurCell[1] += sample * rightAmp;
+ mixBufCurCell[2] += interpolated * leftAmp;
+ mixBufCurCell[3] += interpolated * rightAmp;
+
+ srcBuf++;
+ mixBufCurCell += 4;
+ }
+
+ sample = (srcBuf[0] ^ 0x80) << 8;
+ mixBufCurCell[0] += sample * leftAmp;
+ mixBufCurCell[1] += sample * rightAmp;
+ mixBufCurCell[2] += sample * leftAmp;
+ mixBufCurCell[3] += sample * rightAmp;
+ break;
+ case 0x10000:
+ for (uint32 i = 0; i < feedSize; i++) {
+ sample = (srcBuf[0] ^ 0x80) << 8;
+ mixBufCurCell[0] += sample * leftAmp;
+ mixBufCurCell[1] += sample * rightAmp;
+
+ srcBuf++;
+ mixBufCurCell += 2;
+ }
+ break;
+ case 0x20000:
+ for (uint32 i = 0; i < feedSize; i++) {
+ sample = (srcBuf[0] ^ 0x80) << 8;
+ mixBufCurCell[0] += sample * leftAmp;
+ mixBufCurCell[1] += sample * rightAmp;
+
+ srcBuf += 2;
+ mixBufCurCell += 2;
+ }
+ break;
+ default:
+ for (uint32 i = 0; i < feedSize; i++) {
+ sample = (srcBuf[0] ^ 0x80) << 8;
+ mixBufCurCell[0] += sample * leftAmp;
+ mixBufCurCell[1] += sample * rightAmp;
+
+ mixBufCurCell += 2;
+ position += ratio;
+ srcBuf += position >> 16;
+ position &= 0xFFFF;
+ }
+ break;
+ }
+}
+
+void SmushMixer::mixStereo8(const uint8 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int volume) {
+ int32 *mixBufCurCell = &_mixBuf[2 * mixBufStartIndex];
+ uint32 ratio = ((uint32)inFrameCount << 16) / feedSize;
+ uint32 position = 0;
+ int32 left, right, interpolatedLeft, interpolatedRight;
+
+ switch (ratio) {
+ case 0x8000:
+ for (int i = 0; i < inFrameCount - 1; i++) {
+ left = (srcBuf[0] ^ 0x80) << 8;
+ right = (srcBuf[1] ^ 0x80) << 8;
+ interpolatedLeft = ((srcBuf[0] + srcBuf[2]) << 7) ^ 0x8000;
+ interpolatedRight = ((srcBuf[1] + srcBuf[3]) << 7) ^ 0x8000;
+
+ mixBufCurCell[0] += left * volume;
+ mixBufCurCell[1] += right * volume;
+ mixBufCurCell[2] += interpolatedLeft * volume;
+ mixBufCurCell[3] += interpolatedRight * volume;
+
+ srcBuf += 2;
+ mixBufCurCell += 4;
+ }
+
+ left = (srcBuf[0] ^ 0x80) << 8;
+ right = (srcBuf[1] ^ 0x80) << 8;
+ mixBufCurCell[0] += left * volume;
+ mixBufCurCell[1] += right * volume;
+ mixBufCurCell[2] += left * volume;
+ mixBufCurCell[3] += right * volume;
+ break;
+ case 0x10000:
+ for (uint32 i = 0; i < 2 * feedSize; i++) {
+ mixBufCurCell[0] += ((srcBuf[0] ^ 0x80) << 8) * volume;
+
+ srcBuf++;
+ mixBufCurCell++;
+ }
+ break;
+ case 0x20000:
+ for (uint32 i = 0; i < feedSize; i++) {
+ mixBufCurCell[0] += ((srcBuf[0] ^ 0x80) << 8) * volume;
+ mixBufCurCell[1] += ((srcBuf[1] ^ 0x80) << 8) * volume;
+
+ srcBuf += 4;
+ mixBufCurCell += 2;
+ }
+ break;
+ default:
+ for (uint32 i = 0; i < feedSize; i++) {
+ mixBufCurCell[0] += ((srcBuf[0] ^ 0x80) << 8) * volume;
+ mixBufCurCell[1] += ((srcBuf[1] ^ 0x80) << 8) * volume;
+
+ mixBufCurCell += 2;
+ position += 2 * ratio;
+ srcBuf += position >> 16;
+ position &= 0xFFFF;
+ }
+ break;
+ }
+}
+
+void SmushMixer::mixMono16(const int16 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int leftAmp, int rightAmp) {
+ int32 *mixBufCurCell = &_mixBuf[2 * mixBufStartIndex];
+ uint32 ratio = ((uint32)inFrameCount << 16) / feedSize;
+ uint32 position = 0;
+ int32 interpolated;
+
+ switch (ratio) {
+ case 0x8000:
+ for (int i = 0; i < inFrameCount - 1; i++) {
+ // The disasm interpolates with the sample two positions ahead
+ interpolated = (srcBuf[0] + srcBuf[2]) >> 1;
+
+ mixBufCurCell[0] += srcBuf[0] * leftAmp;
+ mixBufCurCell[1] += srcBuf[0] * rightAmp;
+ mixBufCurCell[2] += interpolated * leftAmp;
+ mixBufCurCell[3] += interpolated * rightAmp;
+
+ srcBuf++;
+ mixBufCurCell += 4;
+ }
+
+ mixBufCurCell[0] += srcBuf[0] * leftAmp;
+ mixBufCurCell[1] += srcBuf[0] * rightAmp;
+ mixBufCurCell[2] += srcBuf[0] * leftAmp;
+ mixBufCurCell[3] += srcBuf[0] * rightAmp;
+ break;
+ case 0x10000:
+ for (uint32 i = 0; i < feedSize; i++) {
+ mixBufCurCell[0] += srcBuf[0] * leftAmp;
+ mixBufCurCell[1] += srcBuf[0] * rightAmp;
+
+ srcBuf++;
+ mixBufCurCell += 2;
+ }
+ break;
+ case 0x20000:
+ for (uint32 i = 0; i < feedSize; i++) {
+ mixBufCurCell[0] += srcBuf[0] * leftAmp;
+ mixBufCurCell[1] += srcBuf[0] * rightAmp;
+
+ srcBuf += 2;
+ mixBufCurCell += 2;
+ }
+ break;
+ default:
+ for (uint32 i = 0; i < feedSize; i++) {
+ mixBufCurCell[0] += srcBuf[0] * leftAmp;
+ mixBufCurCell[1] += srcBuf[0] * rightAmp;
+
+ mixBufCurCell += 2;
+ position += ratio;
+ srcBuf += position >> 16;
+ position &= 0xFFFF;
+ }
+ break;
+ }
+}
+
+void SmushMixer::mixStereo16(const int16 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int volume) {
+ int32 *mixBufCurCell = &_mixBuf[2 * mixBufStartIndex];
+ uint32 ratio = ((uint32)inFrameCount << 16) / feedSize;
+ uint32 position = 0;
+
+ switch (ratio) {
+ case 0x8000:
+ for (int i = 0; i < inFrameCount - 1; i++) {
+ mixBufCurCell[0] += srcBuf[0] * volume;
+ mixBufCurCell[1] += srcBuf[1] * volume;
+ mixBufCurCell[2] += ((srcBuf[0] + srcBuf[2]) >> 1) * volume;
+ mixBufCurCell[3] += ((srcBuf[1] + srcBuf[3]) >> 1) * volume;
+
+ srcBuf += 2;
+ mixBufCurCell += 4;
+ }
+
+ mixBufCurCell[0] += srcBuf[0] * volume;
+ mixBufCurCell[1] += srcBuf[1] * volume;
+ mixBufCurCell[2] += srcBuf[0] * volume;
+ mixBufCurCell[3] += srcBuf[1] * volume;
+ break;
+ case 0x10000:
+ for (uint32 i = 0; i < 2 * feedSize; i++) {
+ mixBufCurCell[0] += srcBuf[0] * volume;
+
+ srcBuf++;
+ mixBufCurCell++;
+ }
+ break;
+ case 0x20000:
+ for (uint32 i = 0; i < feedSize; i++) {
+ mixBufCurCell[0] += srcBuf[0] * volume;
+ mixBufCurCell[1] += srcBuf[1] * volume;
+
+ srcBuf += 4;
+ mixBufCurCell += 2;
+ }
+ break;
+ default:
+ for (uint32 i = 0; i < feedSize; i++) {
+ mixBufCurCell[0] += srcBuf[0] * volume;
+ mixBufCurCell[1] += srcBuf[1] * volume;
+
+ mixBufCurCell += 2;
+ position += 2 * ratio;
+ srcBuf += position >> 16;
+ position &= 0xFFFF;
+ }
+ break;
+ }
+}
+
+void SmushMixer::mixMono16Unsigned(const uint16 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int leftAmp, int rightAmp) {
+ int32 *mixBufCurCell = &_mixBuf[2 * mixBufStartIndex];
+ uint32 ratio = ((uint32)inFrameCount << 16) / feedSize;
+ uint32 position = 0;
+ int32 sample, interpolated;
+
+ switch (ratio) {
+ case 0x8000:
+ for (int i = 0; i < inFrameCount - 1; i++) {
+ sample = (int16)(srcBuf[0] ^ 0x8000);
+ // The disasm interpolates with the sample two positions ahead
+ interpolated = (sample + (int16)(srcBuf[2] ^ 0x8000)) >> 1;
+
+ mixBufCurCell[0] += sample * leftAmp;
+ mixBufCurCell[1] += sample * rightAmp;
+ mixBufCurCell[2] += interpolated * leftAmp;
+ mixBufCurCell[3] += interpolated * rightAmp;
+
+ srcBuf++;
+ mixBufCurCell += 4;
+ }
+
+ sample = (int16)(srcBuf[0] ^ 0x8000);
+ mixBufCurCell[0] += sample * leftAmp;
+ mixBufCurCell[1] += sample * rightAmp;
+ mixBufCurCell[2] += sample * leftAmp;
+ mixBufCurCell[3] += sample * rightAmp;
+ break;
+ case 0x10000:
+ for (uint32 i = 0; i < feedSize; i++) {
+ sample = (int16)(srcBuf[0] ^ 0x8000);
+ mixBufCurCell[0] += sample * leftAmp;
+ mixBufCurCell[1] += sample * rightAmp;
+
+ srcBuf++;
+ mixBufCurCell += 2;
+ }
+ break;
+ case 0x20000:
+ for (uint32 i = 0; i < feedSize; i++) {
+ sample = (int16)(srcBuf[0] ^ 0x8000);
+ mixBufCurCell[0] += sample * leftAmp;
+ mixBufCurCell[1] += sample * rightAmp;
+
+ srcBuf += 2;
+ mixBufCurCell += 2;
+ }
+ break;
+ default:
+ for (uint32 i = 0; i < feedSize; i++) {
+ sample = (int16)(srcBuf[0] ^ 0x8000);
+ mixBufCurCell[0] += sample * leftAmp;
+ mixBufCurCell[1] += sample * rightAmp;
+
+ mixBufCurCell += 2;
+ position += ratio;
+ srcBuf += position >> 16;
+ position &= 0xFFFF;
+ }
+ break;
+ }
+}
+
+void SmushMixer::mixStereo16Unsigned(const uint16 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int volume) {
+ int32 *mixBufCurCell = &_mixBuf[2 * mixBufStartIndex];
+ uint32 ratio = ((uint32)inFrameCount << 16) / feedSize;
+ uint32 position = 0;
+ int32 left, right;
+
+ switch (ratio) {
+ case 0x8000:
+ for (int i = 0; i < inFrameCount - 1; i++) {
+ left = (int16)(srcBuf[0] ^ 0x8000);
+ right = (int16)(srcBuf[1] ^ 0x8000);
+
+ mixBufCurCell[0] += left * volume;
+ mixBufCurCell[1] += right * volume;
+ mixBufCurCell[2] += ((left + (int16)(srcBuf[2] ^ 0x8000)) >> 1) * volume;
+ mixBufCurCell[3] += ((right + (int16)(srcBuf[3] ^ 0x8000)) >> 1) * volume;
+
+ srcBuf += 2;
+ mixBufCurCell += 4;
+ }
+
+ left = (int16)(srcBuf[0] ^ 0x8000);
+ right = (int16)(srcBuf[1] ^ 0x8000);
+ mixBufCurCell[0] += left * volume;
+ mixBufCurCell[1] += right * volume;
+ mixBufCurCell[2] += left * volume;
+ mixBufCurCell[3] += right * volume;
+ break;
+ case 0x10000:
+ for (uint32 i = 0; i < 2 * feedSize; i++) {
+ mixBufCurCell[0] += (int16)(srcBuf[0] ^ 0x8000) * volume;
+
+ srcBuf++;
+ mixBufCurCell++;
+ }
+ break;
+ case 0x20000:
+ for (uint32 i = 0; i < feedSize; i++) {
+ mixBufCurCell[0] += (int16)(srcBuf[0] ^ 0x8000) * volume;
+ mixBufCurCell[1] += (int16)(srcBuf[1] ^ 0x8000) * volume;
+
+ srcBuf += 4;
+ mixBufCurCell += 2;
+ }
+ break;
+ default:
+ for (uint32 i = 0; i < feedSize; i++) {
+ mixBufCurCell[0] += (int16)(srcBuf[0] ^ 0x8000) * volume;
+ mixBufCurCell[1] += (int16)(srcBuf[1] ^ 0x8000) * volume;
+
+ mixBufCurCell += 2;
+ position += 2 * ratio;
+ srcBuf += position >> 16;
+ position &= 0xFFFF;
+ }
+ break;
+ }
+}
+
+void SmushMixer::mixMono16Swap(const uint16 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int leftAmp, int rightAmp) {
+ int32 *mixBufCurCell = &_mixBuf[2 * mixBufStartIndex];
+ uint32 ratio = ((uint32)inFrameCount << 16) / feedSize;
+ uint32 position = 0;
+ int32 sample, interpolated;
+
+ switch (ratio) {
+ case 0x8000:
+ for (int i = 0; i < inFrameCount - 1; i++) {
+ sample = (uint16)SWAP_BYTES_16(srcBuf[0]);
+ interpolated = (int16)SWAP_BYTES_16((uint16)(((int16)srcBuf[0] + (int16)srcBuf[2]) >> 1));
+
+ mixBufCurCell[0] += sample * leftAmp;
+ mixBufCurCell[1] += sample * rightAmp;
+ mixBufCurCell[2] += interpolated * leftAmp;
+ mixBufCurCell[3] += interpolated * rightAmp;
+
+ srcBuf++;
+ mixBufCurCell += 4;
+ }
+
+ sample = (int16)SWAP_BYTES_16(srcBuf[0]);
+ mixBufCurCell[0] += sample * leftAmp;
+ mixBufCurCell[1] += sample * rightAmp;
+ mixBufCurCell[2] += sample * leftAmp;
+ mixBufCurCell[3] += sample * rightAmp;
+ break;
+ case 0x10000:
+ for (uint32 i = 0; i < feedSize; i++) {
+ sample = (uint16)SWAP_BYTES_16(srcBuf[0]);
+ mixBufCurCell[0] += sample * leftAmp;
+ mixBufCurCell[1] += sample * rightAmp;
+
+ srcBuf++;
+ mixBufCurCell += 2;
+ }
+ break;
+ case 0x20000:
+ for (uint32 i = 0; i < feedSize; i++) {
+ sample = (uint16)SWAP_BYTES_16(srcBuf[0]);
+ mixBufCurCell[0] += sample * leftAmp;
+ mixBufCurCell[1] += sample * rightAmp;
+
+ srcBuf += 2;
+ mixBufCurCell += 2;
+ }
+ break;
+ default:
+ for (uint32 i = 0; i < feedSize; i++) {
+ sample = (uint16)SWAP_BYTES_16(srcBuf[0]);
+ mixBufCurCell[0] += sample * leftAmp;
+ mixBufCurCell[1] += sample * rightAmp;
+
+ mixBufCurCell += 2;
+ position += ratio;
+ srcBuf += position >> 16;
+ position &= 0xFFFF;
+ }
+ break;
+ }
+}
+
+void SmushMixer::mixStereo16Swap(const uint16 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int volume) {
+ int32 *mixBufCurCell = &_mixBuf[2 * mixBufStartIndex];
+ uint32 ratio = ((uint32)inFrameCount << 16) / feedSize;
+ uint32 position = 0;
+
+ switch (ratio) {
+ case 0x8000:
+ for (int i = 0; i < inFrameCount - 1; i++) {
+ mixBufCurCell[0] += volume * (uint16)SWAP_BYTES_16(srcBuf[0]);
+ mixBufCurCell[1] += volume * (uint16)SWAP_BYTES_16(srcBuf[1]);
+ mixBufCurCell[2] += volume * (uint16)SWAP_BYTES_16((uint16)(((int16)srcBuf[0] + (int16)srcBuf[2]) >> 1));
+ mixBufCurCell[3] += volume * (uint16)SWAP_BYTES_16((uint16)(((int16)srcBuf[1] + (int16)srcBuf[3]) >> 1));
+
+ srcBuf += 2;
+ mixBufCurCell += 4;
+ }
+
+ mixBufCurCell[0] += volume * (uint16)SWAP_BYTES_16(srcBuf[0]);
+ mixBufCurCell[1] += volume * (uint16)SWAP_BYTES_16(srcBuf[1]);
+ mixBufCurCell[2] += volume * (uint16)SWAP_BYTES_16(srcBuf[0]);
+ mixBufCurCell[3] += volume * (uint16)SWAP_BYTES_16(srcBuf[1]);
+ break;
+ case 0x10000:
+ for (uint32 i = 0; i < 2 * feedSize; i++) {
+ mixBufCurCell[0] += volume * (uint16)SWAP_BYTES_16(srcBuf[0]);
+
+ srcBuf++;
+ mixBufCurCell++;
+ }
+ break;
+ case 0x20000:
+ for (uint32 i = 0; i < feedSize; i++) {
+ mixBufCurCell[0] += volume * (uint16)SWAP_BYTES_16(srcBuf[0]);
+ mixBufCurCell[1] += volume * (uint16)SWAP_BYTES_16(srcBuf[1]);
+
+ srcBuf += 4;
+ mixBufCurCell += 2;
+ }
+ break;
+ default:
+ for (uint32 i = 0; i < feedSize; i++) {
+ mixBufCurCell[0] += volume * (uint16)SWAP_BYTES_16(srcBuf[0]);
+ mixBufCurCell[1] += volume * (uint16)SWAP_BYTES_16(srcBuf[1]);
+
+ mixBufCurCell += 2;
+ position += 2 * ratio;
+ srcBuf += position >> 16;
+ position &= 0xFFFF;
+ }
+ break;
+ }
+}
+
+SmushDriverStream::SmushDriverStream(SmushDriver *driver) {
+ _driver = driver;
+}
+
+int SmushDriverStream::readBuffer(int16 *buffer, const int numSamples) {
+ return _driver->ringRead(buffer, numSamples);
+}
+
+SmushDriver::SmushDriver(SmushTimer *timer) {
+ _timer = timer;
+ _sound = nullptr;
+ _system = g_system->getMixer();
+ _ring = nullptr;
+ _ringBlockCount = 0;
+ _ringPlayPosition = 0;
+ _lastBlock = -1;
+ _driverRate = 0;
+ _driverVolume = 0;
+ _fillTimer = 0;
+ memset(_volumeTable, 0, sizeof(_volumeTable));
+ _volumeTableBuilt = false;
+}
+
+SmushDriver::~SmushDriver() {
+ sndDrivTerminate();
+}
+
+int SmushDriver::sndDrivInitialize(int rate, int channelCount, SmushSound *sound) {
+ if (rate < 22050)
+ rate = 22050;
+
+ if (sndDrivOpenRing())
+ return -1;
+
+ if (sndDrivMixerInitialize(4, 1024))
+ return -1;
+
+ _sound = sound;
+ _driverRate = rate;
+ _fillTimer = _timer->sysAlloc(sndDrivFillThreadCallback, 1193180 / (rate / 1024), SYS_TIMER_ONE_SHOT, this);
+
+ return 0;
+}
+
+void SmushDriver::sndDrivTerminate() {
+ if (_fillTimer)
+ _timer->sysFree(_fillTimer);
+
+ _fillTimer = 0;
+ sndDrivMixerTerminate();
+ sndDrivCloseRing();
+}
+
+int SmushDriver::sndDrivSetVol(int volume) {
+ if (!_volumeTableBuilt) {
+ _volumeTable[0] = -10000;
+ _volumeTable[1] = -7000;
+ _volumeTable[2] = -6000;
+ _volumeTable[3] = -5200;
+ _volumeTable[4] = -5000;
+ _volumeTable[5] = -4500;
+ _volumeTable[6] = -4200;
+ _volumeTable[7] = -4100;
+ _volumeTable[8] = -4000;
+
+ int position = 16000;
+ for (int i = 8; i < 32; i++) {
+ int value = (int)(((uint64)2987803337LL * (uint64)(position - 16000)) >> 32) >> 4;
+ position += 2000;
+ _volumeTable[i] = value + (value >> 31) - 4000;
+ }
+
+ position = 32000;
+ for (int i = 32; i < 64; i++) {
+ int value = (int)(((uint64)2216757315LL * (uint64)(position - 32000)) >> 32) >> 4;
+ position += 1000;
+ _volumeTable[i] = value + (value >> 31) - 2000;
+ }
+
+ position = 64000;
+ for (int i = 64; i < 128; i++) {
+ int value = (int)(((uint64)2181570691LL * (uint64)(position - 64000)) >> 32) >> 5;
+ position += 1000;
+ _volumeTable[i] = value + (value >> 31) - 1000;
+ }
+
+ _volumeTableBuilt = true;
+ }
+
+ _driverVolume = volume & 0x7F;
+
+ int centibels = _volumeTable[_driverVolume];
+ int hostVolume = (int)(Audio::Mixer::kMaxChannelVolume * pow(10.0, centibels / 2000.0) + 0.5);
+ _system->setChannelVolume(_soundHandle, hostVolume);
+
+ return 0;
+}
+
+int SmushDriver::sndDrivGetVol() {
+ return _driverVolume;
+}
+
+int SmushDriver::sndDrivGetRate() {
+ return _driverRate;
+}
+
+int SmushDriver::sndDrivDummy(int rate) {
+ return rate;
+}
+
+void SmushDriver::sndDrivFillThread() {
+ int playBlock = sndDrivGetPlayBlock();
+ if (playBlock != -1) {
+ int lastBlock = _lastBlock;
+ if (playBlock != (lastBlock + 1) % _ringBlockCount) {
+ if (lastBlock < 0)
+ lastBlock = playBlock;
+
+ _lastBlock = (lastBlock + 1) % _ringBlockCount;
+
+ int16 blockBuf[SMUSH_RING_BLOCK_SIZE / 2];
+ int frameCount = SMUSH_RING_BLOCK_SIZE / 4;
+
+ _mixer.souMixerStart();
+ _sound->souEngineFill(frameCount, this);
+ sndDrivMixerEnd(blockBuf, frameCount);
+
+ Common::StackLock lock(_ringMutex);
+ memcpy(_ring + _lastBlock * SMUSH_RING_BLOCK_SIZE, blockBuf, SMUSH_RING_BLOCK_SIZE);
+ }
+ }
+
+ _timer->sysReset(_fillTimer, 1193180 / (_driverRate / 1024));
+}
+
+void SmushDriver::sndDrivMix(const uint8 *srcBuf, int32 mixBufStartIndex, uint32 feedSize, int32 srcByteCount, int reserved, int volume, int pan, int format) {
+ int channelCount, wordSize, signedFlag;
+
+ switch (format) {
+ case 0:
+ channelCount = 1;
+ wordSize = 8;
+ signedFlag = 1;
+ break;
+ case 1:
+ channelCount = 2;
+ wordSize = 8;
+ signedFlag = 1;
+ break;
+ case 2:
+ channelCount = 1;
+ wordSize = 16;
+ signedFlag = 0;
+ break;
+ case 3:
+ channelCount = 2;
+ wordSize = 16;
+ signedFlag = 0;
+ break;
+ case 4:
+ channelCount = 1;
+ wordSize = 16;
+ signedFlag = 1;
+ break;
+ case 5:
+ channelCount = 2;
+ wordSize = 16;
+ signedFlag = 1;
+ break;
+ default:
+ channelCount = format;
+ wordSize = format;
+ signedFlag = format;
+ break;
+ }
+
+ _mixer.souMixerMix(srcBuf, srcByteCount / (channelCount * (wordSize >> 3)), wordSize, channelCount, feedSize, mixBufStartIndex, volume, (pan + 127) / 2, signedFlag, 0);
+}
+
+void SmushDriver::sndDrivFillThreadCallback(int timer, void *context) {
+ ((SmushDriver *)context)->sndDrivFillThread();
+}
+
+int SmushDriver::sndDrivOpenRing() {
+ _ringBlockCount = 3;
+ _ringPlayPosition = 0;
+ _ring = (byte *)calloc(_ringBlockCount * SMUSH_RING_BLOCK_SIZE, 1);
+ if (!_ring)
+ return -1;
+
+ _system->playStream(Audio::Mixer::kSFXSoundType, &_soundHandle, new SmushDriverStream(this), -1, Audio::Mixer::kMaxChannelVolume, 0, DisposeAfterUse::YES);
+ return 0;
+}
+
+void SmushDriver::sndDrivCloseRing() {
+ _system->stopHandle(_soundHandle);
+
+ Common::StackLock lock(_ringMutex);
+ free(_ring);
+ _ring = nullptr;
+}
+
+int SmushDriver::sndDrivGetPlayBlock() {
+ Common::StackLock lock(_ringMutex);
+ if (!_ring)
+ return -1;
+
+ return _ringPlayPosition / SMUSH_RING_BLOCK_SIZE % _ringBlockCount;
+}
+
+int SmushDriver::sndDrivMixerInitialize(int arg, int size) {
+ return _mixer.souMixerInitialize(2, 4 * size);
+}
+
+void SmushDriver::sndDrivMixerTerminate() {
+ _mixer.souMixerTerminate();
+}
+
+int SmushDriver::sndDrivMixerEnd(int16 *destBuf, int frameCount) {
+ return _mixer.souMixerEnd(destBuf, frameCount);
+}
+
+int SmushDriver::ringRead(int16 *buffer, int numSamples) {
+ Common::StackLock lock(_ringMutex);
+ if (!_ring) {
+ memset(buffer, 0, numSamples * 2);
+ return numSamples;
+ }
+
+ int ringSize = _ringBlockCount * SMUSH_RING_BLOCK_SIZE;
+ byte *dest = (byte *)buffer;
+ int remaining = numSamples * 2;
+
+ while (remaining > 0) {
+ int chunk = MIN(remaining, ringSize - _ringPlayPosition);
+ memcpy(dest, _ring + _ringPlayPosition, chunk);
+
+ dest += chunk;
+ remaining -= chunk;
+ _ringPlayPosition = (_ringPlayPosition + chunk) % ringSize;
+ }
+
+ return numSamples;
+}
+
+} // End of namespace Grim
diff --git a/engines/grim/movie/smush_mixer.h b/engines/grim/movie/smush_mixer.h
new file mode 100644
index 00000000000..e6fd74afc79
--- /dev/null
+++ b/engines/grim/movie/smush_mixer.h
@@ -0,0 +1,128 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifndef GRIM_MOVIE_SMUSH_MIXER_H
+#define GRIM_MOVIE_SMUSH_MIXER_H
+
+#include "common/scummsys.h"
+#include "common/mutex.h"
+#include "audio/audiostream.h"
+#include "audio/mixer.h"
+
+namespace Grim {
+
+#define SMUSH_RING_BLOCK_SIZE 6144
+
+class SmushTimer;
+class SmushSound;
+
+class SmushMixer {
+
+private:
+ int32 *_mixBuf;
+ int _mixBufSize;
+ int _outChannelCount;
+ byte _mixerTable[17 * 17];
+
+ void mixMono8(const uint8 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int leftAmp, int rightAmp);
+ void mixStereo8(const uint8 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int volume);
+ void mixMono16(const int16 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int leftAmp, int rightAmp);
+ void mixStereo16(const int16 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int volume);
+ void mixMono16Unsigned(const uint16 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int leftAmp, int rightAmp);
+ void mixStereo16Unsigned(const uint16 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int volume);
+ void mixMono16Swap(const uint16 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int leftAmp, int rightAmp);
+ void mixStereo16Swap(const uint16 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int volume);
+
+public:
+ SmushMixer();
+ ~SmushMixer();
+
+ int souMixerInitialize(int outChannelCount, int mixBufSize);
+ int souMixerTerminate();
+ int souMixerStart();
+ void souMixerMix(const uint8 *srcBuf, int32 inFrameCount, int wordSize, int channelCount, uint32 feedSize, int32 mixBufStartIndex, int volume, int pan, int signedFlag, int swapEndiannessFlag);
+ int souMixerEnd(int16 *destBuf, int frameCount);
+
+};
+
+class SmushDriver;
+
+class SmushDriverStream : public Audio::AudioStream {
+
+private:
+ SmushDriver *_driver;
+
+public:
+ SmushDriverStream(SmushDriver *driver);
+
+ int readBuffer(int16 *buffer, const int numSamples) override;
+ bool isStereo() const override { return true; }
+ int getRate() const override { return 22050; }
+ bool endOfData() const override { return false; }
+
+};
+
+class SmushDriver {
+
+private:
+ SmushMixer _mixer;
+ SmushTimer *_timer;
+ SmushSound *_sound;
+ Audio::Mixer *_system;
+ Audio::SoundHandle _soundHandle;
+ Common::Mutex _ringMutex;
+ byte *_ring;
+ int _ringBlockCount;
+ int _ringPlayPosition;
+ int _lastBlock;
+ int _driverRate;
+ int _driverVolume;
+ int _fillTimer;
+ int _volumeTable[128];
+ bool _volumeTableBuilt;
+
+ int sndDrivOpenRing();
+ void sndDrivCloseRing();
+ int sndDrivGetPlayBlock();
+ int sndDrivMixerInitialize(int arg, int size);
+ void sndDrivMixerTerminate();
+ int sndDrivMixerEnd(int16 *destBuf, int frameCount);
+
+public:
+ SmushDriver(SmushTimer *timer);
+ ~SmushDriver();
+
+ int sndDrivInitialize(int rate, int channelCount, SmushSound *sound);
+ void sndDrivTerminate();
+ int sndDrivSetVol(int volume);
+ int sndDrivGetVol();
+ int sndDrivGetRate();
+ int sndDrivDummy(int rate);
+ void sndDrivFillThread();
+ void sndDrivMix(const uint8 *srcBuf, int32 mixBufStartIndex, uint32 feedSize, int32 srcByteCount, int reserved, int volume, int pan, int format);
+ static void sndDrivFillThreadCallback(int timer, void *context);
+
+ int ringRead(int16 *buffer, int numSamples);
+
+};
+
+} // End of namespace Grim
+#endif
diff --git a/engines/grim/movie/smush_sound.cpp b/engines/grim/movie/smush_sound.cpp
new file mode 100644
index 00000000000..ee7087287d5
--- /dev/null
+++ b/engines/grim/movie/smush_sound.cpp
@@ -0,0 +1,1093 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include "common/scummsys.h"
+#include "common/endian.h"
+#include "common/util.h"
+
+#include "engines/grim/movie/smush_mixer.h"
+#include "engines/grim/movie/smush_sound.h"
+#include "engines/grim/movie/smush_timer.h"
+
+namespace Grim {
+
+#define SOU_BUFFER_SLACK 6144
+
+SmushSound::SmushSound(SmushTimer *timer, SmushDriver *driver) {
+ _timer = timer;
+ _driver = driver;
+ memset(_channels, 0, sizeof(_channels));
+ memset(_playback, 0, sizeof(_playback));
+ _channelCount = 0;
+ _masterVolume = 127;
+ _sampleRate = 22050;
+ _duckFloor = 64;
+ _duckRate = 256;
+ _duckLevel = 256;
+ _resetFlag = 1;
+ _bytesPerFrame[0] = 1;
+ _bytesPerFrame[1] = 2;
+ _bytesPerFrame[2] = 2;
+ _bytesPerFrame[3] = 4;
+ _bytesPerFrame[4] = 2;
+ _bytesPerFrame[5] = 4;
+ memset(_groupVolumes, 0, sizeof(_groupVolumes));
+ _pauseCount = 0;
+ _engineOk = 0;
+ _syncMark = 0;
+ _syncAccumulator = 0.0;
+ _fillCounter = 0;
+ _musicParam = 0;
+ _voiceParam = 0;
+ _normalParam = 0;
+ _panInvert = 0;
+ _driverRate = 22050;
+ _reserveRam = 64000;
+ memset(_bufferSizes, 0, sizeof(_bufferSizes));
+ _bufferSizes[0] = 500000;
+}
+
+SmushSound::~SmushSound() {
+ engineTerminate();
+}
+
+int SmushSound::moduleStartup() {
+ return engineInitialize(_driverRate, _reserveRam, _bufferSizes);
+}
+
+int SmushSound::moduleShutdown() {
+ return engineTerminate();
+}
+
+int SmushSound::engineInitialize(int rate, int reserveRam, const int *bufferSizes) {
+ _channelCount = 0;
+ _masterVolume = 127;
+ memset(_groupVolumes, 0, sizeof(_groupVolumes));
+
+ int size = bufferSizes[0];
+ int index = 1;
+ while (size) {
+ size &= ~3;
+ if (engineAllocChannel(size, reserveRam)) {
+ engineTerminate();
+ return -1;
+ }
+
+ size = bufferSizes[index++];
+ }
+
+ _engineOk = (_driver->sndDrivInitialize(rate, _channelCount, this) == 0);
+ _sampleRate = _driver->sndDrivGetRate();
+ _fillCounter = 0;
+ _musicParam = 1;
+ _voiceParam = 1;
+ _normalParam = 1;
+ _timer->sysExtSync(0);
+
+ return 0;
+}
+
+int SmushSound::engineTerminate() {
+ if (_engineOk) {
+ _driver->sndDrivTerminate();
+ _engineOk = 0;
+ }
+
+ for (int i = 0; i < _channelCount; i++) {
+ free(_channels[i].dataBuffer);
+ _channels[i].dataBuffer = nullptr;
+ free(_channels[i].stagingBuffer);
+ _channels[i].stagingBuffer = nullptr;
+ }
+
+ int result = _fillCounter;
+ _channelCount = 0;
+ return result;
+}
+
+int SmushSound::engineAllocChannel(int size, int reserveRam) {
+ SmushSoundChannel *chan = &_channels[_channelCount];
+
+ chan->state = 0;
+ chan->consumedPosition = 0;
+ chan->soundClass = 0;
+ chan->soundId = SOU_FREE_ID;
+ chan->dataBuffer = nullptr;
+ chan->stagingBuffer = nullptr;
+ chan->capacity = size & ~3;
+
+ chan->stagingBuffer = (byte *)calloc(6144, 1);
+ if (!chan->stagingBuffer)
+ return -1;
+
+ chan->dataBuffer = (byte *)calloc(chan->capacity + SOU_BUFFER_SLACK, 1);
+ if (chan->dataBuffer) {
+ memset(chan->dataBuffer, 127, chan->capacity);
+ _channelCount++;
+ return 0;
+ }
+
+ if (chan->capacity > reserveRam) {
+ do {
+ chan->dataBuffer = (byte *)calloc(chan->capacity + SOU_BUFFER_SLACK, 1);
+ if (chan->dataBuffer)
+ break;
+
+ chan->capacity -= 10000;
+ } while (chan->capacity > reserveRam);
+ }
+
+ chan->capacity &= ~3;
+ if (chan->capacity > reserveRam) {
+ free(chan->dataBuffer);
+ chan->dataBuffer = nullptr;
+
+ chan->capacity -= reserveRam;
+ chan->dataBuffer = (byte *)calloc(chan->capacity + SOU_BUFFER_SLACK, 1);
+ if (chan->dataBuffer) {
+ memset(chan->dataBuffer, 127, chan->capacity);
+ _channelCount++;
+ return 0;
+ }
+ }
+
+ free(chan->stagingBuffer);
+ chan->stagingBuffer = nullptr;
+ return -2;
+}
+
+int SmushSound::souEngineStart(const byte *data, int soundId, int volume, int pan, int soundClass) {
+ if (!_engineOk)
+ return 0;
+
+ uint32 tag = READ_BE_UINT32(data);
+ if (tag == MKTAG('P', 'S', 'A', 'D') || tag == MKTAG('P', 'V', 'O', 'C')) {
+ if (data[8] || data[9] || data[12] || data[13] || data[16] || data[17]) {
+ int8 panByte = (int8)data[17];
+ int newPan;
+ if (panByte == -128)
+ newPan = 128;
+ else
+ newPan = panByte + pan;
+
+ return souEngineStartStream(data + 18, READ_BE_UINT32(data + 4) - 10, soundId, (volume * data[16]) >> 7, newPan,
+ soundClass | READ_LE_INT16(data + 14), READ_LE_INT16(data + 8), READ_LE_INT16(data + 10), READ_LE_INT16(data + 12), 1);
+ }
+
+ return souEngineStartStream(data + 20, READ_BE_UINT32(data + 4) - 12, soundId, volume, pan, soundClass,
+ READ_BE_UINT16(data + 10), READ_BE_UINT16(data + 14), READ_BE_UINT16(data + 18), 1);
+ }
+
+ if (tag == MKTAG('C', 'r', 'e', 'a')) {
+ int offset = data[20] + (data[21] << 8);
+ // The disasm builds the size by repeating a single byte three times
+ return souEngineStartCreative(data + offset + 6, 65793 * data[offset + 1], soundId, volume, pan, soundClass);
+ }
+
+ if (tag == MKTAG('S', 'A', 'U', 'D'))
+ return souEngineStartStream(data, READ_BE_UINT32(data + 4) + 8, soundId, volume, pan, soundClass, 0, 0, 1, 0);
+
+ return 0;
+}
+
+void SmushSound::souEngineStop(int soundId) {
+ for (int i = 0; i < _channelCount; i++) {
+ if (_channels[i].soundId == soundId) {
+ _channels[i].state = 0;
+ _channels[i].soundId = SOU_FREE_ID;
+ }
+ }
+}
+
+void SmushSound::souEnginePause() {
+ _pauseCount++;
+}
+
+int SmushSound::souEngineResume() {
+ if (_pauseCount)
+ return --_pauseCount;
+
+ return _pauseCount;
+}
+
+void SmushSound::souEngineStopAll() {
+ for (int i = 0; i < _channelCount; i++) {
+ _channels[i].state = 0;
+ _channels[i].soundId = SOU_FREE_ID;
+ }
+}
+
+void SmushSound::souEngineSetVol(int soundId, int volume) {
+ if (soundId == SOU_FREE_ID) {
+ _masterVolume = volume;
+ return;
+ }
+
+ for (int i = 0; i < _channelCount; i++) {
+ if (_channels[i].soundId == soundId)
+ _channels[i].volume = volume;
+ }
+}
+
+int SmushSound::souEngineGetVol(int soundId) {
+ if (soundId == SOU_FREE_ID)
+ return _masterVolume;
+
+ for (int i = 0; i < _channelCount; i++) {
+ if (_channels[i].soundId == soundId)
+ return _channels[i].volume;
+ }
+
+ return -1;
+}
+
+void SmushSound::souEngineSetPan(int soundId, int pan) {
+ for (int i = 0; i < _channelCount; i++) {
+ if (_channels[i].soundId == soundId)
+ _channels[i].pan = pan;
+ }
+}
+
+int SmushSound::souEngineGetPan(int soundId) {
+ for (int i = 0; i < _channelCount; i++) {
+ if (_channels[i].soundId == soundId)
+ return _channels[i].pan;
+ }
+
+ return -1;
+}
+
+int SmushSound::souEngineSetHVol(int volume) {
+ if (_driver)
+ return _driver->sndDrivSetVol(volume);
+
+ return 0;
+}
+
+int SmushSound::souEngineGetHVol() {
+ if (_driver)
+ return _driver->sndDrivGetVol();
+
+ return -1;
+}
+
+int SmushSound::souEngineSetRate(int rate) {
+ _sampleRate = rate;
+ if (_driver)
+ return _driver->sndDrivDummy(rate);
+
+ return rate;
+}
+
+int SmushSound::souEngineGetRate() {
+ if (_driver)
+ return _driver->sndDrivGetRate();
+
+ return -1;
+}
+
+int SmushSound::souEngineQuery(int soundId) {
+ if (soundId == SOU_FREE_ID) {
+ for (int i = 0; i < _channelCount; i++) {
+ if (_channels[i].state)
+ return _channels[i].consumedPosition + 1;
+ }
+
+ return 0;
+ }
+
+ if (soundId >= 0 || -1 - soundId >= _channelCount) {
+ for (int i = 0; i < _channelCount; i++) {
+ if (_channels[i].soundId == soundId) {
+ if (_channels[i].state)
+ return _channels[i].consumedPosition + 1;
+
+ break;
+ }
+ }
+ } else {
+ SmushSoundChannel *chan = &_channels[-1 - soundId];
+ if (chan->state)
+ return chan->consumedPosition + 1;
+ }
+
+ return 0;
+}
+
+int SmushSound::souEngineQueryTime(int channelIndex) {
+ int samples = _channels[channelIndex].consumedPosition;
+
+ switch (_channels[channelIndex].format) {
+ case 1:
+ case 2:
+ case 4:
+ samples >>= 1;
+ break;
+ case 3:
+ case 5:
+ samples >>= 2;
+ break;
+ default:
+ break;
+ }
+
+ return (int)(int64)((double)samples / ((double)souEngineGetRate() * 0.001));
+}
+
+int SmushSound::souEngineSetParam(int soundId, int param, int value1, int value2) {
+ switch (param) {
+ case 1:
+ setGroupVolume(value1, value2);
+ return 0;
+ case 2:
+ if (value1)
+ _duckFloor = value1;
+ if (!value2)
+ return 0;
+
+ _duckRate = value2;
+ return 0;
+ case 3:
+ _musicParam = value1;
+ return 0;
+ case 4:
+ _voiceParam = value1;
+ return 0;
+ case 5:
+ _normalParam = value1;
+ return 0;
+ default:
+ return -1;
+ }
+}
+
+int SmushSound::souEngineGetParam(int soundId, int param, int *value1, int *value2) {
+ switch (param) {
+ case 1:
+ // The disasm passes the group index in the pointer slot
+ if (value1)
+ *value1 = getGroupVolume((uint8)(intptr)value2);
+ return 0;
+ case 2:
+ if (value1)
+ *value1 = _duckFloor;
+ if (value2)
+ *value2 = _duckRate;
+ return 0;
+ case 3:
+ if (value1)
+ *value1 = _musicParam;
+ return 0;
+ case 4:
+ if (value1)
+ *value1 = _voiceParam;
+ return 0;
+ case 5:
+ if (value1)
+ *value1 = _normalParam;
+ return 0;
+ default:
+ return -1;
+ }
+}
+
+uint8 SmushSound::getGroupVolume(uint8 index) {
+ if (index == 0xFF)
+ return _masterVolume;
+
+ return _groupVolumes[index];
+}
+
+void SmushSound::setGroupVolume(uint8 index, uint8 volume) {
+ if (index == 0xFF)
+ _masterVolume = volume;
+
+ _groupVolumes[index] = volume;
+}
+
+int SmushSound::souEngineMarkSyncTime() {
+ _syncMark = _timer->sysGetTime();
+ return _syncMark;
+}
+
+int SmushSound::souEngineExternalSync(double ticks) {
+ _syncAccumulator += ticks;
+
+ if (_syncMark) {
+ int now = _timer->sysGetTime();
+ if (now > _syncMark) {
+ int elapsed = now - _syncMark;
+ if (elapsed / 5 < 1193180) {
+ _syncMark = 0;
+ _syncAccumulator -= (double)elapsed;
+ }
+ }
+ }
+
+ _timer->sysExtSync((int)(int64)_syncAccumulator);
+ int result = (int)(int64)_syncAccumulator;
+ _syncAccumulator -= (double)(int)(int64)_syncAccumulator;
+ return result;
+}
+
+int SmushSound::souEngineStartCreative(const byte *data, int size, int soundId, int volume, int pan, int soundClass) {
+ int largest = 0;
+ for (int i = _channelCount - 1; i >= 0; i--) {
+ if (_channels[i].capacity > largest)
+ largest = _channels[i].capacity;
+ }
+
+ int bestDistance = 10000000;
+ for (int i = _channelCount - 1; i >= 0; i--) {
+ int capacity = _channels[i].capacity;
+ int distance = capacity - size;
+ if (distance < 0)
+ distance = size - capacity;
+
+ if (distance < bestDistance && size < capacity) {
+ bestDistance = distance;
+ largest = capacity;
+ }
+ }
+
+ int scores[SOU_MAX_CHANNELS] = {};
+ for (int i = _channelCount - 1; i >= 0; i--) {
+ SmushSoundChannel *chan = &_channels[i];
+ scores[i] = 0;
+
+ if (chan->capacity != largest)
+ continue;
+
+ if (chan->state && chan->state != 3 && chan->soundClass > soundClass)
+ continue;
+
+ if (!chan->state)
+ scores[i] = 4096;
+ if (chan->soundClass < soundClass)
+ scores[i] += 2048;
+ if (chan->soundClass == soundClass)
+ scores[i] += 1024;
+ if (chan->state == 3)
+ scores[i] += 512;
+ scores[i] += chan->age + 1;
+ }
+
+ int best = -1;
+ int bestScore = 0;
+ for (int i = _channelCount - 1; i >= 0; i--) {
+ if (scores[i] > bestScore) {
+ best = i;
+ bestScore = scores[i];
+ }
+ }
+
+ if (best < 0)
+ return 0;
+
+ for (int i = _channelCount - 1; i >= 0; i--) {
+ if (_channels[i].age < 256)
+ _channels[i].age++;
+ }
+
+ SmushSoundChannel *chan = &_channels[best];
+ chan->age = 0;
+ chan->state = 0;
+
+ byte *buffer = chan->dataBuffer;
+ buffer[0] = 1;
+ buffer[1] = 8;
+ buffer[2] = 0;
+ buffer[3] = 0;
+ buffer[4] = 0;
+ buffer[5] = 0;
+ buffer[6] = size >> 24;
+ buffer[7] = size >> 16;
+ buffer[8] = size >> 8;
+ buffer[9] = size;
+ buffer[10] = 0;
+ buffer[11] = 0;
+ memcpy(buffer + 12, data, size);
+
+ chan->scriptPointer = buffer;
+ chan->dataArea = buffer + 12;
+ chan->dataAreaSize = chan->capacity - 12;
+ chan->writePosition = size;
+ chan->soundId = soundId;
+ chan->volume = volume;
+ chan->pan = pan;
+ chan->soundClass = soundClass;
+ chan->consumedPosition = 0;
+ chan->state = 2;
+
+ return pan;
+}
+
+int SmushSound::souEngineStartStream(const byte *data, int size, int soundId, uint32 volume, int pan, int soundClass, int tag1, int chunkCounter, int tag2, int copyFlag) {
+ if (chunkCounter) {
+ int index = _channelCount - 1;
+ if (index < 0)
+ return index;
+
+ while (!_channels[index].state || tag1 != _channels[index].tag1 || chunkCounter != _channels[index].chunkCounter + 1 || tag2 != _channels[index].tag2) {
+ index--;
+ if (index < 0)
+ return index;
+ }
+
+ SmushSoundChannel *chan = &_channels[index];
+ chan->chunkCounter++;
+
+ int position = chan->writePosition % chan->dataAreaSize;
+ if (position + size <= chan->dataAreaSize) {
+ memcpy(chan->dataArea + position, data, size);
+ } else {
+ memcpy(chan->dataArea + position, data, chan->dataAreaSize - position);
+ memcpy(chan->dataArea, data + (chan->dataAreaSize - position), position + size - chan->dataAreaSize);
+ }
+
+ if (volume <= 0x7E)
+ chan->volume = volume;
+ if (pan > -128 && pan < 128)
+ chan->pan = pan;
+
+ chan->writePosition += size;
+ return pan;
+ }
+
+ int streamSize = chunkCounter;
+ if (tag2 != 1 || copyFlag)
+ streamSize = READ_BE_UINT32(data + 4);
+
+ int largest = 0;
+ for (int i = _channelCount - 1; i >= 0; i--) {
+ if (_channels[i].capacity > largest)
+ largest = _channels[i].capacity;
+ }
+
+ int bestDistance = 10000000;
+ if ((soundClass & 0xC0) == 0x40) {
+ if (tag2 > 1)
+ souEngineMarkSyncTime();
+ } else {
+ for (int i = _channelCount - 1; i >= 0; i--) {
+ int capacity = _channels[i].capacity;
+ int distance = capacity - streamSize;
+ if (distance < 0)
+ distance = streamSize - capacity;
+
+ if (distance < bestDistance && capacity > 2 * streamSize / 3) {
+ int needed = (tag2 <= 1) ? size : 2 * size;
+ if (capacity > needed) {
+ bestDistance = distance;
+ largest = capacity;
+ }
+ }
+ }
+ }
+
+ int scores[SOU_MAX_CHANNELS] = {};
+ for (int i = _channelCount - 1; i >= 0; i--) {
+ SmushSoundChannel *chan = &_channels[i];
+ scores[i] = 0;
+
+ if (chan->capacity != largest)
+ continue;
+
+ if (chan->state && chan->state != 3 && chan->soundClass > soundClass)
+ continue;
+
+ if (!chan->state)
+ scores[i] = 4096;
+ if (chan->soundClass < soundClass)
+ scores[i] += 2048;
+ if (chan->soundClass == soundClass)
+ scores[i] += 1024;
+ if (chan->state == 3)
+ scores[i] += 512;
+ scores[i] += chan->age + 1;
+ }
+
+ int best = -1;
+ int bestScore = -1;
+ for (int i = _channelCount - 1; i >= 0; i--) {
+ if (scores[i] && scores[i] > bestScore) {
+ best = i;
+ bestScore = scores[i];
+ }
+ }
+
+ if (best < 0)
+ return best;
+
+ for (int i = _channelCount - 1; i >= 0; i--) {
+ if (_channels[i].age < 255)
+ _channels[i].age++;
+ }
+
+ SmushSoundChannel *chan = &_channels[best];
+ chan->tag1 = tag1;
+ chan->tag2 = tag2;
+ chan->chunkCounter = 0;
+ chan->age = 0;
+ chan->state = 0;
+
+ int headerSize = READ_BE_UINT32(data + 12);
+ int totalSize = READ_BE_UINT32(data + headerSize + 20);
+
+ int copiedSize;
+ int bufferSize;
+ if (tag2 != 1 || copyFlag) {
+ int copySize = chan->capacity;
+ copiedSize = size;
+ if (size < copySize)
+ copySize = size;
+
+ memcpy(chan->dataBuffer, data, copySize);
+ chan->scriptPointer = chan->dataBuffer + 16;
+ bufferSize = chan->capacity;
+ } else {
+ copiedSize = size;
+ chan->scriptPointer = (byte *)data + 16;
+ bufferSize = size;
+ }
+
+ chan->dataAreaSize = bufferSize - headerSize - 24;
+ chan->dataArea = chan->scriptPointer + headerSize + 8;
+ chan->writePosition = copiedSize - headerSize - 24;
+ chan->totalSize = totalSize;
+ chan->soundId = soundId;
+ chan->volume = 127;
+ chan->pan = 0;
+ if (volume < 0x80)
+ chan->volume = volume;
+ if (pan > -128 && pan < 128)
+ chan->pan = pan;
+
+ chan->soundClass = soundClass;
+ chan->consumedPosition = 0;
+ chan->state = 2;
+
+ uint32 formatTag = READ_BE_UINT32(chan->dataArea - 8);
+ if (formatTag == MKTAG('S', '6', 'S', 'Z')) {
+ chan->format = 5;
+ } else if (formatTag == MKTAG('S', '6', 'M', 'Z')) {
+ chan->format = 4;
+ } else if (formatTag == MKTAG('S', 'D', '2', 'S')) {
+ chan->format = 1;
+ } else if (formatTag > MKTAG('S', 'D', '2', 'S')) {
+ if (formatTag == MKTAG('S', 'D', '6', 'M'))
+ chan->format = 2;
+ else if (formatTag == MKTAG('S', 'D', '6', 'M') + 6)
+ chan->format = 3;
+ else if (formatTag == MKTAG('S', 'D', '6', 'M') + 518)
+ chan->format = 1;
+ else
+ chan->format = 0;
+ } else {
+ chan->format = 0;
+ }
+
+ return formatTag;
+}
+
+void SmushSound::souEngineFill(int frameCount, SmushDriver *driver) {
+ _fillCounter++;
+ souEngineExternalSync((double)frameCount * 1193180.0 / (double)_sampleRate);
+
+ if (_pauseCount)
+ return;
+
+ if (_resetFlag) {
+ _resetFlag = 0;
+ for (int i = 0; i < SOU_MAX_CHANNELS; i++) {
+ _playback[i].tailBytes = 0;
+ _playback[i].loopRemaining = 0;
+ }
+ }
+
+ int voicePlaying = 0;
+
+ for (int i = 0; i < _channelCount; i++) {
+ SmushSoundChannel *chan = &_channels[i];
+ SmushSoundPlayback *rt = &_playback[i];
+
+ int soundClass = chan->soundClass & 0xC0;
+ bool enabled = (soundClass == 128 && _voiceParam) || (soundClass == 64 && _musicParam) || (!soundClass && _normalParam);
+ int scaledVolume = chan->volume * _masterVolume;
+ int volume = scaledVolume / 127;
+ if (soundClass == 64 && _voiceParam)
+ volume = (_duckLevel * (scaledVolume / 127)) >> 8;
+
+ if (chan->state == 2 && rt->previousState == 1) {
+ int position = rt->dataSize ? rt->readPosition % rt->dataSize : 0;
+ rt->tailBytes = 6144;
+ rt->previousVolume = chan->volume;
+ rt->previousFormat = chan->format;
+ rt->previousRate = rt->rate;
+ memset(chan->stagingBuffer, 128, 6144);
+
+ int copySize = rt->dataSize - position;
+ if (copySize > 6144)
+ copySize = 6144;
+
+ memcpy(chan->stagingBuffer, rt->dataStart + position, copySize);
+ rt->gain = 0;
+ } else if (chan->state == 1) {
+ int writePosition = chan->writePosition;
+ if (rt->readPosition < writePosition - chan->dataAreaSize + 15000) {
+ int totalSize = chan->totalSize;
+ if (writePosition < totalSize) {
+ rt->gain = 0;
+ int available = writePosition - rt->readPosition - 15000;
+ if (available > rt->remaining)
+ available = rt->remaining;
+
+ int limit = totalSize - rt->dataSize;
+ if (available + rt->readPosition > limit)
+ available = limit - rt->readPosition;
+
+ if (available > 0) {
+ rt->tailBytes = 6144;
+ rt->previousRate = rt->rate;
+ memcpy(chan->stagingBuffer, rt->dataStart + (rt->dataSize ? rt->readPosition % rt->dataSize : 0), 6144);
+ rt->readPosition += available;
+ rt->remaining -= available;
+ }
+ }
+ }
+ }
+
+ int tailFrameBytes = _bytesPerFrame[rt->previousFormat];
+ if (rt->tailBytes > tailFrameBytes) {
+ int destOffset = 0;
+ int wanted = rt->previousRate * tailFrameBytes * frameCount / _sampleRate;
+ int chunkBytes = rt->tailBytes;
+ if (chunkBytes >= wanted)
+ chunkBytes = wanted;
+
+ chunkBytes -= chunkBytes % 4;
+ int remainingBytes = chunkBytes;
+ if (chunkBytes >= 4) {
+ while (true) {
+ if (chunkBytes > 1024)
+ chunkBytes = 1024;
+
+ int chunkFrames;
+ if (chunkBytes == wanted)
+ chunkFrames = frameCount;
+ else
+ chunkFrames = _sampleRate * (chunkBytes / _bytesPerFrame[rt->previousFormat]) / rt->previousRate;
+
+ if (enabled) {
+ int pan = chan->pan;
+ if (_panInvert)
+ pan = -pan;
+
+ driver->sndDrivMix(chan->stagingBuffer - rt->tailBytes + 6144, destOffset, chunkFrames, chunkBytes, 1,
+ rt->previousVolume * rt->tailBytes * _masterVolume / 780288, pan, rt->previousFormat);
+ }
+
+ destOffset += chunkFrames;
+ bool done = remainingBytes - chunkBytes < 4;
+ remainingBytes -= chunkBytes;
+ rt->tailBytes -= chunkBytes;
+ if (done)
+ break;
+
+ chunkBytes = remainingBytes;
+ }
+ }
+ }
+
+ if (chan->state == 2) {
+ if (rt->previousState != 1)
+ rt->gain = 16;
+
+ rt->script = chan->scriptPointer;
+ rt->dataStart = chan->dataArea;
+ rt->dataSize = chan->dataAreaSize;
+ rt->format = chan->format;
+ rt->remaining = 0;
+ rt->loopRemaining = 0;
+ chan->state = 1;
+ }
+
+ if (chan->state) {
+ int destOffset = 0;
+ int framesLeft = frameCount;
+
+ if (frameCount > 0) {
+ while (true) {
+ if (rt->remaining > 0) {
+ int ringPosition = rt->dataSize ? rt->readPosition % rt->dataSize : 0;
+ int wanted = rt->rate * framesLeft * _bytesPerFrame[rt->format] / _sampleRate;
+ int chunkBytes = rt->remaining;
+ if (chunkBytes >= wanted)
+ chunkBytes = wanted;
+
+ if (ringPosition + chunkBytes > rt->dataSize)
+ chunkBytes = rt->dataSize - ringPosition;
+
+ int gain = rt->gain;
+ if (chunkBytes + rt->readPosition <= chan->writePosition) {
+ if (gain < 16)
+ rt->gain = gain + 1;
+ if (chunkBytes > 1024)
+ chunkBytes = 1024;
+
+ chan->state = 1;
+ if ((chan->soundClass & 0xC0) == 0x80)
+ voicePlaying = 1;
+ } else {
+ if (gain)
+ rt->gain = gain - 1;
+
+ chan->state = 3;
+ if (chunkBytes > 1024)
+ chunkBytes = 1024;
+
+ rt->readPosition -= chunkBytes;
+ rt->remaining += chunkBytes;
+ ringPosition = rt->dataSize ? rt->readPosition % rt->dataSize : 0;
+ }
+
+ int chunkFrames;
+ if (chunkBytes == wanted)
+ chunkFrames = framesLeft;
+ else
+ chunkFrames = _sampleRate * (chunkBytes / _bytesPerFrame[rt->format]) / rt->rate;
+
+ if (enabled) {
+ int pan = chan->pan;
+ if (_panInvert)
+ pan = -pan;
+
+ driver->sndDrivMix(rt->dataStart + ringPosition, destOffset, chunkFrames, chunkBytes, 1,
+ (rt->gain * volume) >> 4, pan, rt->format);
+ }
+
+ rt->remaining -= chunkBytes;
+ rt->readPosition += chunkBytes;
+ framesLeft -= chunkFrames;
+ destOffset += chunkFrames;
+ }
+
+ if (rt->remaining <= 0) {
+ bool backToMix = false;
+
+ while (framesLeft) {
+ byte *op = rt->script;
+
+ switch (op[0]) {
+ case 1: {
+ rt->loopRemaining = 0;
+ int dataAreaSize = chan->dataAreaSize;
+ rt->readPosition = READ_BE_UINT32(op + 2);
+ rt->remaining = READ_BE_UINT32(op + 6);
+ rt->rate = _sampleRate;
+ rt->script += op[1] + 2;
+
+ int writePosition = chan->writePosition;
+ int slack = (writePosition >= chan->totalSize) ? 0 : 15000;
+ if (rt->readPosition < writePosition + slack - dataAreaSize) {
+ int skip = writePosition - rt->readPosition - dataAreaSize + 15000;
+ if (skip <= rt->remaining) {
+ rt->readPosition += skip;
+ rt->remaining -= skip;
+ } else {
+ chan->state = 0;
+ chan->soundId = SOU_FREE_ID;
+ framesLeft = 0;
+ }
+ }
+ break;
+ }
+ case 2:
+ case 8:
+ case 9:
+ case 10:
+ case 11: {
+ uint8 index = op[4];
+ uint8 value;
+ switch (index) {
+ case 0xFF:
+ value = _masterVolume;
+ break;
+ case 0xFE:
+ value = chan->volume;
+ break;
+ case 0xFD:
+ value = chan->pan;
+ break;
+ default:
+ value = _groupVolumes[index];
+ break;
+ }
+
+ switch (op[0]) {
+ case 2:
+ if (value || !index)
+ value = 1;
+ break;
+ case 8:
+ value = op[5] < value;
+ break;
+ case 9:
+ value = value < op[5];
+ break;
+ case 10:
+ value = value == op[5];
+ break;
+ case 11:
+ value = value != op[5];
+ break;
+ default:
+ break;
+ }
+
+ if (!value)
+ rt->script = op + op[1] + 2;
+ else
+ rt->script = op + READ_BE_UINT16(op + 2);
+ break;
+ }
+ case 3: {
+ int8 variable = op[2];
+ if (variable == -1) {
+ _masterVolume = op[3];
+ rt->script = op + op[1] + 2;
+ } else if (variable == -2) {
+ int newVolume = (int8)op[3];
+ int scaled = newVolume * _masterVolume;
+ chan->volume = newVolume;
+ volume = scaled / 127;
+ if ((chan->soundClass & 0xC0) == 0x40 && _voiceParam)
+ volume = (_duckLevel * (scaled / 127)) >> 8;
+
+ rt->script = op + op[1] + 2;
+ } else {
+ if (variable == -3)
+ chan->pan = op[3];
+ else
+ _groupVolumes[op[2]] = op[3];
+
+ rt->script = op + op[1] + 2;
+ }
+ break;
+ }
+ case 4: {
+ int8 variable = op[2];
+ if (variable == -1)
+ _masterVolume += op[3];
+ else if (variable == -2)
+ chan->volume += op[3];
+ else if (variable == -3)
+ chan->pan += op[3];
+ else
+ _groupVolumes[op[2]] += op[3];
+
+ rt->script = op + op[1] + 2;
+ break;
+ }
+ case 6:
+ rt->loopRemaining = 0;
+ rt->readPosition = READ_BE_UINT32(op + 2);
+ rt->remaining = READ_BE_UINT32(op + 6);
+ rt->rate = READ_BE_UINT32(op + 10);
+ rt->script = op + op[1] + 2;
+ break;
+ case 7: {
+ if (!rt->loopRemaining) {
+ rt->loopRemaining = READ_BE_UINT32(op + 6);
+ rt->repeatAccumulator = 0;
+ }
+
+ rt->readPosition = rt->repeatAccumulator + READ_BE_UINT32(op + 2);
+ uint32 segmentSize = READ_BE_UINT32(op + 14);
+ if ((uint32)rt->loopRemaining < segmentSize)
+ segmentSize = rt->loopRemaining;
+
+ rt->remaining = segmentSize;
+ rt->rate = READ_BE_UINT32(op + 10);
+ rt->loopRemaining -= rt->remaining;
+ rt->repeatAccumulator += rt->remaining;
+ if (rt->loopRemaining)
+ rt->script = op + op[1] + 2;
+ else
+ rt->script = op + READ_BE_UINT16(op + 18);
+
+ int writePosition = chan->writePosition;
+ int dataAreaSize = chan->dataAreaSize;
+ int slack = (writePosition >= chan->totalSize) ? 0 : 15000;
+ if (rt->readPosition < writePosition + slack - dataAreaSize) {
+ int skip = writePosition - rt->readPosition - dataAreaSize + 15000;
+ if (skip <= rt->remaining) {
+ rt->readPosition += skip;
+ rt->remaining -= skip;
+ } else {
+ chan->state = 0;
+ chan->soundId = SOU_FREE_ID;
+ framesLeft = 0;
+ }
+ }
+ break;
+ }
+ default:
+ chan->state = 0;
+ chan->soundId = SOU_FREE_ID;
+ framesLeft = 0;
+ break;
+ }
+
+ if (rt->remaining > 0) {
+ backToMix = true;
+ break;
+ }
+ }
+
+ if (!backToMix)
+ break;
+ }
+
+ if (framesLeft <= 0)
+ break;
+ }
+ }
+ }
+
+ chan->consumedPosition = rt->readPosition;
+ rt->previousState = chan->state;
+ }
+
+ if (voicePlaying) {
+ if (_duckLevel > _duckFloor) {
+ _duckLevel -= (_duckRate * frameCount) >> 13;
+ if (_duckLevel < _duckFloor)
+ _duckLevel = _duckFloor;
+ }
+ } else if (_duckLevel < 256) {
+ _duckLevel += (_duckRate * frameCount) >> 15;
+ if (_duckLevel > 256)
+ _duckLevel = 256;
+ }
+}
+
+} // End of namespace Grim
diff --git a/engines/grim/movie/smush_sound.h b/engines/grim/movie/smush_sound.h
new file mode 100644
index 00000000000..ffcf93e77c8
--- /dev/null
+++ b/engines/grim/movie/smush_sound.h
@@ -0,0 +1,143 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifndef GRIM_MOVIE_SMUSH_SOUND_H
+#define GRIM_MOVIE_SMUSH_SOUND_H
+
+#include "common/scummsys.h"
+
+namespace Grim {
+
+#define SOU_MAX_CHANNELS 16
+#define SOU_FREE_ID -255
+
+class SmushTimer;
+class SmushDriver;
+
+struct SmushSoundChannel {
+ byte *dataBuffer;
+ byte *stagingBuffer;
+ byte *scriptPointer;
+ byte *dataArea;
+ int capacity;
+ uint8 volume;
+ int8 pan;
+ uint8 state;
+ uint8 format;
+ int soundClass;
+ int soundId;
+ int age;
+ int dataAreaSize;
+ int writePosition;
+ int consumedPosition;
+ int totalSize;
+ int tag1;
+ int chunkCounter;
+ int tag2;
+};
+
+struct SmushSoundPlayback {
+ byte *script;
+ byte *dataStart;
+ int dataSize;
+ int readPosition;
+ int remaining;
+ int rate;
+ uint8 format;
+ int previousState;
+ int previousRate;
+ int previousVolume;
+ int tailBytes;
+ uint8 previousFormat;
+ int gain;
+ int repeatAccumulator;
+ int loopRemaining;
+};
+
+class SmushSound {
+
+private:
+ SmushTimer *_timer;
+ SmushDriver *_driver;
+ SmushSoundChannel _channels[SOU_MAX_CHANNELS];
+ SmushSoundPlayback _playback[SOU_MAX_CHANNELS];
+ int _channelCount;
+ int _masterVolume;
+ int _sampleRate;
+ int _duckFloor;
+ int _duckRate;
+ int _duckLevel;
+ int _resetFlag;
+ int _bytesPerFrame[6];
+ uint8 _groupVolumes[256];
+ int _pauseCount;
+ int _engineOk;
+ int _syncMark;
+ double _syncAccumulator;
+ int _fillCounter;
+ int _musicParam;
+ int _voiceParam;
+ int _normalParam;
+ int _panInvert;
+ int _driverRate;
+ int _reserveRam;
+ int _bufferSizes[SOU_MAX_CHANNELS + 1];
+
+ int engineInitialize(int rate, int reserveRam, const int *bufferSizes);
+ int engineTerminate();
+ int engineAllocChannel(int size, int reserveRam);
+ uint8 getGroupVolume(uint8 index);
+ void setGroupVolume(uint8 index, uint8 volume);
+ int souEngineMarkSyncTime();
+ int souEngineExternalSync(double ticks);
+ int souEngineStartCreative(const byte *data, int size, int soundId, int volume, int pan, int soundClass);
+
+public:
+ SmushSound(SmushTimer *timer, SmushDriver *driver);
+ ~SmushSound();
+
+ int moduleStartup();
+ int moduleShutdown();
+
+ int souEngineStart(const byte *data, int soundId, int volume, int pan, int soundClass);
+ void souEngineStop(int soundId);
+ void souEnginePause();
+ int souEngineResume();
+ void souEngineStopAll();
+ void souEngineSetVol(int soundId, int volume);
+ int souEngineGetVol(int soundId);
+ void souEngineSetPan(int soundId, int pan);
+ int souEngineGetPan(int soundId);
+ int souEngineSetHVol(int volume);
+ int souEngineGetHVol();
+ int souEngineSetRate(int rate);
+ int souEngineGetRate();
+ int souEngineQuery(int soundId);
+ int souEngineQueryTime(int channelIndex);
+ int souEngineSetParam(int soundId, int param, int value1, int value2);
+ int souEngineGetParam(int soundId, int param, int *value1, int *value2);
+ int souEngineStartStream(const byte *data, int size, int soundId, uint32 volume, int pan, int soundClass, int tag1, int chunkCounter, int tag2, int copyFlag);
+ void souEngineFill(int frameCount, SmushDriver *driver);
+
+};
+
+} // End of namespace Grim
+#endif
diff --git a/engines/grim/movie/smush_timer.cpp b/engines/grim/movie/smush_timer.cpp
new file mode 100644
index 00000000000..c480220c7d2
--- /dev/null
+++ b/engines/grim/movie/smush_timer.cpp
@@ -0,0 +1,270 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include "common/system.h"
+
+#include "engines/grim/movie/smush_timer.h"
+
+namespace Grim {
+
+SmushTimer::SmushTimer() {
+ memset(_timers, 0, sizeof(_timers));
+ _sysGlobalTimer = 0;
+ _sysTickIncrement = 0;
+ _timerThreadLastTime = 0;
+ _sysInitCount = 0;
+ _sysDisableFlag = 0;
+ _sysAbortFlag = 0;
+ _sysQueueFlag = 0;
+ _sysTickBusy = 0;
+ _sysDispatcherDisable = 0;
+ _timerHandlerCoreIsReentrant = 0;
+ _dispatcherAbortRequest = 0;
+ _dispatcherActive = 0;
+ _dispatcherSkip = 0;
+ _dispatcherLast = 0;
+ _dispatcherSaved = 0;
+}
+
+SmushTimer::~SmushTimer() {
+}
+
+int SmushTimer::moduleStartup() {
+ if (_sysInitCount++ == 0) {
+ for (int i = 1; i <= SYS_MAX_TIMERS; i++)
+ _timers[i].callback = nullptr;
+
+ _sysTickIncrement = 19886;
+ _timerThreadLastTime = g_system->getMillis();
+ }
+
+ return 0;
+}
+
+void SmushTimer::moduleShutdown() {
+ _sysInitCount--;
+}
+
+void SmushTimer::timerThread() {
+ if (!_sysInitCount)
+ return;
+
+ // The host timer exists only after the first movie, don't replay the idle time as ticks
+ uint32 now = g_system->getMillis();
+ if (now - _timerThreadLastTime > 1000)
+ _timerThreadLastTime = now;
+
+ int elapsed = now - _timerThreadLastTime;
+ if (elapsed > 16) {
+ _timerThreadLastTime += 16;
+ timerTick();
+ }
+}
+
+void SmushTimer::timerTick() {
+ _sysGlobalTimer += _sysTickIncrement;
+ if (_sysDisableFlag)
+ return;
+
+ _sysTickBusy = 1;
+ _sysQueueFlag = _sysAbortFlag;
+ _sysAbortFlag = 0;
+
+ for (int i = 1; i <= SYS_MAX_TIMERS; i++) {
+ SmushTimerSlot *slot = &_timers[i];
+ if (!slot->callback)
+ continue;
+
+ slot->accumulator += _sysTickIncrement;
+ slot->extSync += _sysTickIncrement;
+
+ if ((slot->flags & 5) != 1 || !slot->period || slot->accumulator < slot->period)
+ continue;
+
+ while (true) {
+ slot->accumulator -= slot->period;
+ if (slot->flags & SYS_TIMER_ONE_SHOT) {
+ slot->period = 0;
+ slot->callback(i, slot->context);
+ break;
+ }
+
+ slot->callback(i, slot->context);
+ if (slot->accumulator < slot->period)
+ break;
+ }
+ }
+
+ for (int i = 1; i <= SYS_MAX_TIMERS; i++) {
+ SmushTimerSlot *slot = &_timers[i];
+ if (!slot->callback || (slot->flags & 5) != 0 || !slot->period || slot->reentrant)
+ continue;
+
+ slot->reentrant = 1;
+ if (slot->accumulator >= slot->period) {
+ while (true) {
+ slot->accumulator -= slot->period;
+ if (slot->flags & SYS_TIMER_ONE_SHOT) {
+ slot->period = 0;
+ slot->callback(i, slot->context);
+ break;
+ }
+
+ slot->callback(i, slot->context);
+ if (slot->accumulator < slot->period)
+ break;
+ }
+ }
+ slot->reentrant = 0;
+ }
+
+ _sysAbortFlag = _sysQueueFlag;
+ _sysTickBusy = 0;
+
+ if (!_timerHandlerCoreIsReentrant && !_sysDispatcherDisable) {
+ _timerHandlerCoreIsReentrant = 1;
+ sysTimerDispatcher();
+ _timerHandlerCoreIsReentrant = 0;
+ }
+}
+
+void SmushTimer::sysTimerDispatcher() {
+ if (_dispatcherSkip) {
+ _dispatcherSkip--;
+ if (_dispatcherSkip)
+ return;
+ }
+
+ if (_dispatcherAbortRequest) {
+ _dispatcherAbortRequest = 0;
+ _dispatcherActive = 1;
+ return;
+ }
+
+ _dispatcherAbortRequest = 0;
+ _dispatcherActive = 1;
+ sysTimerDispatcherLoop();
+ _dispatcherActive = 0;
+ _dispatcherLast = _dispatcherSaved;
+
+ _sysAbortFlag = 0;
+}
+
+void SmushTimer::sysTimerDispatcherLoop() {
+ for (int i = 1; i <= SYS_MAX_TIMERS; i++) {
+ SmushTimerSlot *slot = &_timers[i];
+ slot->busy = 1;
+
+ if (slot->callback && (slot->flags & 5) == 4 && slot->period && slot->accumulator >= slot->period) {
+ while (true) {
+ slot->accumulator -= slot->period;
+ if (slot->flags & SYS_TIMER_ONE_SHOT) {
+ slot->period = 0;
+ slot->callback(i, slot->context);
+ break;
+ }
+
+ slot->callback(i, slot->context);
+ if (slot->accumulator < slot->period)
+ break;
+ }
+ }
+
+ slot->busy = 0;
+ }
+}
+
+int SmushTimer::sysAlloc(SmushTimerCallback callback, int period, int flags, void *context) {
+ int i = 1;
+ while (_timers[i].callback) {
+ i++;
+ if (i > SYS_MAX_TIMERS)
+ return 0;
+ }
+
+ SmushTimerSlot *slot = &_timers[i];
+ slot->reentrant = 0;
+ slot->accumulator = 0;
+ slot->context = context;
+ slot->flags = flags;
+ slot->period = period;
+ slot->callback = callback;
+ slot->busy = 0;
+ slot->extSync = 0;
+
+ return i;
+}
+
+void SmushTimer::sysFree(int timer) {
+ if (timer > 0 && timer <= SYS_MAX_TIMERS)
+ _timers[timer].callback = nullptr;
+}
+
+void SmushTimer::sysDisable() {
+ _sysDisableFlag++;
+}
+
+void SmushTimer::sysEnable() {
+ _sysDisableFlag--;
+}
+
+void SmushTimer::sysReset(int timer, int period) {
+ if (timer > 0 && timer <= SYS_MAX_TIMERS) {
+ _timers[timer].period = period;
+ _timers[timer].accumulator = 0;
+ }
+}
+
+void SmushTimer::sysDelay(int ticks) {
+ int start = _sysGlobalTimer;
+ int end = ticks + _sysGlobalTimer;
+ int now = _sysGlobalTimer;
+
+ do {
+ if (now >= end)
+ break;
+
+ sysGetTime();
+ now = _sysGlobalTimer;
+ } while (_sysGlobalTimer >= start);
+}
+
+int SmushTimer::sysGetTime() {
+ return _sysGlobalTimer;
+}
+
+int SmushTimer::sysGetCount() {
+ return _sysTickIncrement;
+}
+
+void SmushTimer::sysExtSync(int value) {
+ for (int i = 1; i <= SYS_MAX_TIMERS; i++) {
+ SmushTimerSlot *slot = &_timers[i];
+ if (slot->callback && (slot->flags & SYS_TIMER_EXT_SYNC)) {
+ if (value > 0)
+ slot->accumulator += value - slot->extSync;
+
+ slot->extSync = 0;
+ }
+ }
+}
+
+} // End of namespace Grim
diff --git a/engines/grim/movie/smush_timer.h b/engines/grim/movie/smush_timer.h
new file mode 100644
index 00000000000..78f17197965
--- /dev/null
+++ b/engines/grim/movie/smush_timer.h
@@ -0,0 +1,92 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifndef GRIM_MOVIE_SMUSH_TIMER_H
+#define GRIM_MOVIE_SMUSH_TIMER_H
+
+#include "common/scummsys.h"
+
+namespace Grim {
+
+#define SYS_MAX_TIMERS 19
+
+#define SYS_TIMER_ONE_SHOT 2
+#define SYS_TIMER_EXT_SYNC 8
+
+typedef void (*SmushTimerCallback)(int timer, void *context);
+
+struct SmushTimerSlot {
+ SmushTimerCallback callback;
+ int period;
+ int flags;
+ void *context;
+ int reentrant;
+ int accumulator;
+ int busy;
+ int extSync;
+};
+
+class SmushTimer {
+
+private:
+ SmushTimerSlot _timers[SYS_MAX_TIMERS + 1];
+ int _sysGlobalTimer;
+ int _sysTickIncrement;
+ uint32 _timerThreadLastTime;
+ int _sysInitCount;
+ int _sysDisableFlag;
+ int _sysAbortFlag;
+ int _sysQueueFlag;
+ int _sysTickBusy;
+ int _sysDispatcherDisable;
+ int _timerHandlerCoreIsReentrant;
+ int _dispatcherAbortRequest;
+ int _dispatcherActive;
+ int _dispatcherSkip;
+ int _dispatcherLast;
+ int _dispatcherSaved;
+
+ void timerTick();
+ void sysTimerDispatcher();
+ void sysTimerDispatcherLoop();
+
+public:
+ SmushTimer();
+ ~SmushTimer();
+
+ int moduleStartup();
+ void moduleShutdown();
+ void timerThread();
+
+ int sysAlloc(SmushTimerCallback callback, int period, int flags, void *context);
+ void sysFree(int timer);
+ void sysDisable();
+ void sysEnable();
+ void sysReset(int timer, int period);
+ void sysDelay(int ticks);
+ int sysGetTime();
+ int sysGetCount();
+ void sysExtSync(int value);
+
+};
+
+} // End of namespace Grim
+#endif
diff --git a/engines/grim/movie/smush_wave.cpp b/engines/grim/movie/smush_wave.cpp
new file mode 100644
index 00000000000..856da7ef005
--- /dev/null
+++ b/engines/grim/movie/smush_wave.cpp
@@ -0,0 +1,162 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include "common/scummsys.h"
+#include "common/endian.h"
+
+#include "engines/grim/movie/codecs/vima.h"
+#include "engines/grim/movie/smush_sound.h"
+#include "engines/grim/movie/smush_wave.h"
+
+namespace Grim {
+
+SmushWave::SmushWave(SmushSound *sound) {
+ _sound = sound;
+ _packetBuffer = nullptr;
+ _trackId = -1;
+ _chunkCounter = 0;
+ _totalSize = 0;
+ memset(_vimaTable, 0, sizeof(_vimaTable));
+ _vimaTableBuilt = false;
+}
+
+SmushWave::~SmushWave() {
+ moduleShutdown();
+}
+
+int SmushWave::moduleStartup() {
+ _packetBuffer = (byte *)calloc(SMUSH_WAVE_PACKET_SIZE, 1);
+ return (_packetBuffer != nullptr) - 1;
+}
+
+int SmushWave::moduleShutdown() {
+ free(_packetBuffer);
+ _packetBuffer = nullptr;
+ return 0;
+}
+
+const char *SmushWave::flobdecInfo(int index) {
+ switch (index) {
+ case 0:
+ return "Audio";
+ case 1:
+ return "Decompresses VBR-ADPCM 16-bit audio.";
+ case 2:
+ return "Decompresses Variable Bit Rate ADPCM 16-bit audio.";
+ default:
+ return "";
+ }
+}
+
+uint32 SmushWave::flobdecGetID() {
+ return MKTAG('W', 'a', 'v', 'e');
+}
+
+int SmushWave::flobdecGetType() {
+ return 4;
+}
+
+void SmushWave::flobdecReset() {
+ _chunkCounter = 0;
+}
+
+int SmushWave::flobdecDecodeHeader(const byte *header) {
+ _chunkCounter = 0;
+ _totalSize = READ_LE_UINT32(header + 8);
+ return 0;
+}
+
+int SmushWave::flobdecDecodeBody(const byte *body) {
+ const byte *data = body + 4;
+ int sampleCount = READ_BE_INT32(body);
+ if (sampleCount < 0) {
+ int trackId = READ_BE_INT32(body + 4);
+ if (trackId != _trackId) {
+ _trackId = trackId;
+ _chunkCounter = 0;
+ }
+
+ sampleCount = READ_BE_INT32(body + 8);
+ data = body + 12;
+ }
+
+ int channelCount = (data[0] & 0x80) ? 2 : 1;
+
+ if (_sound) {
+ if (!_vimaTableBuilt) {
+ vimaInit(_vimaTable);
+ _vimaTableBuilt = true;
+ }
+
+ byte *packet = _packetBuffer;
+ int pcmSize = 2 * channelCount * sampleCount;
+
+ if (_chunkCounter) {
+ memcpy(packet, "PSAD", 4);
+ WRITE_BE_UINT32(packet + 4, pcmSize + 10);
+ WRITE_LE_UINT16(packet + 8, 100);
+ WRITE_LE_UINT16(packet + 10, _chunkCounter);
+ WRITE_LE_UINT16(packet + 12, 100);
+ packet[16] = 127;
+ packet[17] = 0;
+
+ decompressVima(data, (int16 *)(packet + 18), pcmSize, _vimaTable, true);
+ } else {
+ memcpy(packet, "PSAD", 4);
+ WRITE_BE_UINT32(packet + 4, pcmSize + 46);
+ WRITE_LE_UINT16(packet + 8, 100);
+ WRITE_LE_UINT16(packet + 10, _chunkCounter);
+ WRITE_LE_UINT16(packet + 12, 100);
+ packet[16] = 127;
+ packet[17] = 0;
+ WRITE_BE_UINT32(packet + 22, _totalSize + 36);
+ WRITE_LE_UINT32(packet + 30, 201326592);
+ packet[34] = 1;
+ packet[35] = 8;
+ WRITE_LE_UINT32(packet + 36, 0);
+ WRITE_BE_UINT32(packet + 40, _totalSize);
+ packet[44] = 5;
+ packet[45] = 0;
+ memcpy(packet + 46, (channelCount == 1) ? "S6MZ" : "S6SZ", 4);
+ WRITE_BE_UINT32(packet + 50, _totalSize);
+
+ decompressVima(data, (int16 *)(packet + 54), pcmSize, _vimaTable, true);
+ }
+
+ _sound->souEngineStart(packet, -1, 127, 0, 64);
+ _chunkCounter++;
+ }
+
+ if (_chunkCounter && _totalSize > 100000)
+ return 0;
+
+ return 128;
+}
+
+int SmushWave::flobdecSetVal() {
+ return -1;
+}
+
+int SmushWave::flobdecGetVal() {
+ return -1;
+}
+
+} // End of namespace Grim
diff --git a/engines/grim/movie/smush_wave.h b/engines/grim/movie/smush_wave.h
new file mode 100644
index 00000000000..bbf6a362009
--- /dev/null
+++ b/engines/grim/movie/smush_wave.h
@@ -0,0 +1,63 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifndef GRIM_MOVIE_SMUSH_WAVE_H
+#define GRIM_MOVIE_SMUSH_WAVE_H
+
+#include "common/scummsys.h"
+
+namespace Grim {
+
+#define SMUSH_WAVE_PACKET_SIZE 500000
+
+class SmushSound;
+
+class SmushWave {
+
+private:
+ SmushSound *_sound;
+ byte *_packetBuffer;
+ int _trackId;
+ uint16 _chunkCounter;
+ uint32 _totalSize;
+ uint16 _vimaTable[5786];
+ bool _vimaTableBuilt;
+
+public:
+ SmushWave(SmushSound *sound);
+ ~SmushWave();
+
+ int moduleStartup();
+ int moduleShutdown();
+
+ const char *flobdecInfo(int index);
+ uint32 flobdecGetID();
+ int flobdecGetType();
+ void flobdecReset();
+ int flobdecDecodeHeader(const byte *header);
+ int flobdecDecodeBody(const byte *body);
+ int flobdecSetVal();
+ int flobdecGetVal();
+
+};
+
+} // End of namespace Grim
+#endif
Commit: 5a746dccd1bea1e4ebf85bc11be2e39469a35c2b
https://github.com/scummvm/scummvm/commit/5a746dccd1bea1e4ebf85bc11be2e39469a35c2b
Author: AndywinXp (andywinxp at gmail.com)
Date: 2026-11-01T22:18:07+01:00
Commit Message:
GRIM: SMUSH: Match the disasm's Blocky16 buffers and frame skipping
* Drop the hack for animations whose height is not a multiple of 8
* Skip the decoding of non-reference frames when the display is skipped
Changed paths:
engines/grim/movie/codecs/blocky16.cpp
engines/grim/movie/codecs/blocky16.h
engines/grim/movie/codecs/smush_decoder.cpp
engines/grim/movie/codecs/smush_decoder.h
engines/grim/movie/smush_animator.cpp
engines/grim/movie/smush_animator.h
diff --git a/engines/grim/movie/codecs/blocky16.cpp b/engines/grim/movie/codecs/blocky16.cpp
index 9fddba0cdb3..303d2727ea0 100644
--- a/engines/grim/movie/codecs/blocky16.cpp
+++ b/engines/grim/movie/codecs/blocky16.cpp
@@ -473,19 +473,20 @@ void Blocky16::level2(byte *d_dst) {
_d_src += 2;
}
byte *tmp_ptr = _tableSmall + (tmp * 128);
- byte l = tmp_ptr[96];
+ // The disasm writes at least one entry, even when the list is empty
+ int l = tmp_ptr[96];
int16 *tmp_ptr2 = (int16 *)tmp_ptr;
- while (l--) {
+ do {
WRITE_2X1_LINE(d_dst + READ_LE_UINT16(tmp_ptr2) * 2, val);
tmp_ptr2++;
- }
+ } while (l-- > 1);
l = tmp_ptr[97];
val >>= 16;
tmp_ptr2 = (int16 *)(tmp_ptr + 32);
- while (l--) {
+ do {
WRITE_2X1_LINE(d_dst + READ_LE_UINT16(tmp_ptr2) * 2, val);
tmp_ptr2++;
- }
+ } while (l-- > 1);
} else if (code >= 0xF9) {
if (code == 0xFD) {
t = *_d_src++;
@@ -558,19 +559,19 @@ void Blocky16::level1(byte *d_dst) {
_d_src += 2;
}
byte *tmp_ptr = _tableBig + (tmp * 388);
- byte l = tmp_ptr[384];
+ int l = tmp_ptr[384];
int16 *tmp_ptr2 = (int16 *)tmp_ptr;
- while (l--) {
+ do {
WRITE_2X1_LINE(d_dst + READ_LE_UINT16(tmp_ptr2) * 2, val);
tmp_ptr2++;
- }
+ } while (l-- > 1);
l = tmp_ptr[385];
val >>= 16;
tmp_ptr2 = (int16 *)(tmp_ptr + 128);
- while (l--) {
+ do {
WRITE_2X1_LINE(d_dst + READ_LE_UINT16(tmp_ptr2) * 2, val);
tmp_ptr2++;
- }
+ } while (l-- > 1);
} else if (code >= 0xF9) {
if (code == 0xFD) {
t = *_d_src++;
@@ -624,19 +625,16 @@ void Blocky16::init(int width, int height) {
_frameSize = _width * _height * 2;
- // some animations, like tb_kitty.snm don't have a multiple of 8 width or height,
- // so set the size of the buffer in _blocksWidth * 8 * _blocksHeight * 8, instead
- // of using _frameSize.
- int size = _blocksWidth * 8 * _blocksHeight * 8 * 2;
- _offset = size - _frameSize;
+ // The disasm rounds the height of each buffer up to a multiple of 8
+ _bufSize = _width * _blocksHeight * 8 * 2;
// workaround for read over buffer by increasing buffer
// 200 bytes is enough for smush anims:
// lol, byeruba, crushed, eldepot, heltrain, hostage
- uint32 deltaSize = size * 3 + 200;
+ uint32 deltaSize = _bufSize * 3 + 200;
_deltaBuf = new byte[deltaSize]();
_deltaBufs[0] = _deltaBuf;
- _deltaBufs[1] = _deltaBuf + _frameSize;
- _curBuf = _deltaBuf + _frameSize * 2;
+ _deltaBufs[1] = _deltaBuf + _bufSize;
+ _curBuf = _deltaBuf + _bufSize * 2;
}
Blocky16::Blocky16() {
@@ -653,8 +651,9 @@ Blocky16::Blocky16() {
_param6_7Ptr = nullptr;
_blocksHeight = _blocksWidth = 0;
_height = _width = 0;
- _offset = _offset1 = _offset2 = 0;
+ _offset1 = _offset2 = 0;
_frameSize = 0;
+ _bufSize = 0;
_d_pitch = 0;
}
@@ -743,7 +742,7 @@ static void bompDecodeMain(byte *dst, const byte *src, int size) {
}
}
-void Blocky16::decode(byte *dst, const byte *src) {
+void Blocky16::decode(byte *dst, const byte *src, int flags) {
_offset1 = ((_deltaBufs[1] - _curBuf) / 2) * 2;
_offset2 = ((_deltaBufs[0] - _curBuf) / 2) * 2;
@@ -754,10 +753,10 @@ void Blocky16::decode(byte *dst, const byte *src) {
if (seq_nb == 0) {
makeTables47(_width);
if (src[32] == src[33]) {
- memset(_deltaBufs[0], src[32], _frameSize);
- memset(_deltaBufs[1], src[32], _frameSize);
+ memset(_deltaBufs[0], src[32], _bufSize);
+ memset(_deltaBufs[1], src[32], _bufSize);
} else {
- int count = _frameSize / 2;
+ int count = _bufSize / 2;
uint16 *ptr1 = (uint16 *)_deltaBufs[0];
uint16 *ptr2 = (uint16 *)_deltaBufs[1];
uint16 val = READ_LE_UINT16(src + 32);
@@ -770,6 +769,12 @@ void Blocky16::decode(byte *dst, const byte *src) {
_prevSeqNb = -1;
}
+ // The disasm doesn't decode the frames that don't swap buffers when the display is skipped
+ if ((flags & 4) && !src[19] && src[18] >= 2 && src[18] <= 4) {
+ _prevSeqNb = seq_nb;
+ return;
+ }
+
switch (src[18]) {
case 0:
#if defined(SCUMM_BIG_ENDIAN)
@@ -835,8 +840,8 @@ void Blocky16::decode(byte *dst, const byte *src) {
} else if (src[19] == 2) {
tmp_ptr = _deltaBufs[0];
_deltaBufs[0] = _deltaBufs[1];
- _deltaBufs[1] = _curBuf - _offset; // subtract the offset here or else a black bar will appear
- _curBuf = tmp_ptr; // on top of tb_kitty.snm.
+ _deltaBufs[1] = _curBuf;
+ _curBuf = tmp_ptr;
}
}
_prevSeqNb = seq_nb;
diff --git a/engines/grim/movie/codecs/blocky16.h b/engines/grim/movie/codecs/blocky16.h
index 45b034bcfe6..8d1c6148643 100644
--- a/engines/grim/movie/codecs/blocky16.h
+++ b/engines/grim/movie/codecs/blocky16.h
@@ -41,7 +41,7 @@ private:
byte *_tableSmall;
int16 _table[256];
int32 _frameSize;
- int _offset;
+ int32 _bufSize;
int _width, _height;
int _blocksWidth, _blocksHeight;
@@ -57,7 +57,7 @@ public:
~Blocky16();
void init(int width, int height);
void deinit();
- void decode(byte *dst, const byte *src);
+ void decode(byte *dst, const byte *src, int flags);
};
} // end of namespace Grim
diff --git a/engines/grim/movie/codecs/smush_decoder.cpp b/engines/grim/movie/codecs/smush_decoder.cpp
index d9090343e6c..0e14626cbc4 100644
--- a/engines/grim/movie/codecs/smush_decoder.cpp
+++ b/engines/grim/movie/codecs/smush_decoder.cpp
@@ -396,7 +396,7 @@ void SmushDecoder::handleFRME(Common::SeekableReadStream *stream, uint32 size) {
delete[] block;
}
-void SmushDecoder::decodeFrameBlock(const byte *block, uint32 size) {
+void SmushDecoder::decodeFrameBlock(const byte *block, uint32 size, int flags) {
Common::MemoryReadStream *memStream = new Common::MemoryReadStream(block, size, DisposeAfterUse::NO);
int remaining = size;
@@ -407,7 +407,7 @@ void SmushDecoder::decodeFrameBlock(const byte *block, uint32 size) {
switch (subType) {
case MKTAG('B', 'l', '1', '6'):
- _videoTrack->handleBlocky16(memStream, subSize);
+ _videoTrack->handleBlocky16(memStream, subSize, flags);
_blocky16Frames++;
break;
case MKTAG('W', 'a', 'v', 'e'):
@@ -553,7 +553,7 @@ void SmushDecoder::SmushVideoTrack::setFrameStart(int frame) {
_frameStart = frame - 1;
}
-void SmushDecoder::SmushVideoTrack::handleBlocky16(Common::SeekableReadStream *stream, uint32 size) {
+void SmushDecoder::SmushVideoTrack::handleBlocky16(Common::SeekableReadStream *stream, uint32 size, int flags) {
if (_curFrame < _frameStart) {
return;
}
@@ -562,7 +562,7 @@ void SmushDecoder::SmushVideoTrack::handleBlocky16(Common::SeekableReadStream *s
byte *ptr = new byte[size];
stream->read(ptr, size);
- _blocky16->decode((byte *)_surface.getPixels(), ptr);
+ _blocky16->decode((byte *)_surface.getPixels(), ptr, flags);
delete[] ptr;
}
diff --git a/engines/grim/movie/codecs/smush_decoder.h b/engines/grim/movie/codecs/smush_decoder.h
index 49586fa6af2..2e00787477b 100644
--- a/engines/grim/movie/codecs/smush_decoder.h
+++ b/engines/grim/movie/codecs/smush_decoder.h
@@ -49,7 +49,7 @@ public:
void setLooping(bool l);
void setWave(SmushWave *wave) { _wave = wave; }
Common::SeekableReadStream *getFile() { return _file; }
- void decodeFrameBlock(const byte *block, uint32 size);
+ void decodeFrameBlock(const byte *block, uint32 size, int flags);
const Graphics::Surface *getFrameSurface();
void resetBlocky16Frames() { _blocky16Frames = 0; }
int getBlocky16Frames() const { return _blocky16Frames; }
@@ -87,7 +87,7 @@ protected:
bool seek(const Audio::Timestamp &time) override { return true; }
void setFrameStart(int frame);
- void handleBlocky16(Common::SeekableReadStream *stream, uint32 size);
+ void handleBlocky16(Common::SeekableReadStream *stream, uint32 size, int flags = 0);
void handleFrameObject(Common::SeekableReadStream *stream, uint32 size);
void handleDeltaPalette(Common::SeekableReadStream *stream, int32 size);
void init();
diff --git a/engines/grim/movie/smush_animator.cpp b/engines/grim/movie/smush_animator.cpp
index c796bcba59b..b39b9b7c40b 100644
--- a/engines/grim/movie/smush_animator.cpp
+++ b/engines/grim/movie/smush_animator.cpp
@@ -146,8 +146,10 @@ int SmushAnimator::checkSkip() {
return 0;
}
-void SmushAnimator::decodeBlocks() {
- _decoder->decodeFrameBlock(_block, _blockSize);
+void SmushAnimator::decodeBlocks(int flags) {
+ int allFlags = _flags | flags;
+ int decodeFlags = ((allFlags >> 13) & 4) | (allFlags & 0x30);
+ _decoder->decodeFrameBlock(_block, _blockSize, decodeFlags);
}
bool SmushAnimator::moreData() {
@@ -181,7 +183,7 @@ int SmushAnimator::animatorRender(int flags) {
if (_block) {
_frame++;
if (_skipping || !checkSkip()) {
- decodeBlocks();
+ decodeBlocks(flags);
if (_frame < _frameCount - 1 || _skipping) {
if (_pause == 1)
diff --git a/engines/grim/movie/smush_animator.h b/engines/grim/movie/smush_animator.h
index 87372f511ae..e4aabf6f556 100644
--- a/engines/grim/movie/smush_animator.h
+++ b/engines/grim/movie/smush_animator.h
@@ -52,7 +52,7 @@ private:
void readBlock(int size);
int checkSkip();
- void decodeBlocks();
+ void decodeBlocks(int flags);
bool moreData();
public:
Commit: 733532ae3b5cd935b6b177abd97a34ab2d46bcc9
https://github.com/scummvm/scummvm/commit/733532ae3b5cd935b6b177abd97a34ab2d46bcc9
Author: AndywinXp (andywinxp at gmail.com)
Date: 2026-11-01T22:18:07+01:00
Commit Message:
GRIM: SMUSH: Match the disasm's loop restart frame pacing
Changed paths:
engines/grim/movie/codecs/smush_decoder.cpp
engines/grim/movie/codecs/smush_decoder.h
engines/grim/movie/smush.cpp
engines/grim/movie/smush.h
diff --git a/engines/grim/movie/codecs/smush_decoder.cpp b/engines/grim/movie/codecs/smush_decoder.cpp
index 0e14626cbc4..53edf492f7d 100644
--- a/engines/grim/movie/codecs/smush_decoder.cpp
+++ b/engines/grim/movie/codecs/smush_decoder.cpp
@@ -61,6 +61,7 @@ SmushDecoder::SmushDecoder() {
_wave = nullptr;
_ownsAudioTrack = false;
_blocky16Frames = 0;
+ _frameDelay = 0;
_videoPause = false;
}
@@ -203,6 +204,7 @@ bool SmushDecoder::readHeader() {
int height = _file->readUint16LE();
_file->readUint16LE();
int frameRate = _file->readUint32LE();
+ _frameDelay = frameRate;
int16 flags = _file->readUint16LE();
// Output information for checking out the flags
diff --git a/engines/grim/movie/codecs/smush_decoder.h b/engines/grim/movie/codecs/smush_decoder.h
index 2e00787477b..cb57913a0ba 100644
--- a/engines/grim/movie/codecs/smush_decoder.h
+++ b/engines/grim/movie/codecs/smush_decoder.h
@@ -48,6 +48,7 @@ public:
int getY() const { return _videoTrack->_y; }
void setLooping(bool l);
void setWave(SmushWave *wave) { _wave = wave; }
+ int getFrameDelay() const { return _frameDelay; }
Common::SeekableReadStream *getFile() { return _file; }
void decodeFrameBlock(const byte *block, uint32 size, int flags);
const Graphics::Surface *getFrameSurface();
@@ -145,6 +146,7 @@ private:
SmushWave *_wave;
bool _ownsAudioTrack;
int _blocky16Frames;
+ int _frameDelay;
Common::SeekableReadStream *_file;
diff --git a/engines/grim/movie/smush.cpp b/engines/grim/movie/smush.cpp
index 97e76c8ccbf..3503331a1a9 100644
--- a/engines/grim/movie/smush.cpp
+++ b/engines/grim/movie/smush.cpp
@@ -180,7 +180,9 @@ bool SmushPlayer::prepareFrame() {
if (_videoLooping) {
_smushDecoder->rewind();
_smushDecoder->start();
- _smushAnim->anmStart(15, 24589);
+ // The disasm restarts looping movies with the frame rate of the header
+ int delay = _smushDecoder->getFrameDelay();
+ _smushAnim->anmStart(delay ? (delay / 2 + 1000000) / delay : 15, 24589);
_smushAnimator->animatorNewAnim(24589, -1, -1);
} else {
_videoFinished = true;
@@ -199,18 +201,35 @@ int SmushPlayer::animatorRender(int flags) {
_internalPalette = _smushDecoder->getPalette();
int currentFrame = _smushDecoder->getCurFrame();
- if (!(flags & 0x8000)) {
- if (_frame != currentFrame)
- _updateNeeded = true;
+ if (_frame != currentFrame)
+ _updateNeeded = true;
- _frame = currentFrame;
- }
+ _frame = currentFrame;
_movieTime = _smushDecoder->getTime();
return state;
}
+void SmushPlayer::pause(bool p) {
+ Common::StackLock lock(_frameMutex);
+ MoviePlayer::pause(p);
+
+ if (_smushAnimator && !_videoFinished && !_currentVideoIsTheora)
+ _smushAnimator->animatorPause(p);
+}
+
+int32 SmushPlayer::getMovieTime() {
+ if (_demo || _currentVideoIsTheora || !_smushSound)
+ return MoviePlayer::getMovieTime();
+
+ Common::StackLock lock(_frameMutex);
+ if (_videoFinished)
+ return 0;
+
+ return _smushSound->souEngineQueryTime(0);
+}
+
void SmushPlayer::resetFrameCounter() {
_smushDecoder->resetBlocky16Frames();
}
diff --git a/engines/grim/movie/smush.h b/engines/grim/movie/smush.h
index e999316ff36..00692ffdc77 100644
--- a/engines/grim/movie/smush.h
+++ b/engines/grim/movie/smush.h
@@ -41,6 +41,8 @@ public:
SmushPlayer(bool demo);
virtual ~SmushPlayer();
void restore(SaveGame *state) override;
+ void pause(bool p) override;
+ int32 getMovieTime() override;
private:
bool loadFile(const Common::String &filename) override;
Commit: e7826e1d125fcd4d91826286728c254fbd3f1a6f
https://github.com/scummvm/scummvm/commit/e7826e1d125fcd4d91826286728c254fbd3f1a6f
Author: AndywinXp (andywinxp at gmail.com)
Date: 2026-11-01T22:18:07+01:00
Commit Message:
GRIM: SMUSH: Remove temp workaround during rewrite
Changed paths:
engines/grim/movie/codecs/smush_decoder.cpp
engines/grim/movie/codecs/smush_decoder.h
diff --git a/engines/grim/movie/codecs/smush_decoder.cpp b/engines/grim/movie/codecs/smush_decoder.cpp
index 53edf492f7d..7d5798d18a5 100644
--- a/engines/grim/movie/codecs/smush_decoder.cpp
+++ b/engines/grim/movie/codecs/smush_decoder.cpp
@@ -59,7 +59,6 @@ SmushDecoder::SmushDecoder() {
_videoTrack = nullptr;
_audioTrack = nullptr;
_wave = nullptr;
- _ownsAudioTrack = false;
_blocky16Frames = 0;
_frameDelay = 0;
_videoPause = false;
@@ -73,7 +72,8 @@ SmushDecoder::~SmushDecoder() {
void SmushDecoder::init() {
_videoTrack->init();
- _audioTrack->init();
+ if (_audioTrack)
+ _audioTrack->init();
}
void SmushDecoder::initFrames() {
@@ -123,10 +123,6 @@ void SmushDecoder::initFrames() {
void SmushDecoder::close() {
VideoDecoder::close();
- if (_ownsAudioTrack)
- delete _audioTrack;
-
- _ownsAudioTrack = false;
_audioTrack = nullptr;
_videoTrack = nullptr;
_videoLooping = false;
@@ -257,11 +253,10 @@ bool SmushDecoder::handleFramesHeader() {
} while (pos < size);
delete[] f_header;
- _audioTrack = new SmushAudioTrack(getSoundType(), true, freq, channels);
- if (_wave)
- _ownsAudioTrack = true;
- else
+ if (!_wave) {
+ _audioTrack = new SmushAudioTrack(getSoundType(), true, freq, channels);
addTrack(_audioTrack);
+ }
return true;
}
@@ -496,8 +491,10 @@ bool SmushDecoder::seekIntern(const Audio::Timestamp &time) {
delay = delay - _videoTrack->getFrameTime(keyframe);
}
- int32 sampleCount = (delay.msecs() / 1000.f) * _audioTrack->getRate() - offset;
- _audioTrack->skipSamples(sampleCount);
+ if (_audioTrack) {
+ int32 sampleCount = (delay.msecs() / 1000.f) * _audioTrack->getRate() - offset;
+ _audioTrack->skipSamples(sampleCount);
+ }
VideoDecoder::seekIntern(time);
return true;
diff --git a/engines/grim/movie/codecs/smush_decoder.h b/engines/grim/movie/codecs/smush_decoder.h
index cb57913a0ba..87c2005a558 100644
--- a/engines/grim/movie/codecs/smush_decoder.h
+++ b/engines/grim/movie/codecs/smush_decoder.h
@@ -144,7 +144,6 @@ private:
SmushAudioTrack *_audioTrack;
SmushVideoTrack *_videoTrack;
SmushWave *_wave;
- bool _ownsAudioTrack;
int _blocky16Frames;
int _frameDelay;
Commit: 0a15cfbfe23720321b03b80b5d22ee4952a9b71b
https://github.com/scummvm/scummvm/commit/0a15cfbfe23720321b03b80b5d22ee4952a9b71b
Author: AndywinXp (andywinxp at gmail.com)
Date: 2026-11-01T22:18:07+01:00
Commit Message:
GRIM: SMUSH: Implement original demo codepath
Changed paths:
engines/grim/movie/codecs/blocky8.cpp
engines/grim/movie/codecs/blocky8.h
engines/grim/movie/smush.cpp
engines/grim/movie/smush.h
engines/grim/movie/smush_anim.cpp
engines/grim/movie/smush_anim.h
engines/grim/movie/smush_animator.cpp
engines/grim/movie/smush_animator.h
engines/grim/movie/smush_mixer.cpp
engines/grim/movie/smush_mixer.h
diff --git a/engines/grim/movie/codecs/blocky8.cpp b/engines/grim/movie/codecs/blocky8.cpp
index 5ae66b151c5..38901667cd7 100644
--- a/engines/grim/movie/codecs/blocky8.cpp
+++ b/engines/grim/movie/codecs/blocky8.cpp
@@ -544,6 +544,7 @@ Blocky8::Blocky8() {
_width = -1;
_height = -1;
_frameSize = 0;
+ _bufSize = 0;
_offset1 = 0;
_offset2 = 0;
_prevSeqNb = 0;
@@ -566,11 +567,13 @@ void Blocky8::init(int width, int height) {
makeTablesInterpolation(8);
_frameSize = _width * _height;
- uint32 deltaSize = _frameSize * 3;
+ // The disasm rounds the height of each buffer up to a multiple of 8
+ _bufSize = _width * ((_height + 7) & ~7);
+ uint32 deltaSize = _bufSize * 3;
_deltaBuf = new byte[deltaSize]();
_deltaBufs[0] = _deltaBuf;
- _deltaBufs[1] = _deltaBuf + _frameSize;
- _curBuf = _deltaBuf + _frameSize * 2;
+ _deltaBufs[1] = _deltaBuf + _bufSize;
+ _curBuf = _deltaBuf + _bufSize * 2;
}
void Blocky8::deinit() {
@@ -595,7 +598,7 @@ Blocky8::~Blocky8() {
}
}
-bool Blocky8::decode(byte *dst, const byte *src) {
+bool Blocky8::decode(byte *dst, const byte *src, int flags) {
if ((_tableBig == nullptr) || (_tableSmall == nullptr) || (_deltaBuf == nullptr))
return false;
@@ -608,8 +611,8 @@ bool Blocky8::decode(byte *dst, const byte *src) {
if (seq_nb == 0) {
makeTables47(_width);
- memset(_deltaBufs[0], src[12], _frameSize);
- memset(_deltaBufs[1], src[13], _frameSize);
+ memset(_deltaBufs[0], src[12], _bufSize);
+ memset(_deltaBufs[1], src[13], _bufSize);
_prevSeqNb = -1;
}
@@ -617,6 +620,12 @@ bool Blocky8::decode(byte *dst, const byte *src) {
gfx_data += 32896;
}
+ // The disasm doesn't decode the frames that don't swap buffers when the display is skipped
+ if ((flags & 0x10) && !src[3] && src[2] >= 2 && src[2] <= 4) {
+ _prevSeqNb = seq_nb;
+ return false;
+ }
+
switch (src[2]) {
case 0:
memcpy(_curBuf, gfx_data, _frameSize);
diff --git a/engines/grim/movie/codecs/blocky8.h b/engines/grim/movie/codecs/blocky8.h
index 7c2cfb23b62..f681c7f4123 100644
--- a/engines/grim/movie/codecs/blocky8.h
+++ b/engines/grim/movie/codecs/blocky8.h
@@ -41,6 +41,7 @@ private:
byte *_tableSmall;
int16 _table[256];
int32 _frameSize;
+ int32 _bufSize;
int _width, _height;
void makeTablesInterpolation(int param);
@@ -55,7 +56,7 @@ public:
~Blocky8();
void init(int width, int height);
void deinit();
- bool decode(byte *dst, const byte *src);
+ bool decode(byte *dst, const byte *src, int flags = 0);
};
} // End of namespace Grim
diff --git a/engines/grim/movie/smush.cpp b/engines/grim/movie/smush.cpp
index 3503331a1a9..295e85ff4fc 100644
--- a/engines/grim/movie/smush.cpp
+++ b/engines/grim/movie/smush.cpp
@@ -47,12 +47,24 @@ SmushPlayer::~SmushPlayer() {
SmushSound *sound = _smushSound;
SmushDriver *driver = _smushDriver;
SmushTimer *timer = _smushTimer;
+ SmushDemoPlayer *demoPlayer = _demoPlayer;
+ SmushDemoDriver *demoDriver = _demoDriver;
_smushAnim = nullptr;
_smushAnimator = nullptr;
_smushWave = nullptr;
_smushSound = nullptr;
_smushDriver = nullptr;
_smushTimer = nullptr;
+ _demoPlayer = nullptr;
+ _demoDriver = nullptr;
+
+ if (demoPlayer && _demoPlaying)
+ demoPlayer->smushPlayStopMovie();
+
+ delete demoPlayer;
+ delete demoDriver;
+ delete[] _demoTarget;
+ _demoTarget = nullptr;
delete anim;
delete animator;
@@ -82,6 +94,18 @@ SmushPlayer::SmushPlayer(bool demo) : MoviePlayer(), _demo(demo) {
_smushWave = nullptr;
_smushAnim = nullptr;
_smushAnimator = nullptr;
+ _demoDriver = nullptr;
+ _demoPlayer = nullptr;
+ _demoPlaying = false;
+ _demoTarget = nullptr;
+ _demoWidth = 0;
+ _demoHeight = 0;
+ memset(_demoPalette, 0, sizeof(_demoPalette));
+
+ if (_demo) {
+ _demoDriver = new SmushDemoDriver();
+ _demoPlayer = new SmushDemoPlayer(this, _demoDriver);
+ }
if (!_demo) {
_smushTimer = new SmushTimer();
@@ -138,15 +162,21 @@ bool SmushPlayer::loadFile(const Common::String &filename) {
}
void SmushPlayer::init() {
- if (!_currentVideoIsTheora) {
- if (_demo) {
- _x = _smushDecoder->getX();
- _y = _smushDecoder->getY();
- } else {
- _smushDecoder->setLooping(_videoLooping);
+ if (!_currentVideoIsTheora && !_demo)
+ _smushDecoder->setLooping(_videoLooping);
+
+ MoviePlayer::init();
+
+ if (_demo && !_currentVideoIsTheora) {
+ if (_demoPlaying) {
+ _demoPlayer->smushPlayStopMovie();
+ _demoPlaying = false;
}
+
+ _x = 0;
+ _y = 0;
+ _demoPlaying = (_demoPlayer->smushPlayStartMovie(_fname, 0, 1000000, 640, 480, 0) == 0);
}
- MoviePlayer::init();
if (_smushAnim && !_currentVideoIsTheora) {
_smushAnim->anmStop();
@@ -156,6 +186,30 @@ void SmushPlayer::init() {
}
bool SmushPlayer::prepareFrame() {
+ if (_demo && !_currentVideoIsTheora && _demoPlayer) {
+ if (_videoPause)
+ return false;
+
+ if (_videoFinished || !_demoPlaying) {
+ if (g_grim->getMode() == GrimEngine::SmushMode)
+ g_grim->setMode(GrimEngine::NormalMode);
+
+ _videoFinished = true;
+ _videoPause = true;
+ return false;
+ }
+
+ int finished = 0;
+ _demoPlayer->smushPlayUpdateMovie(&finished);
+ if (finished) {
+ _demoPlayer->smushPlayStopMovie();
+ _demoPlaying = false;
+ _videoFinished = true;
+ }
+
+ return true;
+ }
+
if (_demo || _currentVideoIsTheora || !_smushAnim)
return MoviePlayer::prepareFrame();
@@ -215,6 +269,13 @@ void SmushPlayer::pause(bool p) {
Common::StackLock lock(_frameMutex);
MoviePlayer::pause(p);
+ if (_demoPlayer && _demoPlaying) {
+ if (p)
+ _demoPlayer->smushPlayPauseMovie();
+ else
+ _demoPlayer->smushPlayResumeMovie();
+ }
+
if (_smushAnimator && !_videoFinished && !_currentVideoIsTheora)
_smushAnimator->animatorPause(p);
}
@@ -230,6 +291,54 @@ int32 SmushPlayer::getMovieTime() {
return _smushSound->souEngineQueryTime(0);
}
+int SmushPlayer::videoStart(int width, int height, int depth) {
+ _demoWidth = width;
+ _demoHeight = height;
+
+ delete[] _demoTarget;
+ _demoTarget = new byte[width * height]();
+ return 0;
+}
+
+void SmushPlayer::videoStop() {
+ _internalSurface = nullptr;
+ delete[] _demoTarget;
+ _demoTarget = nullptr;
+}
+
+void SmushPlayer::videoLock(SmushDemoTarget *target, SmushDemoRect *rect) {
+ target->pixels = _demoTarget;
+ target->width = _demoWidth;
+ target->height = _demoHeight;
+ target->bytesPerPixel = 1;
+
+ rect->x = 0;
+ rect->y = 0;
+ rect->width = _demoWidth;
+ rect->height = _demoHeight;
+}
+
+void SmushPlayer::videoUnlock() {
+ const byte *pixels;
+ int width;
+ int height;
+ _demoPlayer->getFrameBuffer(&pixels, &width, &height);
+
+ _demoSurface.init(width, height, _demoWidth, _demoTarget, Graphics::PixelFormat::createFormatCLUT8());
+ _internalSurface = &_demoSurface;
+ _internalPalette = _demoPalette;
+ _frame++;
+ _updateNeeded = true;
+}
+
+void SmushPlayer::videoSetPalette(const uint32 *palette, int start, int count) {
+ for (int i = start; i < start + count; i++) {
+ _demoPalette[i * 3] = palette[i] & 0xFF;
+ _demoPalette[i * 3 + 1] = (palette[i] >> 8) & 0xFF;
+ _demoPalette[i * 3 + 2] = (palette[i] >> 16) & 0xFF;
+ }
+}
+
void SmushPlayer::resetFrameCounter() {
_smushDecoder->resetBlocky16Frames();
}
@@ -259,9 +368,16 @@ void SmushPlayer::handleFrame() {
void SmushPlayer::handleTimer() {
if (_smushTimer)
_smushTimer->timerThread();
+ else if (_demoPlayer && _demoPlaying)
+ _demoPlayer->streamStep();
}
void SmushPlayer::deinit() {
+ if (_demoPlayer && _demoPlaying) {
+ _demoPlayer->smushPlayStopMovie();
+ _demoPlaying = false;
+ }
+
// The disasm stops the sound only if the movie is cut, not when it ends by itself
bool wasPlaying = !_videoFinished;
@@ -275,13 +391,26 @@ void SmushPlayer::deinit() {
}
void SmushPlayer::postHandleFrame() {
- if (_demo && !_currentVideoIsTheora) {
- _x = _smushDecoder->getX();
- _y = _smushDecoder->getY();
- }
+ if (_demoPlayer && !_currentVideoIsTheora)
+ _demoPlayer->getSize(&_x, &_y);
}
void SmushPlayer::restore(SaveGame *state) {
+ if (_demoPlayer && !_currentVideoIsTheora) {
+ if (isPlaying()) {
+ if (_demoPlaying) {
+ _demoPlayer->smushPlayStopMovie();
+ _demoPlaying = false;
+ }
+
+ _frame = -1;
+ _demoPlaying = (_demoPlayer->smushPlayStartMovie(_fname, 0, 1000000, 640, 480, 0) == 0);
+ timerCallback(this);
+ }
+
+ return;
+ }
+
if (isPlaying() && !_currentVideoIsTheora) {
if (_smushAnim) {
_frame = -1;
diff --git a/engines/grim/movie/smush.h b/engines/grim/movie/smush.h
index 00692ffdc77..a6830ec2a50 100644
--- a/engines/grim/movie/smush.h
+++ b/engines/grim/movie/smush.h
@@ -25,6 +25,7 @@
#include "engines/grim/movie/movie.h"
#include "engines/grim/movie/smush_anim.h"
+#include "graphics/surface.h"
#include "video/video_decoder.h"
namespace Grim {
@@ -36,7 +37,7 @@ class SmushDriver;
class SmushSound;
class SmushWave;
-class SmushPlayer : public MoviePlayer, public SmushAnimClient {
+class SmushPlayer : public MoviePlayer, public SmushAnimClient, public SmushDemoVideo {
public:
SmushPlayer(bool demo);
virtual ~SmushPlayer();
@@ -44,6 +45,12 @@ public:
void pause(bool p) override;
int32 getMovieTime() override;
+ int videoStart(int width, int height, int depth) override;
+ void videoStop() override;
+ void videoLock(SmushDemoTarget *target, SmushDemoRect *rect) override;
+ void videoUnlock() override;
+ void videoSetPalette(const uint32 *palette, int start, int count) override;
+
private:
bool loadFile(const Common::String &filename) override;
bool prepareFrame() override;
@@ -64,6 +71,14 @@ private:
SmushWave *_smushWave;
SmushAnimPlay *_smushAnim;
SmushAnimator *_smushAnimator;
+ SmushDemoDriver *_demoDriver;
+ SmushDemoPlayer *_demoPlayer;
+ bool _demoPlaying;
+ byte *_demoTarget;
+ int _demoWidth;
+ int _demoHeight;
+ byte _demoPalette[768];
+ Graphics::Surface _demoSurface;
Video::VideoDecoder *_theoraDecoder; // HACK for now, move to other class later?
};
diff --git a/engines/grim/movie/smush_anim.cpp b/engines/grim/movie/smush_anim.cpp
index 6552e1429a4..0145be58e29 100644
--- a/engines/grim/movie/smush_anim.cpp
+++ b/engines/grim/movie/smush_anim.cpp
@@ -20,6 +20,10 @@
*/
#include "common/scummsys.h"
+#include "common/endian.h"
+#include "common/file.h"
+#include "common/system.h"
+#include "common/util.h"
#include "engines/grim/movie/smush_anim.h"
#include "engines/grim/movie/smush_sound.h"
@@ -210,4 +214,721 @@ void SmushAnimPlay::syncTimer(int timer, void *context) {
anim->_lateTicks++;
}
+SmushDemoPlayer::SmushDemoPlayer(SmushDemoVideo *video, SmushDemoDriver *driver) {
+ _video = video;
+ _driver = driver;
+ _stream = new SmushDemoStream();
+ _anim = new SmushDemoAnim(_stream, this);
+ memset(&_audio, 0, sizeof(_audio));
+ _useDriver = true;
+ _playing = 0;
+ _playFlags = 0;
+ memset(_staging, 0, sizeof(_staging));
+ memset(_palette, 0, sizeof(_palette));
+ memset(_paletteSaved, 0, sizeof(_paletteSaved));
+ memset(_paletteTarget, 0, sizeof(_paletteTarget));
+ _frameSource = nullptr;
+ _frameWidth = 0;
+ _frameHeight = 0;
+ _positionX = 0;
+ _positionY = 0;
+ _sourceX = 0;
+ _sourceY = 0;
+ _paused = 0;
+ _done = 0;
+ _flags = 0;
+ _lastResult = 0;
+ _frameReady = 0;
+ _paletteFlag = 0;
+ _annotation = 0;
+ _staged = 0;
+ _audioPaused = 0;
+ _audioStartFrame = 0;
+ _audioClockBase = 0;
+ _audioClockLast = 0;
+ _startTime = 0;
+ _lastUpdate = 0;
+ _fps = 12;
+ _frame = 0;
+ _framesShown = 0;
+ _frameCount = 1;
+ _dropCount = 0;
+ _behind = 0;
+ _fadeStart = 0;
+ _fadeSteps = 1;
+ _fadeRemaining = 0;
+ _firstUpdate = 1;
+ _streamStop = 1;
+}
+
+SmushDemoPlayer::~SmushDemoPlayer() {
+ delete _anim;
+ delete _stream;
+}
+
+int SmushDemoPlayer::preFrame(int frame, int frameCount) {
+ _frameCount = frameCount;
+ return 0;
+}
+
+int SmushDemoPlayer::postFrame(int frame, int frameCount) {
+ _frameCount = frameCount;
+ _frame = frame;
+ return 0;
+}
+
+int SmushDemoPlayer::frameCallback(int flags, bool paletteChanged, bool display, int paletteState) {
+ if (paletteState) {
+ const byte *palette = _anim->anmGetPalette();
+ for (int i = 0; i < 256; i++)
+ _palette[i] = palette[i * 3] | (palette[i * 3 + 1] << 8) | (palette[i * 3 + 2] << 16);
+
+ _paletteFlag = (paletteChanged ? 1 : 0) + 1;
+ }
+
+ _frameReady = 1;
+ return 0;
+}
+
+int SmushDemoPlayer::chunkCallback(const byte *chunk, int size, int flags) {
+ if (READ_LE_UINT16(chunk) != 8)
+ return 0;
+
+ int part = (int16)READ_LE_UINT16(chunk + 6);
+ const byte *data = chunk + 18;
+ int remaining = size - 18;
+
+ if (part) {
+ if (part <= 0 || part > 5)
+ return 0;
+
+ memcpy(_paletteSaved, _palette, sizeof(_palette));
+ for (int i = 0; i < 256; i++) {
+ _paletteTarget[i] = data[0] | (READ_LE_UINT16(data + 1) << 8);
+ data += 3;
+ }
+
+ _fadeSteps = 5 * part;
+ _fadeRemaining = 5 * part;
+ _fadeStart = _framesShown;
+ return 0;
+ }
+
+ _audioStartFrame = _frame;
+ if (!remaining)
+ return 0;
+
+ int staged = _staged;
+ while (true) {
+ if (staged >= 2) {
+ int needed = (_staging[0] << 8) + _staging[1] + 2 - staged;
+ if (needed > remaining) {
+ memcpy(_staging + staged, data, remaining);
+ staged += remaining;
+ _staged = staged;
+ return 0;
+ }
+
+ memcpy(_staging + staged, data, needed);
+ decodeIACT(_staging);
+ staged = 0;
+ data += needed;
+ _staged = 0;
+ remaining -= needed;
+ if (!remaining)
+ return 0;
+
+ continue;
+ }
+
+ if (remaining == 1) {
+ if (staged) {
+ _staging[1] = *data++;
+ remaining--;
+ staged = 2;
+ _staged = 2;
+ if (!remaining)
+ return 0;
+
+ continue;
+ }
+
+ _staging[0] = *data;
+ _staged = 1;
+ return 0;
+ }
+
+ if (!staged) {
+ _staging[0] = *data++;
+ remaining--;
+ }
+
+ _staging[1] = *data++;
+ remaining--;
+ staged = 2;
+ _staged = 2;
+ if (!remaining)
+ return 0;
+ }
+}
+
+void SmushDemoPlayer::setAnnotation(int value) {
+ _annotation = value;
+}
+
+void SmushDemoPlayer::setFrameRate(int fps) {
+ _fps = (int16)fps;
+ if (_startTime)
+ _startTime = audioGetTime();
+}
+
+void SmushDemoPlayer::setFrameBuffer(byte *buffer, int x, int y, int sourceX, int sourceY, int width, int height) {
+ _frameSource = buffer;
+ _frameWidth = width;
+ _frameHeight = height;
+ _positionX = x;
+ _positionY = y;
+ _sourceX = sourceX;
+ _sourceY = sourceY;
+}
+
+void SmushDemoPlayer::decodeIACT(const byte *block) {
+ int shiftLeft = block[2] >> 4;
+ int shiftRight = block[2] & 0xF;
+ byte *destination = _audio.data + _audio.produced % _audio.size;
+ const byte *cursor = block + 3;
+
+ for (int i = 0; i < 1024; i++) {
+ int16 sample;
+
+ if (*cursor == 128) {
+ sample = (int16)((cursor[1] << 8) + cursor[2]);
+ cursor += 3;
+ } else {
+ sample = (int16)((int8)*cursor << shiftLeft);
+ cursor++;
+ }
+
+ WRITE_LE_UINT16(destination, sample);
+ destination += 2;
+
+ if (*cursor == 128) {
+ sample = (int16)((cursor[1] << 8) + cursor[2]);
+ cursor += 3;
+ } else {
+ sample = (int16)((int8)*cursor << shiftRight);
+ cursor++;
+ }
+
+ WRITE_LE_UINT16(destination, sample);
+ destination += 2;
+ }
+
+ _audio.produced += 4096;
+}
+
+void SmushDemoPlayer::fadePalette() {
+ if (!_fadeRemaining)
+ return;
+
+ int steps = _fadeSteps;
+ int remaining = _fadeStart + steps - _frame;
+ int elapsed = _frame - _fadeStart;
+
+ for (int i = 0; i < 256; i++) {
+ uint32 from = _paletteSaved[i];
+ uint32 to = _paletteTarget[i];
+ uint32 red = (remaining * (from & 0xFF) + elapsed * (to & 0xFF)) / steps;
+ uint32 green = (elapsed * ((to >> 8) & 0xFF) + remaining * ((from >> 8) & 0xFF)) / steps;
+ uint32 blue = (elapsed * ((to >> 16) & 0xFF) + remaining * ((from >> 16) & 0xFF)) / steps;
+ _palette[i] = (red & 0xFF) | ((green & 0xFF) << 8) | ((blue & 0xFF) << 16);
+ }
+
+ _fadeRemaining--;
+ _paletteFlag = 1;
+}
+
+int SmushDemoPlayer::audioOpen(int rate, int channels, int bits) {
+ if (_useDriver)
+ return _driver->sndDrivOpen(rate, channels, bits, &_audio);
+
+ _audio.data = new byte[_audio.size]();
+ return _audio.data == nullptr;
+}
+
+void SmushDemoPlayer::audioClose() {
+ if (_useDriver) {
+ _driver->sndDrivClose(&_audio);
+ return;
+ }
+
+ delete[] _audio.data;
+ _audio.data = nullptr;
+}
+
+int SmushDemoPlayer::audioPause() {
+ if (!_useDriver)
+ return 1;
+
+ int result = _driver->sndDrivPause(&_audio);
+ if (!result)
+ _audioPaused = 1;
+
+ return result;
+}
+
+int SmushDemoPlayer::audioResume() {
+ if (!_useDriver)
+ return 1;
+
+ int result = _driver->sndDrivResume(&_audio);
+ if (!result)
+ _audioPaused = 0;
+
+ return result;
+}
+
+void SmushDemoPlayer::audioFill() {
+ if (!_useDriver) {
+ int consumed = _audio.produced - 1024;
+ _audio.consumed = consumed <= 0 ? 0 : consumed;
+ return;
+ }
+
+ if (_frame - _audioStartFrame > 5) {
+ int produced = _audio.produced;
+ if (produced * _fps / 88200 >= _frameCount - 1) {
+ int space = _audio.size + _audio.consumed - produced;
+ if (space > 4096) {
+ int blocks = (space - 1) >> 12;
+ do {
+ memset(_audio.data + _audio.produced % _audio.size, 0, 4096);
+ blocks--;
+ _audio.produced += 4096;
+ } while (blocks);
+ }
+ }
+ }
+
+ _driver->sndDrivFill(&_audio);
+}
+
+int SmushDemoPlayer::audioGetTime() {
+ if (!_useDriver)
+ return g_system->getMillis();
+
+ if (_audioStartFrame || _frame <= 2)
+ return _driver->sndDrivGetTime(&_audio);
+
+ _audioClockLast = _driver->sndDrivGetTime(&_audio);
+ if (!_audioClockBase)
+ _audioClockBase = g_system->getMillis() - 1000 * _frame / _fps;
+
+ return _audioClockLast - _audioClockBase + g_system->getMillis();
+}
+
+int SmushDemoPlayer::getFramesDue() {
+ if (_firstUpdate)
+ return 1;
+
+ return (int)((uint32)(_fps * (audioGetTime() - _startTime)) / 1000u - (uint32)_framesShown);
+}
+
+int SmushDemoPlayer::stepAnimation() {
+ int result;
+
+ while (true) {
+ int flags = (!_behind || _lastResult == 11) ? 0 : 16;
+ result = _anim->animatorRender(flags);
+ _lastResult = (int16)result;
+ if ((int16)result != 11)
+ break;
+
+ if (!_stream->streamFill(24576))
+ break;
+ }
+
+ if (result & 0x8000)
+ _done = 1;
+
+ return result;
+}
+
+bool SmushDemoPlayer::streamStep() {
+ if (_streamStop)
+ return false;
+
+ bool read = false;
+ while (_stream->streamFill(24576))
+ read = true;
+
+ return read;
+}
+
+int SmushDemoPlayer::initialize(int bufferBytes, int width, int height) {
+ if (_stream->streamInitialize(bufferBytes - (width * height + 204800), width * height + 204800))
+ return 0;
+
+ if (_anim->anmInitialize(width, height)) {
+ _stream->streamTerminate();
+ return 0;
+ }
+
+ if (_anim->anmBuffersInitialize(0, 0)) {
+ _anim->anmTerminate();
+ _stream->streamTerminate();
+ return 0;
+ }
+
+ if (_anim->anmRegisterCodecs(width, height)) {
+ _anim->anmBuffersTerminate();
+ _anim->anmTerminate();
+ _stream->streamTerminate();
+ return 0;
+ }
+
+ return 1;
+}
+
+void SmushDemoPlayer::terminate() {
+ _anim->anmUnregisterCodecs();
+ _anim->anmBuffersTerminate();
+ _anim->anmTerminate();
+ _stream->streamTerminate();
+}
+
+int SmushDemoPlayer::open(const Common::String &name, int flags, int offset) {
+ if (!name.empty() && !Common::File::exists(Common::Path(name)))
+ return 1;
+
+ memset(&_audio, 0, sizeof(_audio));
+ _audio.size = 491520;
+
+ if (audioOpen(22050, 2, 16))
+ return 1;
+
+ _anim->animatorInitialize();
+ _anim->animatorNewAnim(name, 0, -1, -1, 0, offset);
+
+ if (!name.empty())
+ _stream->streamFill(0x20000);
+ else
+ _stream->streamInitMemory();
+
+ memset(_palette, 0, sizeof(_palette));
+ _paused = 0;
+ _done = 0;
+ _flags = flags;
+ _lastResult = 0;
+ _frameReady = 0;
+ _paletteFlag = 0;
+ _annotation = 0;
+ _staged = 0;
+ _audioPaused = 0;
+ _audioStartFrame = 0;
+ _audioClockBase = 0;
+ _startTime = 0;
+ _lastUpdate = g_system->getMillis();
+ _frameSource = nullptr;
+ _frameWidth = 0;
+ _frameHeight = 0;
+ _fps = 12;
+ _frame = 0;
+ _framesShown = 0;
+ _frameCount = 1;
+ _dropCount = 0;
+ _behind = 0;
+ _firstUpdate = 1;
+ _fadeStart = 0;
+ _fadeRemaining = 0;
+ _fadeSteps = 1;
+ _streamStop = name.empty() ? 1 : 0;
+ return 0;
+}
+
+int SmushDemoPlayer::close() {
+ audioClose();
+ memset(&_audio, 0, sizeof(_audio));
+
+ _streamStop = 0;
+ _done = 1;
+ return 0;
+}
+
+int SmushDemoPlayer::update(int *frameDecoded, int *finished, int *annotation, int *paletteChanged, const uint32 **palette) {
+ *finished = 0;
+ *frameDecoded = 0;
+ *annotation = 0;
+ *paletteChanged = 0;
+ *palette = nullptr;
+
+ bool late = false;
+ if (_paused)
+ return 0;
+
+ if (!_frameReady) {
+ int saved = _annotation;
+ _annotation = 0;
+ stepAnimation();
+ if (!_frameReady && !_annotation)
+ _annotation = saved;
+
+ audioFill();
+ }
+
+ uint32 time = g_system->getMillis();
+ if (time - _lastUpdate > 200)
+ late = true;
+
+ int due;
+ if (late)
+ due = 1;
+ else
+ due = getFramesDue();
+
+ if (due <= _fps / 2 || _audioPaused) {
+ if (due < 1 && _audioPaused)
+ audioResume();
+ } else {
+ audioPause();
+ }
+
+ if (due > 0) {
+ fadePalette();
+ if (_frameReady)
+ *frameDecoded = 1;
+
+ *paletteChanged = _paletteFlag;
+ *palette = _palette;
+ *annotation = _annotation;
+ _paletteFlag = 0;
+ _frameReady = 0;
+ _lastUpdate = time;
+ if (_firstUpdate) {
+ _firstUpdate = 0;
+ _startTime = audioGetTime();
+ }
+ }
+
+ if (_done || _framesShown >= _frameCount - 1) {
+ *finished = 1;
+ return 0;
+ }
+
+ *finished = 0;
+ return 0;
+}
+
+void SmushDemoPlayer::pause() {
+ if (!_paused && _audio.data) {
+ _paused = 1;
+ audioPause();
+ }
+}
+
+void SmushDemoPlayer::resume() {
+ if (_paused && _audio.data) {
+ _paused = 0;
+ audioResume();
+ }
+}
+
+void SmushDemoPlayer::getFrameBuffer(const byte **pixels, int *width, int *height) {
+ *pixels = _frameSource;
+ *width = _frameWidth;
+ *height = _frameHeight;
+}
+
+void SmushDemoPlayer::getSize(int *x, int *y) {
+ *x = _positionX;
+ *y = _positionY;
+}
+
+int SmushDemoPlayer::smushPlayStartMovie(const Common::String &name, int flags, int bufferBytes, int width, int height, int offset) {
+ _useDriver = (flags >= 0);
+
+ if (_video->videoStart(width, height, 8))
+ return 1;
+
+ if (!initialize(bufferBytes, width, height)) {
+ _video->videoStop();
+ return 1;
+ }
+
+ if (open(name, flags & 0xFFFFFFF, offset)) {
+ terminate();
+ _video->videoStop();
+ return 1;
+ }
+
+ _playFlags = flags & 0xF0000000;
+ _playing = 1;
+ return 0;
+}
+
+int SmushDemoPlayer::smushPlayUpdateMovie(int *finished) {
+ int frameDecoded;
+ int annotation;
+ int paletteChanged;
+ const uint32 *palette;
+
+ update(&frameDecoded, finished, &annotation, &paletteChanged, &palette);
+ if (!*finished && frameDecoded) {
+ if (paletteChanged)
+ _video->videoSetPalette(palette, 0, 256);
+
+ SmushDemoTarget target;
+ SmushDemoRect rect;
+ _video->videoLock(&target, &rect);
+ smushPlayBlit(&target, &rect);
+ _video->videoUnlock();
+ }
+
+ return 0;
+}
+
+int SmushDemoPlayer::smushPlayStopMovie() {
+ close();
+ terminate();
+ _video->videoStop();
+ _playing = 0;
+ return 0;
+}
+
+int SmushDemoPlayer::smushPlayPauseMovie() {
+ if (_playing) {
+ pause();
+ return 0;
+ }
+
+ return 0;
+}
+
+int SmushDemoPlayer::smushPlayResumeMovie() {
+ if (_playing) {
+ resume();
+ return 0;
+ }
+
+ return 0;
+}
+
+int SmushDemoPlayer::smushPlayBlit(SmushDemoTarget *target, const SmushDemoRect *rect) {
+ int due = getFramesDue();
+ _framesShown = _frame;
+
+ if (target && _frameSource) {
+ if (!(_flags & 8) && due > 1 && _dropCount < 3) {
+ _dropCount++;
+ } else {
+ _dropCount = 0;
+ if (_flags & 1) {
+ _flags &= ~6;
+ if (_frameWidth <= rect->width / 2)
+ _flags |= 4;
+ else if (_frameHeight < rect->height)
+ _flags |= 2;
+ }
+
+ int verticalScale = ((_flags & 6) != 0) + 1;
+ int doubleBoth = _flags & 4;
+ int horizontalScale = (doubleBoth != 0) + 1;
+ int width = _frameWidth * horizontalScale;
+ int height = _frameHeight * verticalScale;
+ int x = _sourceX;
+ int y = _sourceY;
+
+ if (x < rect->x) {
+ width += x - rect->x;
+ x = rect->x;
+ }
+
+ if (x + width > rect->width + rect->x)
+ width = rect->width + rect->x - x;
+
+ if (y < rect->y) {
+ height += y - rect->y;
+ y = rect->y;
+ }
+
+ if (y + height > rect->height + rect->y)
+ height = rect->height + rect->y - y;
+
+ int copyWidth = width / horizontalScale;
+ int copyHeight = height / verticalScale;
+ int alignedX = (x & ~7) * horizontalScale;
+ int alignedWidth = ((copyWidth + x + 7) & ~7) - (x & ~7);
+
+ const byte *source = _frameSource + alignedX + y * _frameWidth * verticalScale;
+ byte *destination = target->pixels + alignedX + y * target->width * verticalScale;
+
+ if (doubleBoth)
+ smushPlayBlitDouble(source, _frameWidth, destination, target->width, alignedWidth, copyHeight);
+ else if (_flags & 2)
+ smushPlayBlitDoubleRows(source, _frameWidth, destination, target->width, alignedWidth, copyHeight);
+ else
+ smushPlayBlitCopy(source, _frameWidth, destination, target->width, alignedWidth, copyHeight);
+ }
+ }
+
+ uint32 dueAfter = (uint32)getFramesDue();
+ if (dueAfter < 2) {
+ if (!dueAfter)
+ _behind = 0;
+ } else {
+ _behind = 1;
+ }
+
+ return 0;
+}
+
+void SmushDemoPlayer::smushPlayBlitDouble(const byte *source, int sourcePitch, byte *destination, int destinationPitch, int width, int rows) {
+ for (int row = 0; row < rows; row++) {
+ const byte *in = source;
+ byte *out = destination;
+
+ for (int i = 0; i < (width >> 2); i++) {
+ byte p0 = in[0];
+ byte p1 = in[1];
+ byte p2 = in[2];
+ byte p3 = in[3];
+
+ out[0] = p0;
+ out[1] = p0;
+ out[2] = p1;
+ out[3] = p1;
+ out[4] = p2;
+ out[5] = p2;
+ out[6] = p3;
+ out[7] = p3;
+ memcpy(out + destinationPitch, out, 8);
+
+ in += 4;
+ out += 8;
+ }
+
+ source += sourcePitch;
+ destination += destinationPitch * 2;
+ }
+}
+
+void SmushDemoPlayer::smushPlayBlitDoubleRows(const byte *source, int sourcePitch, byte *destination, int destinationPitch, int width, int rows) {
+ for (int row = 0; row < rows; row++) {
+ memcpy(destination, source, (width >> 2) * 4);
+ memcpy(destination + destinationPitch, source, (width >> 2) * 4);
+
+ source += sourcePitch;
+ destination += destinationPitch * 2;
+ }
+}
+
+void SmushDemoPlayer::smushPlayBlitCopy(const byte *source, int sourcePitch, byte *destination, int destinationPitch, int width, int rows) {
+ for (int row = 0; row < rows; row++) {
+ memcpy(destination, source, (width >> 2) * 4);
+
+ source += sourcePitch;
+ destination += destinationPitch;
+ }
+}
+
} // End of namespace Grim
diff --git a/engines/grim/movie/smush_anim.h b/engines/grim/movie/smush_anim.h
index a50ab11e0a5..c5eaf37aa6e 100644
--- a/engines/grim/movie/smush_anim.h
+++ b/engines/grim/movie/smush_anim.h
@@ -23,12 +23,129 @@
#define GRIM_MOVIE_SMUSH_ANIM_H
#include "common/scummsys.h"
+#include "common/str.h"
+
+#include "engines/grim/movie/smush_animator.h"
+#include "engines/grim/movie/smush_mixer.h"
namespace Grim {
class SmushTimer;
class SmushSound;
+struct SmushDemoTarget {
+ byte *pixels;
+ int width;
+ int height;
+ int bytesPerPixel;
+};
+
+class SmushDemoVideo {
+public:
+ virtual ~SmushDemoVideo() {}
+
+ virtual int videoStart(int width, int height, int depth) = 0;
+ virtual void videoStop() = 0;
+ virtual void videoLock(SmushDemoTarget *target, SmushDemoRect *rect) = 0;
+ virtual void videoUnlock() = 0;
+ virtual void videoSetPalette(const uint32 *palette, int start, int count) = 0;
+};
+
+class SmushDemoPlayer : public SmushDemoClient {
+
+private:
+ SmushDemoVideo *_video;
+ SmushDemoDriver *_driver;
+ SmushDemoStream *_stream;
+ SmushDemoAnim *_anim;
+ SmushDemoAudio _audio;
+ bool _useDriver;
+
+ int _playing;
+ int _playFlags;
+
+ byte _staging[4096 + 64];
+ uint32 _palette[256];
+ uint32 _paletteSaved[256];
+ uint32 _paletteTarget[256];
+ byte *_frameSource;
+ int _frameWidth;
+ int _frameHeight;
+ int _positionX;
+ int _positionY;
+ int _sourceX;
+ int _sourceY;
+ int _paused;
+ int _done;
+ int _flags;
+ int _lastResult;
+ int _frameReady;
+ int _paletteFlag;
+ int _annotation;
+ int _staged;
+ int _audioPaused;
+ int _audioStartFrame;
+ int _audioClockBase;
+ int _audioClockLast;
+ int _startTime;
+ uint32 _lastUpdate;
+ int _fps;
+ int _frame;
+ int _framesShown;
+ int _frameCount;
+ int _dropCount;
+ int _behind;
+ int _fadeStart;
+ int _fadeSteps;
+ int _fadeRemaining;
+ int _firstUpdate;
+ int _streamStop;
+
+ int audioOpen(int rate, int channels, int bits);
+ void audioClose();
+ int audioPause();
+ int audioResume();
+ void audioFill();
+ int audioGetTime();
+ int getFramesDue();
+ int stepAnimation();
+ void fadePalette();
+ void decodeIACT(const byte *block);
+ void smushPlayBlitDouble(const byte *source, int sourcePitch, byte *destination, int destinationPitch, int width, int rows);
+ void smushPlayBlitDoubleRows(const byte *source, int sourcePitch, byte *destination, int destinationPitch, int width, int rows);
+ void smushPlayBlitCopy(const byte *source, int sourcePitch, byte *destination, int destinationPitch, int width, int rows);
+
+public:
+ SmushDemoPlayer(SmushDemoVideo *video, SmushDemoDriver *driver);
+ ~SmushDemoPlayer();
+
+ int preFrame(int frame, int frameCount) override;
+ int postFrame(int frame, int frameCount) override;
+ int frameCallback(int flags, bool paletteChanged, bool display, int paletteState) override;
+ int chunkCallback(const byte *chunk, int size, int flags) override;
+ void setAnnotation(int value) override;
+ void setFrameRate(int fps) override;
+ void setFrameBuffer(byte *buffer, int x, int y, int sourceX, int sourceY, int width, int height) override;
+
+ int initialize(int bufferBytes, int width, int height);
+ void terminate();
+ int open(const Common::String &name, int flags, int offset);
+ int close();
+ int update(int *frameDecoded, int *finished, int *annotation, int *paletteChanged, const uint32 **palette);
+ void pause();
+ void resume();
+ void getFrameBuffer(const byte **pixels, int *width, int *height);
+ void getSize(int *x, int *y);
+ bool streamStep();
+
+ int smushPlayStartMovie(const Common::String &name, int flags, int bufferBytes, int width, int height, int offset);
+ int smushPlayUpdateMovie(int *finished);
+ int smushPlayStopMovie();
+ int smushPlayPauseMovie();
+ int smushPlayResumeMovie();
+ int smushPlayBlit(SmushDemoTarget *target, const SmushDemoRect *rect);
+};
+
class SmushAnimClient {
public:
virtual ~SmushAnimClient() {}
diff --git a/engines/grim/movie/smush_animator.cpp b/engines/grim/movie/smush_animator.cpp
index b39b9b7c40b..af78bd05618 100644
--- a/engines/grim/movie/smush_animator.cpp
+++ b/engines/grim/movie/smush_animator.cpp
@@ -20,8 +20,11 @@
*/
#include "common/endian.h"
+#include "common/file.h"
#include "common/stream.h"
+#include "common/debug.h"
+#include "engines/grim/movie/codecs/blocky8.h"
#include "engines/grim/movie/codecs/smush_decoder.h"
#include "engines/grim/movie/smush_animator.h"
@@ -257,4 +260,938 @@ int SmushAnimator::animatorRender(int flags) {
}
}
+SmushDemoStream::SmushDemoStream() {
+ _ring = nullptr;
+ _ringSize = 0;
+ _maxRead = 0;
+ _writePos = 0;
+ _readPos = 0;
+ _held = 0;
+ _rewindPos = 0;
+ _alignSkip = 0;
+ _file = nullptr;
+ _fileIsMemory = false;
+ _fileOffset = 0;
+ _fileSize = 0;
+ _busy = 0;
+ _restart = 0;
+ _hasData = 0;
+ _released = 0;
+ _refCount = 0;
+ _openPending = false;
+ _openOffset = 0;
+}
+
+SmushDemoStream::~SmushDemoStream() {
+ delete _file;
+ delete[] _ring;
+}
+
+int SmushDemoStream::streamInitialize(int ringSize, int maxRead) {
+ if (_refCount++ != 0)
+ return 0;
+
+ _ring = new byte[maxRead + ringSize]();
+ _maxRead = maxRead;
+ _ringSize = ringSize & 0xFFFFF800;
+ _file = nullptr;
+ memset(_ring, 0, _ringSize);
+ return 0;
+}
+
+void SmushDemoStream::streamTerminate() {
+ if (--_refCount)
+ return;
+
+ if (_file) {
+ delete _file;
+ _file = nullptr;
+ }
+
+ delete[] _ring;
+ _ring = nullptr;
+}
+
+void SmushDemoStream::streamInitMemory() {
+ _restart = 1;
+ _hasData = 1;
+ _busy = 0;
+ _rewindPos = 0;
+ _alignSkip = 0;
+ _writePos = _ringSize - 1;
+ _readPos = 0;
+ _held = 0;
+ _fileIsMemory = true;
+ _fileOffset = 0;
+ _fileSize = _ringSize - 1;
+}
+
+void SmushDemoStream::streamReset() {
+ _writePos = 0;
+ _readPos = 0;
+ _restart = 0;
+ _hasData = 0;
+ _held = 0;
+ _alignSkip = 0;
+ if (_file) {
+ delete _file;
+ _file = nullptr;
+ }
+
+ _fileIsMemory = false;
+ _busy = 0;
+}
+
+void SmushDemoStream::streamOpen(const Common::String &name, int offset) {
+ _openName = name;
+ _openPending = true;
+ _openOffset = offset;
+}
+
+int SmushDemoStream::streamDoOpen() {
+ if (!_openPending)
+ return -1;
+
+ if (!_openName.empty()) {
+ Common::File *file = new Common::File();
+ if (!file->open(Common::Path(_openName))) {
+ delete file;
+ _openPending = false;
+ return -2;
+ }
+
+ delete _file;
+ _file = file;
+ _fileIsMemory = false;
+ _fileSize = _file->size();
+ }
+
+ _file->seek(_openOffset & 0xFFFFF800);
+ _alignSkip = _openOffset & 0x7FF;
+ _fileOffset = _openOffset & 0xFFFFF800;
+ _rewindPos = _writePos;
+ _restart = 1;
+ _hasData = 0;
+ _openPending = false;
+ return 0;
+}
+
+bool SmushDemoStream::streamIsReady() {
+ return _hasData && _restart;
+}
+
+bool SmushDemoStream::streamHasSpace(int size) {
+ int end = _writePos;
+ if (_readPos > _writePos)
+ end = _writePos + _ringSize;
+
+ return end >= _readPos + size;
+}
+
+bool SmushDemoStream::streamFill(int limit) {
+ bool opened = (streamDoOpen() == 0);
+ int size;
+
+ if (!_file || _fileOffset >= _fileSize)
+ return false;
+
+ if (_readPos > _writePos) {
+ if (_readPos != 0) {
+ size = (_readPos - _writePos - 2048) & 0xFFFFF800;
+ if (size < 2048)
+ return false;
+ } else if (_ringSize - _writePos > 2048) {
+ size = _ringSize - _writePos - 2048;
+ } else {
+ size = (_readPos - _writePos - 2048) & 0xFFFFF800;
+ if (size < 2048)
+ return false;
+ }
+ } else if (_readPos != 0) {
+ size = _ringSize - _writePos;
+ } else if (_ringSize - _writePos > 2048) {
+ size = _ringSize - _writePos - 2048;
+ } else {
+ size = (_readPos - _writePos - 2048) & 0xFFFFF800;
+ if (size < 2048)
+ return false;
+ }
+
+ if (opened) {
+ if (size > _maxRead)
+ size = _maxRead;
+ } else if (size > limit) {
+ size = limit;
+ }
+
+ int toRead = _fileSize - _fileOffset;
+ if (toRead >= size)
+ toRead = size;
+
+ if ((int)_file->read(_ring + _writePos, toRead) == toRead) {
+ int next = _writePos + size;
+ _fileOffset += size;
+ if (next >= _ringSize)
+ next -= _ringSize;
+
+ _writePos = next;
+ if (_restart)
+ _hasData = 1;
+ } else {
+ _file->seek(_fileOffset);
+ }
+
+ return true;
+}
+
+int SmushDemoStream::streamRead(int size, const byte **data) {
+ *data = nullptr;
+
+ if (_busy || (!_file && !_fileIsMemory))
+ return -1;
+
+ int wanted = size;
+ if (size > _maxRead) {
+ warning("File Streamer: Invalid frame size %d", size);
+ return -2;
+ }
+
+ if (!_released) {
+ int next = _readPos + _held;
+ bool wraps = next >= _ringSize;
+ _readPos += _held;
+ _held = 0;
+ if (wraps)
+ _readPos = next - _ringSize;
+
+ _released = 1;
+ }
+
+ if (size < 0) {
+ if (_restart) {
+ _held = 0;
+ _readPos = _rewindPos;
+ }
+
+ _released = 0;
+ return -1;
+ }
+
+ if (_restart) {
+ streamRewind();
+ _released = 0;
+ return 0;
+ }
+
+ if (_alignSkip)
+ wanted = _alignSkip + size;
+
+ if (!streamHasSpace(wanted))
+ return -1;
+
+ int position = _readPos;
+ _held = wanted;
+ _released = 0;
+ int overflow = _readPos + wanted - _ringSize;
+ if (overflow > 0)
+ memcpy(_ring + _ringSize, _ring, overflow);
+
+ if (!_restart && _alignSkip) {
+ position += _alignSkip;
+ _alignSkip = 0;
+ }
+
+ *data = _ring + position;
+ return 1;
+}
+
+int SmushDemoStream::streamRewind() {
+ if (!_restart)
+ return -1;
+
+ _restart = 0;
+ _hasData = 0;
+ _held = 0;
+ _readPos = _rewindPos;
+ return 0;
+}
+
+SmushDemoAnim::SmushDemoAnim(SmushDemoStream *stream, SmushDemoClient *client) {
+ _stream = stream;
+ _client = client;
+ _blocky8 = nullptr;
+ _refCount = 0;
+ _bitmap = nullptr;
+ _rect.x = 0;
+ _rect.y = 0;
+ _rect.width = 0;
+ _rect.height = 0;
+ _block = nullptr;
+ _blockStatus = 0;
+ _frame = 0;
+ _frameCount = 1;
+ _blockSize = 0;
+ _tag = 0;
+ _originX = 0;
+ _originY = 0;
+ _newAnim = 0;
+ _options = 0;
+ _seeking = 0;
+ _skipping = 0;
+ _quit = 0;
+ _pause = 0;
+ _dirty = 0;
+ _skipUntil = 0;
+ _skipBase = 0;
+ _skipLength = 0;
+ _flags = 0;
+ _overlay = nullptr;
+ _preload = nullptr;
+ _overlaySize = 0;
+ _preloadSize = 0;
+ _preloadUsed = 0;
+ _preloadCursor = 8;
+ _preloadLast = 0;
+ _preloadFirst = 0;
+ memset(_updateFlags, 0, sizeof(_updateFlags));
+ memset(_palette, 0, sizeof(_palette));
+ memset(_paletteAccum, 0, sizeof(_paletteAccum));
+ memset(_paletteDelta, 0, sizeof(_paletteDelta));
+ _paletteState = 0;
+ _paletteRefresh = 0;
+ _noDisplay = 0;
+ _displayPending = 1;
+}
+
+SmushDemoAnim::~SmushDemoAnim() {
+ anmUnregisterCodecs();
+ if (_bitmap) {
+ delete[] _bitmap->pixels;
+ delete _bitmap;
+ }
+
+ delete[] _overlay;
+ delete[] _preload;
+}
+
+int SmushDemoAnim::anmInitialize(int width, int height) {
+ if (_refCount++ != 0)
+ return 0;
+
+ animatorInitialize();
+
+ _bitmap = new SmushDemoBitmap();
+ _bitmap->pixels = new byte[width * height]();
+ _bitmap->width = width;
+ _bitmap->height = height;
+ _bitmap->size = width * height;
+ return 0;
+}
+
+int SmushDemoAnim::anmTerminate() {
+ if (--_refCount)
+ return 0;
+
+ if (_bitmap) {
+ delete[] _bitmap->pixels;
+ delete _bitmap;
+ _bitmap = nullptr;
+ }
+
+ return 0;
+}
+
+void SmushDemoAnim::animatorInitialize() {
+ anmClearUpdateFlags(0);
+ _stream->streamReset();
+ _options = 0;
+ _frame = 0;
+ _frameCount = 1;
+ _tag = 0;
+ _originX = 0;
+ _originY = 0;
+ _newAnim = 0;
+ _dirty = 0;
+ _quit = 0;
+ _pause = 0;
+ _seeking = 0;
+ _skipping = 0;
+ animatorResetView();
+}
+
+void SmushDemoAnim::animatorSetOrigin(int x, int y) {
+ _originX = x;
+ _originY = y;
+}
+
+void SmushDemoAnim::animatorResetView() {
+ _rect.x = 0;
+ _rect.y = 0;
+ _rect.width = _bitmap ? _bitmap->width : 320;
+ _rect.height = _bitmap ? _bitmap->height : 200;
+ animatorSetOrigin(0, 0);
+ anmClearUpdateFlags(0);
+ _dirty = 0;
+}
+
+void SmushDemoAnim::animatorNewAnim(const Common::String &name, int options, int skipLength, int skipUntil, int seeking, int offset) {
+ _seeking = seeking;
+ _flags = options & 0xFFFF;
+ _stream->streamOpen(name, offset + 8);
+ _newAnim = 1;
+ animatorSetSkip(skipLength, skipUntil);
+}
+
+void SmushDemoAnim::animatorSetSkip(int skipLength, int skipUntil) {
+ _skipBase = _frame;
+ _skipUntil = skipUntil;
+ _skipLength = skipLength;
+}
+
+int SmushDemoAnim::animatorPause(int mode) {
+ if (mode == 2) {
+ _pause = (_pause == 0);
+ return _pause;
+ }
+
+ if (mode == 4) {
+ if (_pause) {
+ _pause = 3;
+ return 3;
+ }
+ } else if (mode != 5) {
+ _pause = mode;
+ }
+
+ return _pause;
+}
+
+int SmushDemoAnim::animatorReadBlock(int size) {
+ if (_skipping) {
+ _block = anmPreloadGet(size);
+ const byte *discard;
+ _stream->streamRead(-1, &discard);
+ _blockStatus = _block ? 1 : 0;
+ if (!_block) {
+ _skipping = 0;
+ _skipLength = 0;
+ _skipBase = 0;
+ _tag = 'w';
+ }
+ } else {
+ _blockStatus = _stream->streamRead(size, &_block);
+ if (_blockStatus == 0) {
+ _frame = -1;
+ _newAnim = 0;
+ }
+ }
+
+ return _blockStatus;
+}
+
+int SmushDemoAnim::animatorCheckSkip() {
+ if (!_skipping) {
+ if (_newAnim) {
+ if (_skipLength >= 0 && _frame > _skipBase + _skipLength)
+ return 1;
+ } else if (_skipUntil >= 0) {
+ if (_frame < _skipUntil)
+ return 1;
+
+ _skipUntil = -1;
+ }
+ }
+
+ return 0;
+}
+
+int SmushDemoAnim::animatorDecodeBlocks(int x, int y, int flags) {
+ if (_client)
+ _client->preFrame(_frame, _frameCount);
+
+ if (anmDecodeBody(x, y, _block, _blockSize, _options | flags))
+ _quit = 1;
+
+ if (_client)
+ _client->postFrame(_frame, _frameCount);
+ else if (_frame == _frameCount)
+ _quit = 1;
+
+ anmDisplay(_options | flags);
+ if (_dirty)
+ animatorResetView();
+
+ return 0;
+}
+
+void SmushDemoAnim::animatorHandleHeader(const byte *header) {
+ _options = _flags & 0xFFFF;
+ if (!(_flags & 0x400))
+ anmSetPalette(header + 6, 1);
+
+ _frameCount = READ_LE_UINT16(header + 2);
+ if (header[0] > 1) {
+ int fps = READ_LE_UINT16(header + 774);
+ if (fps && !(_options & 8))
+ _client->setFrameRate(fps);
+ }
+
+ if (!(_options & 0x40))
+ animatorResetView();
+}
+
+int SmushDemoAnim::animatorRender(int flags) {
+ if (_quit) {
+ _quit = 0;
+ return -1;
+ }
+
+ while (true) {
+ switch (_tag) {
+ case 0:
+ if (_seeking) {
+ _seeking = 0;
+ _skipping = 1;
+ }
+
+ animatorReadBlock(8);
+ if (_blockStatus == -1)
+ return 11;
+ if (_blockStatus == -2)
+ return -3;
+
+ if (_block) {
+ _tag = _block[0];
+ _blockSize = READ_BE_UINT32(_block + 4);
+ }
+
+ break;
+ case 'A':
+ animatorReadBlock(_blockSize);
+ if (_blockStatus == -1)
+ return 11;
+ if (_blockStatus == -2)
+ return -3;
+
+ _tag = 0;
+ if (_block)
+ animatorHandleHeader(_block);
+
+ break;
+ case 'F':
+ animatorReadBlock(_blockSize);
+ if (_blockStatus == -1)
+ return 11;
+ if (_blockStatus == -2)
+ return -3;
+
+ _tag = 0;
+ if (!_block)
+ break;
+
+ _frame++;
+ if (_skipping || !animatorCheckSkip()) {
+ animatorDecodeBlocks(_originX, _originY, flags);
+ if (_frame < _frameCount - 1 || _skipping) {
+ if (_pause == 1)
+ _tag = 'p';
+ else if (_pause == 3)
+ _pause = 1;
+ } else if (_stream->streamIsReady() || _seeking) {
+ _tag = 0;
+ } else {
+ _tag = 'e';
+ }
+
+ if (_skipping)
+ return 1;
+
+ return _frame >= 10 ? 0 : 2;
+ }
+
+ if (_frame >= _frameCount - 1) {
+ if (_stream->streamIsReady() || _seeking)
+ _tag = 0;
+ else
+ _tag = 'e';
+ }
+
+ break;
+ case 'e':
+ _frame++;
+ if (!animatorCheckSkip())
+ animatorDecodeBlocks(_originX, _originY, flags);
+
+ if (_stream->streamIsReady() || _seeking)
+ _tag = 0;
+
+ return 1;
+ case 'p':
+ animatorDecodeBlocks(_originX, _originY, flags);
+ if (_pause == 1)
+ return 1;
+
+ if (_pause == 3)
+ _pause = 1;
+
+ _tag = 0;
+ return 1;
+ case 'w':
+ if (_stream->streamIsReady() || _seeking)
+ _tag = 0;
+
+ return 1;
+ default:
+ warning("Read invalid frame header -- CD may be dirty or damaged.");
+ return -3;
+ }
+
+ if (_tag > 0x77) {
+ warning("Read invalid frame header -- CD may be dirty or damaged.");
+ return -3;
+ }
+ }
+}
+
+int SmushDemoAnim::anmBuffersInitialize(int overlaySize, int preloadSize) {
+ _overlaySize = overlaySize;
+ _preloadSize = preloadSize;
+ anmClearUpdateFlags(0);
+
+ if (overlaySize)
+ _overlay = new byte[overlaySize]();
+
+ if (preloadSize)
+ _preload = new byte[preloadSize]();
+
+ return 0;
+}
+
+void SmushDemoAnim::anmBuffersTerminate() {
+ if (_overlaySize) {
+ delete[] _overlay;
+ _overlay = nullptr;
+ }
+
+ if (_preloadSize) {
+ delete[] _preload;
+ _preload = nullptr;
+ }
+}
+
+void SmushDemoAnim::anmClearUpdateFlags(int index) {
+ if (index > 0) {
+ int bit = index - 1;
+ if ((bit & 0xF8) < 1024)
+ _updateFlags[bit >> 3] &= ~(128 >> (bit & 7));
+ } else {
+ memset(_updateFlags, 0, sizeof(_updateFlags));
+ }
+}
+
+bool SmushDemoAnim::anmTestUpdateFlag(int index) {
+ int bit = index - 1;
+ return (bit & 0xF8) < 1024 && (_updateFlags[bit >> 3] & (128 >> (bit & 7))) != 0;
+}
+
+void SmushDemoAnim::anmPreloadPut(const byte *chunk, int size) {
+ int part = READ_LE_UINT16(chunk + 2);
+ int first = READ_LE_UINT16(chunk);
+ int used;
+
+ if (part) {
+ used = _preloadUsed;
+ } else {
+ used = 0;
+ _preloadCursor = 8;
+ _preloadUsed = 0;
+ _preloadLast = -1;
+ _preloadFirst = first;
+ }
+
+ if (part == _preloadLast + 1) {
+ if (size + used < _preloadSize) {
+ memcpy(_preload + used, chunk + 10, size - 10);
+ _preloadUsed += size - 10;
+ }
+
+ _preloadLast = part;
+ }
+}
+
+const byte *SmushDemoAnim::anmPreloadGet(int size) {
+ int next = size + _preloadCursor;
+ if (next > _preloadUsed) {
+ _preloadCursor = 8;
+ return nullptr;
+ }
+
+ _preloadCursor += size;
+ return _preload + next - size;
+}
+
+void SmushDemoAnim::anmDisplay(int flags) {
+ if (!_displayPending)
+ return;
+
+ _displayPending = 0;
+ bool display = !_noDisplay && (flags & 0x8000) == 0;
+ _client->frameCallback(flags, _paletteState >= 2, display, _paletteState);
+ if (_paletteState) {
+ _paletteRefresh = 1;
+ _paletteState = 0;
+ }
+}
+
+void SmushDemoAnim::anmSetPalette(const byte *palette, int mode) {
+ if (palette != _palette)
+ memcpy(_palette, palette, 768);
+
+ _paletteState = (mode != 0) + 1;
+}
+
+void SmushDemoAnim::anmStoreObject(byte *destination, const byte *chunk) {
+ int size = READ_BE_UINT32(chunk + 4) + 8;
+ if (size < _overlaySize)
+ memcpy(destination, chunk, size);
+}
+
+void SmushDemoAnim::anmDrawObject(int x, int y, const byte *object, int flags) {
+ anmFrameObject(x, y, flags, object + 8, READ_BE_UINT32(object + 4));
+}
+
+int SmushDemoAnim::anmFrameObject(int x, int y, int flags, const byte *chunk, int size) {
+ int drawX;
+ int drawY;
+
+ if (flags & 1) {
+ drawX = x;
+ drawY = y;
+ } else {
+ drawX = (int16)(READ_LE_UINT16(chunk + 2) + x);
+ drawY = (int16)(READ_LE_UINT16(chunk + 4) + y);
+ }
+
+ int width = (int16)READ_LE_UINT16(chunk + 6);
+ int height = (int16)READ_LE_UINT16(chunk + 8);
+ if (flags & 2) {
+ drawX -= width >> 1;
+ drawY -= height >> 1;
+ }
+
+ if (chunk[0] != 47 || !_blocky8)
+ return -1;
+
+ _blocky8->init(width, height);
+ if (!_blocky8->decode(_bitmap->pixels, chunk + 14, flags))
+ return 1;
+
+ int clipX = drawX;
+ int clipY = drawY;
+ int sourceX = 0;
+ int sourceY = 0;
+ int clipWidth = width;
+ int clipHeight = height;
+
+ if (_rect.x > drawX) {
+ clipX = _rect.x;
+ sourceX = _rect.x - drawX;
+ clipWidth = width + drawX - _rect.x;
+ }
+
+ if (clipWidth + clipX > _rect.width + _rect.x)
+ clipWidth = _rect.x + _rect.width - clipX;
+
+ if (_rect.y > drawY) {
+ clipY = _rect.y;
+ sourceY = _rect.y - drawY;
+ clipHeight = drawY + height - _rect.y;
+ }
+
+ if (clipHeight + clipY > _rect.height + _rect.y)
+ clipHeight = _rect.y + _rect.height - clipY;
+
+ _client->setFrameBuffer(_bitmap->pixels, clipX, clipY, sourceX, sourceY, clipWidth, clipHeight);
+ return 0;
+}
+
+int SmushDemoAnim::anmRegisterCodecs(int width, int height) {
+ _blocky8 = new Blocky8();
+ _blocky8->init(width, height);
+ return 0;
+}
+
+void SmushDemoAnim::anmUnregisterCodecs() {
+ delete _blocky8;
+ _blocky8 = nullptr;
+}
+
+int SmushDemoAnim::anmDecodeBody(int x, int y, const byte *block, int size, int flags) {
+ _displayPending = 1;
+ int handled = 0;
+ bool skipNext = false;
+ bool storePending = false;
+ uint32 previousTag = 0;
+ const byte *lastObject = nullptr;
+ const byte *start = block;
+ _noDisplay = 0;
+
+ while (size > 4) {
+ handled = 0;
+
+ if (((block - start) & 1) != 0 && !*block) {
+ block++;
+ size--;
+ }
+
+ if (storePending && _overlay) {
+ if (READ_BE_UINT32(block) != MKTAG('I', 'A', 'C', 'T')) {
+ anmStoreObject(_overlay, block);
+ anmDrawObject(x, y, _overlay, flags);
+ storePending = false;
+ handled = 1;
+ }
+ }
+
+ uint32 tag = READ_BE_UINT32(block);
+ uint32 chunkSize = READ_BE_UINT32(block + 4);
+ const byte *payload = block + 8;
+
+ if (skipNext) {
+ skipNext = false;
+ } else {
+ switch (tag) {
+ case MKTAG('F', 'T', 'C', 'H'):
+ if (_overlay) {
+ int offsetX;
+ int offsetY;
+ if (chunkSize == 12) {
+ offsetX = READ_BE_UINT16(payload + 6);
+ offsetY = READ_BE_UINT16(payload + 10);
+ } else {
+ offsetX = (int16)READ_LE_UINT16(payload + 2);
+ offsetY = (int16)READ_LE_UINT16(payload + 4);
+ }
+
+ anmDrawObject(x + offsetX, y + offsetY, _overlay, flags);
+ }
+
+ break;
+ case MKTAG('F', 'O', 'B', 'J'):
+ lastObject = payload;
+ if (handled)
+ break;
+
+ if (previousTag == MKTAG('S', 'K', 'I', 'P')) {
+ if ((uint16)anmFrameObject(x, y, flags, payload, chunkSize))
+ _noDisplay = 1;
+ } else if ((int16)anmFrameObject(x, y, flags, payload, chunkSize) > 0) {
+ _noDisplay = 1;
+ }
+
+ break;
+ case MKTAG('G', 'O', 'S', 'T'): {
+ int mode = 0;
+ if (READ_LE_UINT16(payload) == 0)
+ mode = 0x2000;
+ else if (READ_LE_UINT16(payload) == 1)
+ mode = 0x4000;
+ else if (READ_LE_UINT16(payload) == 2)
+ mode = 0x6000;
+
+ int offsetX = (int16)READ_LE_UINT16(payload + 2);
+ int offsetY = (int16)READ_LE_UINT16(payload + 4);
+ anmFrameObject(x + offsetX, y + offsetY, flags | mode, lastObject, chunkSize);
+ break;
+ }
+ case MKTAG('I', 'A', 'C', 'T'):
+ _client->chunkCallback(payload, chunkSize, flags);
+ break;
+ case MKTAG('N', 'P', 'A', 'L'):
+ if (animatorPause(5) != 1)
+ anmSetPalette(payload, 1);
+
+ break;
+ case MKTAG('L', 'O', 'A', 'D'):
+ anmPreloadPut(payload, chunkSize);
+ break;
+ case MKTAG('S', 'A', 'U', 'D'):
+ case MKTAG('P', 'S', 'A', 'D'):
+ break;
+ case MKTAG('S', 'T', 'O', 'R'):
+ storePending = true;
+ break;
+ case MKTAG('S', 'K', 'I', 'P'): {
+ int second = READ_LE_UINT16(payload + 2);
+ int first = READ_LE_UINT16(payload);
+ if (second)
+ skipNext = anmTestUpdateFlag(first) != anmTestUpdateFlag(second);
+ else
+ skipNext = anmTestUpdateFlag(first) != 0;
+
+ break;
+ }
+ case MKTAG('V', 'M', 'S', 'K'):
+ if (animatorPause(5) != 1)
+ _noDisplay = 1;
+
+ break;
+ case MKTAG('X', 'P', 'A', 'L'): {
+ int mode = READ_LE_UINT16(payload + 2);
+ if (mode == 0 || mode == 512) {
+ if (mode == 512)
+ memcpy(_palette, payload + 1540, 768);
+
+ for (int i = 0; i < 768; i++) {
+ _paletteAccum[i] = _palette[i] << 7;
+ _paletteDelta[i] = (int16)READ_LE_UINT16(payload + 4 + i * 2);
+ }
+ } else if (animatorPause(5) != 1) {
+ for (int i = 0; i < 768; i++) {
+ _paletteAccum[i] += _paletteDelta[i];
+ _palette[i] = _paletteAccum[i] >> 7;
+ }
+
+ _paletteRefresh = 1;
+ _paletteState = 1;
+ }
+
+ break;
+ }
+ case MKTAG('T', 'R', 'E', 'S'):
+ case MKTAG('T', 'E', 'X', 'T'):
+ if (flags & 0x200) {
+ const byte pseudo[2] = { 9, 0 };
+ _client->chunkCallback(pseudo, chunkSize, flags);
+ } else if (tag == MKTAG('T', 'R', 'E', 'S')) {
+ _client->setAnnotation((int16)READ_LE_UINT16(payload + 16));
+ }
+
+ break;
+ default: {
+ bool printable = true;
+ for (int i = 0; i < 4; i++) {
+ byte c = (tag >> (i * 8)) & 0xFF;
+ if (c < 0x30 || c > 0x5A)
+ printable = false;
+ }
+
+ if (!printable) {
+ warning("Bad IFF Header found %x at %x", tag, (uint32)(block - start));
+ return -1;
+ }
+
+ break;
+ }
+ }
+ }
+
+ previousTag = tag;
+ block = payload + chunkSize;
+ size -= 8 + chunkSize;
+ }
+
+ return 0;
+}
+
} // End of namespace Grim
diff --git a/engines/grim/movie/smush_animator.h b/engines/grim/movie/smush_animator.h
index e4aabf6f556..02204cbdc6d 100644
--- a/engines/grim/movie/smush_animator.h
+++ b/engines/grim/movie/smush_animator.h
@@ -23,10 +23,169 @@
#define GRIM_MOVIE_SMUSH_ANIMATOR_H
#include "common/scummsys.h"
+#include "common/str.h"
+
+namespace Common {
+class SeekableReadStream;
+}
namespace Grim {
class SmushDecoder;
+class Blocky8;
+
+struct SmushDemoBitmap {
+ byte *pixels;
+ int width;
+ int height;
+ int size;
+};
+
+struct SmushDemoRect {
+ int x;
+ int y;
+ int width;
+ int height;
+};
+
+class SmushDemoClient {
+public:
+ virtual ~SmushDemoClient() {}
+
+ virtual int preFrame(int frame, int frameCount) = 0;
+ virtual int postFrame(int frame, int frameCount) = 0;
+ virtual int frameCallback(int flags, bool paletteChanged, bool display, int paletteState) = 0;
+ virtual int chunkCallback(const byte *chunk, int size, int flags) = 0;
+ virtual void setAnnotation(int value) = 0;
+ virtual void setFrameRate(int fps) = 0;
+ virtual void setFrameBuffer(byte *buffer, int x, int y, int sourceX, int sourceY, int width, int height) = 0;
+};
+
+class SmushDemoStream {
+private:
+ byte *_ring;
+ int _ringSize;
+ int _maxRead;
+ int _writePos;
+ int _readPos;
+ int _held;
+ int _rewindPos;
+ int _alignSkip;
+ Common::SeekableReadStream *_file;
+ bool _fileIsMemory;
+ int _fileOffset;
+ int _fileSize;
+ int _busy;
+ int _restart;
+ int _hasData;
+ int _released;
+ int _refCount;
+ bool _openPending;
+ int _openOffset;
+ Common::String _openName;
+
+ bool streamHasSpace(int size);
+ int streamDoOpen();
+
+public:
+ SmushDemoStream();
+ ~SmushDemoStream();
+
+ int streamInitialize(int ringSize, int maxRead);
+ void streamTerminate();
+ void streamInitMemory();
+ void streamReset();
+ void streamOpen(const Common::String &name, int offset);
+ bool streamFill(int limit);
+ int streamRead(int size, const byte **data);
+ int streamRewind();
+ bool streamIsReady();
+};
+
+class SmushDemoAnim {
+private:
+ SmushDemoStream *_stream;
+ SmushDemoClient *_client;
+ Blocky8 *_blocky8;
+
+ int _refCount;
+ SmushDemoBitmap *_bitmap;
+ SmushDemoRect _rect;
+ const byte *_block;
+ int _blockStatus;
+ int _frame;
+ int _frameCount;
+ int _blockSize;
+ int _tag;
+ int _originX;
+ int _originY;
+ int _newAnim;
+ int _options;
+ int _seeking;
+ int _skipping;
+ int _quit;
+ int _pause;
+ int _dirty;
+ int _skipUntil;
+ int _skipBase;
+ int _skipLength;
+ int _flags;
+
+ byte *_overlay;
+ byte *_preload;
+ int _overlaySize;
+ int _preloadSize;
+ int _preloadUsed;
+ int _preloadCursor;
+ int _preloadLast;
+ int _preloadFirst;
+ byte _updateFlags[128];
+
+ byte _palette[768];
+ uint16 _paletteAccum[768];
+ uint16 _paletteDelta[768];
+ int _paletteState;
+ int _paletteRefresh;
+ int _noDisplay;
+ int _displayPending;
+
+ int animatorReadBlock(int size);
+ int animatorCheckSkip();
+ int animatorDecodeBlocks(int x, int y, int flags);
+ void animatorHandleHeader(const byte *header);
+ int anmFrameObject(int x, int y, int flags, const byte *chunk, int size);
+ void anmStoreObject(byte *destination, const byte *chunk);
+ void anmDrawObject(int x, int y, const byte *object, int flags);
+ void anmPreloadPut(const byte *chunk, int size);
+ bool anmTestUpdateFlag(int index);
+
+public:
+ SmushDemoAnim(SmushDemoStream *stream, SmushDemoClient *client);
+ ~SmushDemoAnim();
+
+ int anmInitialize(int width, int height);
+ int anmTerminate();
+ void animatorInitialize();
+ int animatorRender(int flags);
+ void animatorSetOrigin(int x, int y);
+ void animatorNewAnim(const Common::String &name, int options, int skipLength, int skipUntil, int seeking, int offset);
+ void animatorSetSkip(int skipLength, int skipUntil);
+ void animatorResetView();
+ int animatorPause(int mode);
+ int anmBuffersInitialize(int overlaySize, int preloadSize);
+ void anmBuffersTerminate();
+ void anmClearUpdateFlags(int index);
+ int anmDecodeBody(int x, int y, const byte *block, int size, int flags);
+ const byte *anmPreloadGet(int size);
+ void anmDisplay(int flags);
+ void anmSetPalette(const byte *palette, int mode);
+ const byte *anmGetPalette() const { return _palette; }
+ int anmRegisterCodecs(int width, int height);
+ void anmUnregisterCodecs();
+ const SmushDemoBitmap *getBitmap() const { return _bitmap; }
+ int getFrame() const { return _frame; }
+ int getFrameCount() const { return _frameCount; }
+};
class SmushAnimator {
diff --git a/engines/grim/movie/smush_mixer.cpp b/engines/grim/movie/smush_mixer.cpp
index 80ddaaba27f..518a22ccbee 100644
--- a/engines/grim/movie/smush_mixer.cpp
+++ b/engines/grim/movie/smush_mixer.cpp
@@ -846,4 +846,244 @@ int SmushDriver::ringRead(int16 *buffer, int numSamples) {
return numSamples;
}
+SmushDemoDriverStream::SmushDemoDriverStream(SmushDemoDriver *driver) {
+ _driver = driver;
+}
+
+int SmushDemoDriverStream::readBuffer(int16 *buffer, const int numSamples) {
+ return _driver->ringRead(buffer, numSamples);
+}
+
+SmushDemoDriver::SmushDemoDriver() {
+ _system = g_system->getMixer();
+ _buffer = nullptr;
+ _bufferSize = 0;
+ _playPosition = 0;
+ _playing = false;
+ _created = false;
+ _rate = 0;
+ _blockAlign = 0;
+ _written = 0;
+ _wrapCount = 0;
+ _lastPosition = 0;
+ _lastTime = 0;
+ _lastMeasure = 0;
+ _lastReturned = 0;
+ _waiting = 0;
+ _paused = 0;
+ _threshold = 0;
+ _msPerByte = 0.0;
+}
+
+SmushDemoDriver::~SmushDemoDriver() {
+ _system->stopHandle(_soundHandle);
+
+ Common::StackLock lock(_mutex);
+ delete[] _buffer;
+ _buffer = nullptr;
+}
+
+void SmushDemoDriver::sndDrivCreate() {
+ _created = true;
+}
+
+void SmushDemoDriver::sndDrivRelease() {
+ _created = false;
+}
+
+int SmushDemoDriver::sndDrivOpen(int rate, int channels, int bits, SmushDemoAudio *audio) {
+ sndDrivCreate();
+
+ _rate = rate;
+ audio->data = new byte[audio->size]();
+
+ _blockAlign = (bits >> 3) * channels;
+ _bufferSize = 5000 * _rate * _blockAlign / 1000;
+
+ {
+ Common::StackLock lock(_mutex);
+ _buffer = new byte[_bufferSize]();
+ _playPosition = 0;
+ _playing = false;
+ }
+
+ _system->playStream(Audio::Mixer::kSFXSoundType, &_soundHandle, new SmushDemoDriverStream(this), -1, Audio::Mixer::kMaxChannelVolume, 0, DisposeAfterUse::YES);
+ audio->opened = true;
+
+ _written = 0;
+ _wrapCount = 0;
+ _lastPosition = 0;
+ _lastMeasure = g_system->getMillis();
+ _lastTime = 0;
+ _lastReturned = 0;
+ _waiting = 1;
+ _paused = 0;
+ _msPerByte = 1000.0 / (double)(_rate * _blockAlign);
+ _threshold = 1000 * _rate * _blockAlign / 1000;
+ return 0;
+}
+
+void SmushDemoDriver::sndDrivClose(SmushDemoAudio *audio) {
+ if (!audio->opened)
+ return;
+
+ _system->stopHandle(_soundHandle);
+ {
+ Common::StackLock lock(_mutex);
+ _playing = false;
+ delete[] _buffer;
+ _buffer = nullptr;
+ }
+
+ audio->opened = false;
+ if (audio->data) {
+ delete[] audio->data;
+ audio->data = nullptr;
+ }
+
+ sndDrivRelease();
+}
+
+int SmushDemoDriver::sndDrivGetTime(SmushDemoAudio *audio) {
+ int position;
+ {
+ Common::StackLock lock(_mutex);
+ position = _playPosition;
+ }
+
+ uint32 time = g_system->getMillis();
+ int result;
+
+ if (position != _lastPosition) {
+ if (position < _lastPosition)
+ _wrapCount++;
+
+ int played = position + _wrapCount * _bufferSize;
+ _lastTime = (int)((double)played * _msPerByte);
+ _lastMeasure = time;
+ _lastPosition = position;
+
+ if (!_waiting) {
+ if (_written < played + 500 * _rate * _blockAlign / 1000) {
+ Common::StackLock lock(_mutex);
+ _playing = false;
+ _waiting = 1;
+ _threshold = played + 500 * _rate * _blockAlign / 1000;
+ }
+ }
+ }
+
+ if (_waiting || _paused)
+ result = _lastTime;
+ else
+ result = _lastTime + (int)(time - _lastMeasure);
+
+ if (result <= _lastReturned)
+ result = _lastReturned;
+
+ _lastReturned = result;
+ return result;
+}
+
+int SmushDemoDriver::sndDrivPause(SmushDemoAudio *audio) {
+ {
+ Common::StackLock lock(_mutex);
+ _playing = false;
+ }
+
+ _paused = 1;
+ return 0;
+}
+
+int SmushDemoDriver::sndDrivResume(SmushDemoAudio *audio) {
+ _paused = 0;
+ _lastMeasure = g_system->getMillis();
+
+ Common::StackLock lock(_mutex);
+ _playing = true;
+ _playPosition = _lastPosition;
+ return 0;
+}
+
+void SmushDemoDriver::sndDrivFill(SmushDemoAudio *audio) {
+ sndDrivGetTime(audio);
+
+ int ringSize = _bufferSize;
+ int playedAbsolute = _lastPosition + ringSize * _wrapCount;
+ int available = audio->produced - audio->consumed - 1024;
+ int space = playedAbsolute - _written + ringSize - 4096;
+ if (space >= available)
+ space = available;
+
+ if (space <= 0)
+ return;
+
+ int offset = _written % ringSize;
+ int firstLength = MIN(space, ringSize - offset);
+ int secondLength = space - firstLength;
+ int remaining = space;
+ int segmentRemaining = firstLength;
+ int destination = offset;
+
+ {
+ Common::StackLock lock(_mutex);
+
+ while (remaining > 0) {
+ int source = audio->consumed % audio->size;
+ int toEnd = audio->size - source;
+ int chunk = MIN(remaining, toEnd);
+ chunk = MIN(chunk, segmentRemaining);
+
+ memcpy(_buffer + destination, audio->data + source, chunk);
+ remaining -= chunk;
+ segmentRemaining -= chunk;
+ audio->consumed += chunk;
+
+ if (segmentRemaining == 0) {
+ segmentRemaining = secondLength;
+ destination = 0;
+ } else {
+ destination += chunk;
+ }
+ }
+ }
+
+ _written += firstLength + secondLength;
+
+ if (_waiting) {
+ if (_written > _threshold) {
+ {
+ Common::StackLock lock(_mutex);
+ _playing = true;
+ _playPosition = _lastPosition;
+ }
+
+ _waiting = 0;
+ _lastMeasure = g_system->getMillis();
+ }
+ }
+}
+
+int SmushDemoDriver::ringRead(int16 *buffer, int numSamples) {
+ Common::StackLock lock(_mutex);
+ if (!_buffer || !_playing) {
+ memset(buffer, 0, numSamples * 2);
+ return numSamples;
+ }
+
+ byte *dest = (byte *)buffer;
+ int remaining = numSamples * 2;
+
+ while (remaining > 0) {
+ int chunk = MIN(remaining, _bufferSize - _playPosition);
+ memcpy(dest, _buffer + _playPosition, chunk);
+
+ dest += chunk;
+ remaining -= chunk;
+ _playPosition = (_playPosition + chunk) % _bufferSize;
+ }
+
+ return numSamples;
+}
+
} // End of namespace Grim
diff --git a/engines/grim/movie/smush_mixer.h b/engines/grim/movie/smush_mixer.h
index e6fd74afc79..69b904f7e0c 100644
--- a/engines/grim/movie/smush_mixer.h
+++ b/engines/grim/movie/smush_mixer.h
@@ -124,5 +124,71 @@ public:
};
+struct SmushDemoAudio {
+ byte *data;
+ int produced;
+ int consumed;
+ int size;
+ bool opened;
+};
+
+class SmushDemoDriver;
+
+class SmushDemoDriverStream : public Audio::AudioStream {
+
+private:
+ SmushDemoDriver *_driver;
+
+public:
+ SmushDemoDriverStream(SmushDemoDriver *driver);
+
+ int readBuffer(int16 *buffer, const int numSamples) override;
+ bool isStereo() const override { return true; }
+ int getRate() const override { return 22050; }
+ bool endOfData() const override { return false; }
+
+};
+
+class SmushDemoDriver {
+
+private:
+ Audio::Mixer *_system;
+ Audio::SoundHandle _soundHandle;
+ Common::Mutex _mutex;
+ byte *_buffer;
+ int _bufferSize;
+ int _playPosition;
+ bool _playing;
+ bool _created;
+ int _rate;
+ int _blockAlign;
+ int _written;
+ int _wrapCount;
+ int _lastPosition;
+ int _lastTime;
+ uint32 _lastMeasure;
+ int _lastReturned;
+ int _waiting;
+ int _paused;
+ int _threshold;
+ double _msPerByte;
+
+public:
+ SmushDemoDriver();
+ ~SmushDemoDriver();
+
+ void sndDrivCreate();
+ void sndDrivRelease();
+ int sndDrivOpen(int rate, int channels, int bits, SmushDemoAudio *audio);
+ void sndDrivClose(SmushDemoAudio *audio);
+ int sndDrivGetTime(SmushDemoAudio *audio);
+ int sndDrivPause(SmushDemoAudio *audio);
+ int sndDrivResume(SmushDemoAudio *audio);
+ void sndDrivFill(SmushDemoAudio *audio);
+
+ int ringRead(int16 *buffer, int numSamples);
+
+};
+
} // End of namespace Grim
#endif
Commit: ab8a5921e3684615badbf65dd7440df86d944478
https://github.com/scummvm/scummvm/commit/ab8a5921e3684615badbf65dd7440df86d944478
Author: AndywinXp (andywinxp at gmail.com)
Date: 2026-11-01T22:18:07+01:00
Commit Message:
GRIM: SMUSH: Clean-up unused code and re-wire up Codec48
Changed paths:
engines/grim/movie/codecs/blocky8.h
engines/grim/movie/codecs/codec48.cpp
engines/grim/movie/codecs/codec48.h
engines/grim/movie/codecs/smush_decoder.cpp
engines/grim/movie/codecs/smush_decoder.h
engines/grim/movie/smush.cpp
engines/grim/movie/smush_animator.cpp
engines/grim/movie/smush_animator.h
diff --git a/engines/grim/movie/codecs/blocky8.h b/engines/grim/movie/codecs/blocky8.h
index f681c7f4123..5a3225c07bd 100644
--- a/engines/grim/movie/codecs/blocky8.h
+++ b/engines/grim/movie/codecs/blocky8.h
@@ -56,7 +56,7 @@ public:
~Blocky8();
void init(int width, int height);
void deinit();
- bool decode(byte *dst, const byte *src, int flags = 0);
+ bool decode(byte *dst, const byte *src, int flags);
};
} // End of namespace Grim
diff --git a/engines/grim/movie/codecs/codec48.cpp b/engines/grim/movie/codecs/codec48.cpp
index 710c6b1e474..81de683b1d3 100644
--- a/engines/grim/movie/codecs/codec48.cpp
+++ b/engines/grim/movie/codecs/codec48.cpp
@@ -24,64 +24,89 @@
#include "common/textconsole.h"
#include "engines/grim/movie/codecs/codec48.h"
+#include "engines/grim/movie/smush_animator.h"
namespace Grim {
Codec48Decoder::Codec48Decoder() {
- _frameSize = 640 * 480; // Yes, this is correct. Looks like the buffers are always this size
-
+ _initialized = false;
_curBuf = 0;
- _deltaBuf[0] = new byte[_frameSize * 2];
- _deltaBuf[1] = _deltaBuf[0] + _frameSize;
+ _buffer = nullptr;
+ _deltaBuf[0] = nullptr;
+ _deltaBuf[1] = nullptr;
+ _width = 0;
+ _height = 0;
+ _blockX = 0;
+ _blockY = 0;
+ _lastBlockX = 10000;
+ _lastBlockY = 10000;
+ _pitch = 0;
_offsetTable = new int16[255];
_tableLastPitch = -1;
_tableLastIndex = -1;
+ _prevSeqNb = 0;
- _interTable = nullptr;
+ _interTable = new byte[65536]();
}
-void Codec48Decoder::init(int width, int height) {
- if (_width == width && _height == height)
- return;
- deinit();
+int Codec48Decoder::init(int width, int height) {
+ _buffer = new byte[width * height * 2]();
+ _deltaBuf[0] = _buffer;
+ _deltaBuf[1] = _buffer + width * height;
_width = width;
_height = height;
-
- _blockX = (_width + 7) / 8;
- _blockY = (_height + 7) / 8;
- _pitch = _blockX * 8;
-
- // don't support when this is not equal yet
- assert(_width == _pitch);
+ _curBuf = 0;
+ _initialized = true;
+ return 0;
}
void Codec48Decoder::deinit() {
+ delete[] _buffer;
+ _buffer = nullptr;
+ _initialized = false;
}
Codec48Decoder::~Codec48Decoder() {
- delete[] _deltaBuf[0];
+ deinit();
delete[] _offsetTable;
delete[] _interTable;
}
-bool Codec48Decoder::decode(byte *dst, const byte *src) {
- // The header is identical to codec 37, except the flags field is somewhat different
+int Codec48Decoder::decode(const SmushDemoBitmap *bitmap, const SmushDemoRect *rect, int x, int y, const byte *chunk, int flags, SmushDemoClient *client) {
+ if (!_initialized)
+ return -1;
- const byte *gfxData = src + 0x10;
+ // The header is identical to codec 37, except the flags field is somewhat different
+ const byte *header = chunk + 14;
+ const byte *gfxData = header + 0x10;
+
+ int width = (int16)READ_LE_UINT16(chunk + 6);
+ int height = (int16)READ_LE_UINT16(chunk + 8);
+ if (width > _width)
+ return -1;
+ if (height > _height)
+ return -1;
+
+ _blockX = (width + 7) / 8;
+ _blockY = (height + 7) / 8;
+ _pitch = _blockX * 8;
- makeTable(_pitch, src[1]);
+ makeTable(_pitch, header[1]);
- int16 seqNb = READ_LE_UINT16(src + 2);
+ int16 seqNb = READ_LE_UINT16(header + 2);
- if (seqNb == 0)
- memset(_deltaBuf[0], 0, _frameSize * 2);
+ if (seqNb == 0) {
+ if (_blockX < _lastBlockX || _blockY < _lastBlockY) {
+ memset(_deltaBuf[0], 0, _height * _width);
+ memset(_deltaBuf[1], 0, _height * _width);
+ }
+ _lastBlockX = _blockX;
+ _lastBlockY = _blockY;
+ }
- if (src[12] & (1 << 3)) {
+ if (header[12] & (1 << 3)) {
// Interpolation table present
- if (!_interTable)
- _interTable = new byte[65536];
-
byte *ptr = _interTable;
for (int i = 0; i < 256; i++) {
@@ -99,45 +124,129 @@ bool Codec48Decoder::decode(byte *dst, const byte *src) {
}
}
- switch (src[0]) {
+ switch (header[0]) {
case 0:
// Raw frame
- memcpy(_deltaBuf[_curBuf], gfxData, READ_LE_UINT32(src + 4));
+ memcpy(_deltaBuf[_curBuf], gfxData, READ_LE_UINT32(header + 4));
break;
case 2:
// Blast object
- bompDecodeLine(_deltaBuf[_curBuf], gfxData, _width * _height);
+ bompDecodeLine(_deltaBuf[_curBuf], gfxData, READ_LE_UINT32(header + 4));
break;
case 3:
// 8x8 block encoding
if (!(seqNb && seqNb != _prevSeqNb + 1)) {
- if (seqNb & 1 || !(src[12] & 1) || src[12] & 0x10)
+ if (seqNb & 1 || !(header[12] & 1) || header[12] & 0x10)
_curBuf ^= 1;
+ if ((seqNb & 1) && (flags & 0x10) && (header[12] & 1) && !(header[12] & 0x30)) {
+ _prevSeqNb = seqNb;
+ return 1;
+ }
+
decode3(_deltaBuf[_curBuf], gfxData, _deltaBuf[_curBuf ^ 1] - _deltaBuf[_curBuf]);
}
break;
case 5:
- // Some other encoding, but it's unused. (Good)
- warning("SmushDecoder::decode() codec 48 frame type 5 encountered! Please report!");
+ // Frame stored at half size, scaled up with the interpolation table
+ decode5(_deltaBuf[_curBuf], gfxData, _blockX * 8, _blockY * 8);
break;
default:
- warning("SmushDecoder::decode() Unknown codec 48 frame type %d", src[0]);
break;
}
_prevSeqNb = seqNb;
- memcpy(dst, _deltaBuf[_curBuf], _pitch * _height);
- return true;
+
+ int sourceX = 0;
+ int sourceY = 0;
+
+ if (rect->x > x) {
+ sourceX = rect->x - x;
+ width += x - rect->x;
+ x = rect->x;
+ }
+
+ if (x + width > rect->x + rect->width)
+ width = rect->x + rect->width - x;
+
+ if (rect->y > y) {
+ sourceY = rect->y - y;
+ height += y - rect->y;
+ y = rect->y;
+ }
+
+ if (y + height > rect->y + rect->height)
+ height = rect->y + rect->height - y;
+
+ if (header[12] & 2)
+ return 0;
+
+ if (header[12] & 0x10) {
+ drawBlended(bitmap->pixels, bitmap->width, _deltaBuf[_curBuf ^ 1], _deltaBuf[_curBuf], x, y, sourceX, sourceY, width, height, _pitch);
+ return 0;
+ }
+
+ client->setFrameBuffer(_deltaBuf[_curBuf], x, y, sourceX, sourceY, width, height);
+ return 0;
+}
+
+void Codec48Decoder::drawBlended(byte *bitmap, int bitmapPitch, const byte *previous, const byte *current, int x, int y, int sourceX, int sourceY, int width, int height, int sourcePitch) {
+ int offset = sourceY * sourcePitch + sourceX;
+ blendFrames(bitmap + y * bitmapPitch + x, previous + offset, current + offset, width, height, bitmapPitch, sourcePitch);
+}
+
+void Codec48Decoder::blendFrames(byte *dst, const byte *previous, const byte *current, int width, int height, int dstPitch, int sourcePitch) {
+ for (int row = 0; row < height; row++) {
+ for (int col = 0; col < width; col++)
+ dst[col] = _interTable[previous[col] | (current[col] << 8)];
+
+ dst += dstPitch;
+ previous += sourcePitch;
+ current += sourcePitch;
+ }
+}
+
+void Codec48Decoder::decode5(byte *dst, const byte *src, int width, int height) {
+ byte *out = dst + width;
+ int rows = height;
+
+ do {
+ byte pixel = *src++;
+ out[0] = pixel;
+ out[1] = pixel;
+ out += 2;
+
+ int count = width - 2;
+ do {
+ byte previous = pixel;
+ pixel = *src++;
+ out[0] = _interTable[(previous << 8) | pixel];
+ out[1] = pixel;
+ out += 2;
+ count -= 2;
+ } while (count > 0);
+
+ out += width;
+ rows -= 2;
+ } while (rows > 0);
+
+ memcpy(dst, dst + width, width);
+
+ out = dst + width * 2;
+ rows = height - 2;
+ do {
+ for (int i = 0; i < width; i++)
+ out[i] = _interTable[out[i - width] | (out[i + width] << 8)];
+
+ out += width * 2;
+ rows -= 2;
+ } while (rows > 0);
}
void Codec48Decoder::bompDecodeLine(byte *dst, const byte *src, int len) {
- while (len > 0) {
+ do {
byte code = *src++;
- byte num = (code >> 1) + 1;
-
- if (num > len)
- num = len;
+ int num = (code >> 1) + 1;
len -= num;
@@ -150,7 +259,7 @@ void Codec48Decoder::bompDecodeLine(byte *dst, const byte *src, int len) {
}
dst += num;
- }
+ } while (len != 0);
}
void Codec48Decoder::makeTable(int pitch, int index) {
@@ -300,12 +409,12 @@ void Codec48Decoder::decode3(byte *dst, const byte *src, int bufOffset) {
scaleBuffer[4] = _interTable[(dst[_pitch * 2 - 1] << 8) | scaleBuffer[5]];
scaleBuffer[6] = _interTable[(scaleBuffer[7] << 8) | scaleBuffer[5]];
- scaleBuffer[9] = _interTable[(dst[_pitch * 3 - 1] << 8) | scaleBuffer[11]];
- scaleBuffer[8] = _interTable[(dst[_pitch * 3 - 1] << 8) | scaleBuffer[9]];
+ scaleBuffer[9] = _interTable[(dst[_pitch * 4 - 1] << 8) | scaleBuffer[11]];
+ scaleBuffer[8] = _interTable[(dst[_pitch * 4 - 1] << 8) | scaleBuffer[9]];
scaleBuffer[10] = _interTable[(scaleBuffer[11] << 8) | scaleBuffer[9]];
- scaleBuffer[13] = _interTable[(dst[_pitch * 4 - 1] << 8) | scaleBuffer[15]];
- scaleBuffer[12] = _interTable[(dst[_pitch * 4 - 1] << 8) | scaleBuffer[13]];
+ scaleBuffer[13] = _interTable[(dst[_pitch * 6 - 1] << 8) | scaleBuffer[15]];
+ scaleBuffer[12] = _interTable[(dst[_pitch * 6 - 1] << 8) | scaleBuffer[13]];
scaleBuffer[14] = _interTable[(scaleBuffer[15] << 8) | scaleBuffer[13]];
scaleBlock(dst, scaleBuffer);
@@ -334,9 +443,9 @@ void Codec48Decoder::decode3(byte *dst, const byte *src, int bufOffset) {
scaleBuffer[4] = _interTable[(dst[_pitch * 2 - 1] << 8) | scaleBuffer[5]];
scaleBuffer[6] = _interTable[(scaleBuffer[7] << 8) | scaleBuffer[5]];
- scaleBuffer[8] = _interTable[(dst[_pitch * 3 - 1] << 8) | scaleBuffer[9]];
+ scaleBuffer[8] = _interTable[(dst[_pitch * 4 - 1] << 8) | scaleBuffer[9]];
scaleBuffer[10] = _interTable[(scaleBuffer[11] << 8) | scaleBuffer[9]];
- scaleBuffer[12] = _interTable[(dst[_pitch * 4 - 1] << 8) | scaleBuffer[13]];
+ scaleBuffer[12] = _interTable[(dst[_pitch * 6 - 1] << 8) | scaleBuffer[13]];
scaleBuffer[14] = _interTable[(scaleBuffer[15] << 8) | scaleBuffer[13]];
scaleBlock(dst, scaleBuffer);
diff --git a/engines/grim/movie/codecs/codec48.h b/engines/grim/movie/codecs/codec48.h
index 21900cbd96e..42275e1b541 100644
--- a/engines/grim/movie/codecs/codec48.h
+++ b/engines/grim/movie/codecs/codec48.h
@@ -26,31 +26,40 @@
namespace Grim {
+struct SmushDemoBitmap;
+struct SmushDemoRect;
+class SmushDemoClient;
+
class Codec48Decoder {
public:
Codec48Decoder();
~Codec48Decoder();
- void init(int width, int height);
+ int init(int width, int height);
void deinit();
- bool decode(byte *dst, const byte *src);
+ int decode(const SmushDemoBitmap *bitmap, const SmushDemoRect *rect, int x, int y, const byte *chunk, int flags, SmushDemoClient *client);
private:
void makeTable(int pitch, int index);
+ void drawBlended(byte *bitmap, int bitmapPitch, const byte *previous, const byte *current, int x, int y, int sourceX, int sourceY, int width, int height, int sourcePitch);
+ void blendFrames(byte *dst, const byte *previous, const byte *current, int width, int height, int dstPitch, int sourcePitch);
void bompDecodeLine(byte *dst, const byte *src, int len);
void decode3(byte *dst, const byte *src, int bufOffset);
+ void decode5(byte *dst, const byte *src, int width, int height);
void scaleBlock(byte *dst, const byte *src);
void copyBlock(byte *dst, int deltaBufOffset, int offset);
+ bool _initialized;
int _curBuf;
+ byte *_buffer;
byte *_deltaBuf[2];
int _blockX, _blockY;
+ int _lastBlockX, _lastBlockY;
int _pitch;
int16 *_offsetTable;
int _tableLastPitch, _tableLastIndex;
int16 _prevSeqNb;
- int32 _frameSize;
int _width, _height;
byte *_interTable;
};
diff --git a/engines/grim/movie/codecs/smush_decoder.cpp b/engines/grim/movie/codecs/smush_decoder.cpp
index 7d5798d18a5..5e13b48c5c4 100644
--- a/engines/grim/movie/codecs/smush_decoder.cpp
+++ b/engines/grim/movie/codecs/smush_decoder.cpp
@@ -20,214 +20,91 @@
*/
#include "common/endian.h"
-#include "common/events.h"
-#include "common/file.h"
#include "common/rational.h"
-#include "common/system.h"
-#include "common/timer.h"
#include "common/memstream.h"
-#include "audio/audiostream.h"
-#include "audio/mixer.h"
-#include "audio/decoders/raw.h"
-
#include "engines/grim/debug.h"
-#include "engines/grim/movie/codecs/codec48.h"
-#include "engines/grim/movie/codecs/blocky8.h"
#include "engines/grim/movie/codecs/blocky16.h"
#include "engines/grim/movie/codecs/smush_decoder.h"
-#include "engines/grim/movie/codecs/vima.h"
#include "engines/grim/movie/smush_wave.h"
namespace Grim {
-#define ANNO_HEADER "MakeAnim animation type 'Bl16' parameters: "
-#define BUFFER_SIZE 16385
-#define SMUSH_SPEED 66667
-
-bool SmushDecoder::_demo = false;
-
-static uint16 smushDestTable[5786];
-
SmushDecoder::SmushDecoder() {
_file = nullptr;
- _videoLooping = false;
_startPos = 0;
- _frames = nullptr;
_videoTrack = nullptr;
- _audioTrack = nullptr;
_wave = nullptr;
_blocky16Frames = 0;
_frameDelay = 0;
- _videoPause = false;
}
SmushDecoder::~SmushDecoder() {
delete _videoTrack;
- delete _audioTrack;
- delete[] _frames;
}
void SmushDecoder::init() {
_videoTrack->init();
- if (_audioTrack)
- _audioTrack->init();
-}
-
-void SmushDecoder::initFrames() {
- delete[] _frames;
- _frames = new Frame[_videoTrack->getFrameCount()];
-
- int seekPos = _file->pos();
- int curFrame = -1;
- _file->seek(_startPos, SEEK_SET);
- while (curFrame < _videoTrack->getFrameCount() - 1) {
- Frame &frame = _frames[++curFrame];
- frame.frame = curFrame;
- frame.pos = _file->pos();
- frame.keyframe = false;
-
- uint32 tag = _file->readUint32BE();
- uint32 size;
- if (tag == MKTAG('A', 'N', 'N', 'O')) {
- size = _file->readUint32BE();
- _file->seek(size, SEEK_CUR);
- tag = _file->readUint32BE();
- }
- assert(tag == MKTAG('F', 'R', 'M', 'E'));
- size = _file->readUint32BE();
-
- while (size > 0) {
- uint32 subType = _file->readUint32BE();
- uint32 subSize = _file->readUint32BE();
- int32 subPos = _file->pos();
-
- if (subType == MKTAG('B', 'l', '1', '6')) {
- _file->seek(18, SEEK_CUR);
- if (_file->readByte() == 0) {
- frame.keyframe = true;
- }
- }
-
- size -= subSize + 8 + (subSize & 1);
- _file->seek(subPos + subSize + (subSize & 1), SEEK_SET);
- }
-
- _file->seek(size, SEEK_CUR);
- }
-
- _file->seek(seekPos, SEEK_SET);
}
void SmushDecoder::close() {
VideoDecoder::close();
- _audioTrack = nullptr;
_videoTrack = nullptr;
- _videoLooping = false;
_startPos = 0;
- delete[] _frames;
- _frames = nullptr;
if (_file) {
delete _file;
_file = nullptr;
}
}
-
bool SmushDecoder::readHeader() {
if (!_file) {
return false;
}
uint32 mainTag = _file->readUint32BE();
- uint32 pos = _file->pos();
- uint32 expectedTag = 0;
uint32 size = _file->readUint32BE(); // file-size
- // Verify that we have the correct combination of headers.
- if (mainTag == MKTAG('A', 'N', 'I', 'M')) { // Demo
- expectedTag = MKTAG('A', 'H', 'D', 'R');
- } else if (mainTag == MKTAG('S', 'A', 'N', 'M')) { // Retail
- expectedTag = MKTAG('S', 'H', 'D', 'R');
- } else {
+ if (mainTag != MKTAG('S', 'A', 'N', 'M'))
error("Invalid SMUSH-header");
- }
uint32 tag = _file->readUint32BE();
size = _file->readUint32BE();
- pos = _file->pos();
-
- assert(tag == expectedTag);
- (void)expectedTag;
-
- if (tag == MKTAG('A', 'H', 'D', 'R')) { // Demo
- uint32 version = _file->readUint16LE();
- uint16 nbFrames = _file->readUint16LE();
- _file->readUint16BE(); // unknown
-
- int width = -1;
- int height = -1;
- _videoLooping = false;
- _startPos = 0;
-
- _videoTrack = new SmushVideoTrack(width, height, SMUSH_SPEED, nbFrames, false);
- _videoTrack->_x = -1;
- _videoTrack->_y = -1;
- addTrack(_videoTrack);
- _file->read(_videoTrack->getPal(), 0x300);
-
- int audioRate = 11025;
- if (version == 2) {
-
- _file->readUint32LE(); // framerate
- _file->readUint32LE();
- audioRate = _file->readUint32LE();
- }
+ uint32 pos = _file->pos();
- _file->readUint32BE();
- _file->readUint32BE();
- _audioTrack = new SmushAudioTrack(getSoundType(), false, audioRate, 2);
- addTrack(_audioTrack);
- return true;
-
- } else if (tag == MKTAG('S', 'H', 'D', 'R')) { // Retail
- _file->readUint16LE();
- uint16 nbFrames = _file->readUint32LE();
- _file->readUint16LE();
- int width = _file->readUint16LE();
- int height = _file->readUint16LE();
- _file->readUint16LE();
- int frameRate = _file->readUint32LE();
- _frameDelay = frameRate;
-
- int16 flags = _file->readUint16LE();
- // Output information for checking out the flags
- if (Debug::isChannelEnabled(Debug::Movie | Debug::Info)) {
- warning("SMUSH Flags:");
- for (int i = 0; i < 16; i++) {
- warning(" %d", (flags & (1 << i)) != 0);
- }
+ assert(tag == MKTAG('S', 'H', 'D', 'R'));
+
+ _file->readUint16LE();
+ uint16 nbFrames = _file->readUint32LE();
+ _file->readUint16LE();
+ int width = _file->readUint16LE();
+ int height = _file->readUint16LE();
+ _file->readUint16LE();
+ int frameRate = _file->readUint32LE();
+ _frameDelay = frameRate;
+
+ int16 flags = _file->readUint16LE();
+ // Output information for checking out the flags
+ if (Debug::isChannelEnabled(Debug::Movie | Debug::Info)) {
+ warning("SMUSH Flags:");
+ for (int i = 0; i < 16; i++) {
+ warning(" %d", (flags & (1 << i)) != 0);
}
+ }
- _file->seek(pos + size + (size & 1), SEEK_SET);
+ _file->seek(pos + size + (size & 1), SEEK_SET);
- _videoLooping = true;
- // If the video is NOT looping, setLooping will set the speed to the proper value
- _videoTrack = new SmushVideoTrack(width, height, frameRate, nbFrames, true);
- addTrack(_videoTrack);
- return handleFramesHeader();
- }
- return false;
+ _videoTrack = new SmushVideoTrack(width, height, frameRate, nbFrames);
+ addTrack(_videoTrack);
+ return handleFramesHeader();
}
bool SmushDecoder::handleFramesHeader() {
uint32 tag;
int32 size;
int pos = 0;
- int freq = 0;
- int channels = 0;
tag = _file->readUint32BE();
if (tag != MKTAG('F', 'L', 'H', 'D')) {
@@ -241,11 +118,7 @@ bool SmushDecoder::handleFramesHeader() {
if (READ_BE_UINT32(f_header + pos) == MKTAG('B', 'l', '1', '6')) {
pos += READ_BE_UINT32(f_header + pos + 4) + 8;
} else if (READ_BE_UINT32(f_header + pos) == MKTAG('W', 'a', 'v', 'e')) {
- freq = READ_LE_UINT32(f_header + pos + 8);
- channels = READ_LE_UINT32(f_header + pos + 12);
- if (_wave)
- _wave->flobdecDecodeHeader(f_header + pos + 8);
-
+ _wave->flobdecDecodeHeader(f_header + pos + 8);
pos += 20;
} else {
error("SmushDecoder::handleFramesHeader() unknown tag");
@@ -253,11 +126,6 @@ bool SmushDecoder::handleFramesHeader() {
} while (pos < size);
delete[] f_header;
- if (!_wave) {
- _audioTrack = new SmushAudioTrack(getSoundType(), true, freq, channels);
- addTrack(_audioTrack);
- }
-
return true;
}
@@ -279,120 +147,6 @@ bool SmushDecoder::loadStream(Common::SeekableReadStream *stream) {
return true;
}
-const Graphics::Surface *SmushDecoder::decodeNextFrame() {
- handleFrame();
-
- // We might be interested in getting the last frame even after the video ends:
- if (endOfVideo()) {
- return _videoTrack->decodeNextFrame();
- }
- return VideoDecoder::decodeNextFrame();
-}
-
-void SmushDecoder::setLooping(bool l) {
- _videoLooping = l;
-
- if (!_videoLooping) {
- _videoTrack->setMsPerFrame(SMUSH_SPEED);
- }
-}
-
-void SmushDecoder::handleFrame() {
- uint32 tag;
- int32 size;
-
- if (isPaused()) {
- return;
- }
-
- if (_videoTrack->endOfTrack()) { // Looping is handled outside, by rewinding the video.
- _audioTrack->stop(); // HACK: Avoids the movie playing past the last frame
- // pauseVideo(true);
- return;
- }
-
- tag = _file->readUint32BE();
- size = _file->readUint32BE();
- if (tag == MKTAG('A', 'N', 'N', 'O')) {
- char *anno;
- byte *data;
-
- data = new byte[size];
- _file->read(data, size);
- anno = (char *)data;
- if (strncmp(anno, ANNO_HEADER, sizeof(ANNO_HEADER) - 1) == 0) {
- //char *annoData = anno + sizeof(ANNO_HEADER);
-
- // Examples:
- // Water streaming around boat from Manny's balcony
- // MakeAnim animation type 'Bl16' parameters: 10000;12000;100;1;0;0;0;0;25;0;
- // Water in front of the Blue Casket
- // MakeAnim animation type 'Bl16' parameters: 20000;25000;100;1;0;0;0;0;25;0;
- // Scrimshaw exterior:
- // MakeAnim animation type 'Bl16' parameters: 6000;8000;100;0;0;0;0;0;2;0;
- // Lola engine room (loops a limited number of times?):
- // MakeAnim animation type 'Bl16' parameters: 6000;8000;90;1;0;0;0;0;2;0;
- Debug::debug(Debug::Movie, "Announcement data: %s\n", anno);
- // It looks like the announcement data is actually for setting some of the
- // header parameters, not for any looping purpose
- } else {
- Debug::debug(Debug::Movie, "Announcement header not understood: %s\n", anno);
- }
- delete[] anno;
- tag = _file->readUint32BE();
- size = _file->readUint32BE();
- }
-
- assert(tag == MKTAG('F', 'R', 'M', 'E'));
- handleFRME(_file, size);
-
- _videoTrack->finishFrame();
-}
-
-void SmushDecoder::handleFRME(Common::SeekableReadStream *stream, uint32 size) {
- int blockSize = size;
-
- byte *block = new byte[size];
- stream->read(block, size);
-
- Common::MemoryReadStream *memStream = new Common::MemoryReadStream(block, size, DisposeAfterUse::NO);
- while (size > 0) {
- uint32 subType = memStream->readUint32BE();
- uint32 subSize = memStream->readUint32BE();
- uint32 subPos = memStream->pos();
-
- switch (subType) {
- // Retail only:
- case MKTAG('B', 'l', '1', '6'):
- _videoTrack->handleBlocky16(memStream, subSize);
- _blocky16Frames++;
- break;
- case MKTAG('W', 'a', 'v', 'e'):
- if (_wave)
- _wave->flobdecDecodeBody(block + subPos);
- else
- _audioTrack->handleVIMA(memStream, blockSize);
- break;
- // Demo only:
- case MKTAG('F', 'O', 'B', 'J'):
- _videoTrack->handleFrameObject(memStream, subSize);
- break;
- case MKTAG('I', 'A', 'C', 'T'):
- _audioTrack->handleIACT(memStream, subSize);
- break;
- case MKTAG('X', 'P', 'A', 'L'):
- _videoTrack->handleDeltaPalette(memStream, subSize);
- break;
- default:
- Debug::error(Debug::Movie, "SmushDecoder::handleFrame() unknown tag");
- }
- size -= subSize + 8 + (subSize & 1);
- memStream->seek(subPos + subSize + (subSize & 1), SEEK_SET);
- }
- delete memStream;
- delete[] block;
-}
-
void SmushDecoder::decodeFrameBlock(const byte *block, uint32 size, int flags) {
Common::MemoryReadStream *memStream = new Common::MemoryReadStream(block, size, DisposeAfterUse::NO);
@@ -427,137 +181,45 @@ const Graphics::Surface *SmushDecoder::getFrameSurface() {
}
bool SmushDecoder::rewind() {
- if (_wave)
- _wave->flobdecReset();
+ _wave->flobdecReset();
return seekToFrame(0);
}
bool SmushDecoder::seekIntern(const Audio::Timestamp &time) {
- int32 wantedFrame = (uint32)((time.msecs() / 1000.0f) * _videoTrack->getFrameRate().toDouble());
- if (wantedFrame != 0) {
- Debug::debug(Debug::Movie, "Seek to time: %d, frame: %d", time.msecs(), wantedFrame);
- Debug::debug(Debug::Movie, "Current frame: %d", _videoTrack->getCurFrame());
- }
-
- if (wantedFrame > _videoTrack->getFrameCount()) {
- return false;
- }
-
- if (!_frames) {
- initFrames();
- }
-
- // Track down the keyframe
- int keyframe = 0;
- for (int i = wantedFrame; i >= 0; --i) {
- if (_frames[i].keyframe) {
- keyframe = i;
- break;
- }
- }
- _videoTrack->setFrameStart(keyframe);
-
- // VIMA frames are 50 frames ahead of time, so we have to make sure we have 50 frames
- // of audio before the wantedFrame. Here we use 51 to have a bit of safe margin
- if (wantedFrame - keyframe < 51) {
- keyframe = wantedFrame - 51;
- }
- if (keyframe < 0) {
- keyframe = 0;
- }
-
- _file->seek(_frames[keyframe].pos, SEEK_SET);
- _videoTrack->setCurFrame(keyframe - 1);
-
- while (_videoTrack->getCurFrame() < wantedFrame - 1) {
- decodeNextFrame();
- }
-
- // As said, VIMA is 50 frames ahead of time. Every frame it pushes 1470 samples, and 50 * 1470 = 73500.
- // The first frame, instead of 1470, it pushes 73500 samples to have this 50-frames-time.
- // So if we have used frame 0 as keyframe we can remove safely time * rate samples, and we will
- // still have the 50 frames margin. If we have used a later frame as keyframe we don't have the 73500
- // samples pushed the first frame, so we have to be careful not to remove too much data,
- // otherwise the audio will start at a later point. (72030 == 73500 - 1470)
- int offset = (keyframe == 0 ? 0 : 72030);
-
- // Skip decoded audio between the keyframe and the target frame
- Audio::Timestamp delay = 0;
- if (_videoTrack->getCurFrame() > 0) {
- delay = _videoTrack->getFrameTime(_videoTrack->getCurFrame());
- }
- if (keyframe > 0) {
- delay = delay - _videoTrack->getFrameTime(keyframe);
- }
-
- if (_audioTrack) {
- int32 sampleCount = (delay.msecs() / 1000.f) * _audioTrack->getRate() - offset;
- _audioTrack->skipSamples(sampleCount);
- }
+ _file->seek(_startPos, SEEK_SET);
+ _videoTrack->setCurFrame(-1);
VideoDecoder::seekIntern(time);
return true;
}
-SmushDecoder::SmushVideoTrack::SmushVideoTrack(int width, int height, int fps, int numFrames, bool is16Bit) {
- if (!is16Bit) { // Demo
- _format = Graphics::PixelFormat::createFormatCLUT8();
- _codec48 = new Codec48Decoder();
- _blocky8 = new Blocky8();
- _blocky16 = nullptr;
- } else {
- _format = Graphics::PixelFormat(2, 5, 6, 5, 0, 11, 5, 0, 0);
- _codec48 = nullptr;
- _blocky8 = nullptr;
- _blocky16 = new Blocky16();
- _blocky16->init(width, height);
- }
+SmushDecoder::SmushVideoTrack::SmushVideoTrack(int width, int height, int fps, int numFrames) {
+ _format = Graphics::PixelFormat(2, 5, 6, 5, 0, 11, 5, 0, 0);
+ _blocky16 = new Blocky16();
+ _blocky16->init(width, height);
_width = width;
_height = height;
_nbframes = numFrames;
- _is16Bit = is16Bit;
- _x = 0;
- _y = 0;
- setMsPerFrame(fps);
+ _frameRate = Common::Rational(1000000, fps);
_curFrame = 0;
- for (int i = 0; i < 0x300; i++) {
- _palette[i] = 0;
- _deltaPal[i] = 0;
- _dirtyPalette = false;
- }
- _frameStart = 0;
}
SmushDecoder::SmushVideoTrack::~SmushVideoTrack() {
- delete _codec48;
- delete _blocky8;
delete _blocky16;
_surface.free();
}
void SmushDecoder::SmushVideoTrack::init() {
_curFrame = -1;
- _frameStart = -1;
- if (_is16Bit) { // Retail only
- _surface.create(_width, _height, _format);
- }
+ _surface.create(_width, _height, _format);
}
void SmushDecoder::SmushVideoTrack::finishFrame() {
_curFrame++;
}
-void SmushDecoder::SmushVideoTrack::setFrameStart(int frame) {
- _frameStart = frame - 1;
-}
-
void SmushDecoder::SmushVideoTrack::handleBlocky16(Common::SeekableReadStream *stream, uint32 size, int flags) {
- if (_curFrame < _frameStart) {
- return;
- }
-
- assert(_is16Bit);
byte *ptr = new byte[size];
stream->read(ptr, size);
@@ -565,229 +227,8 @@ void SmushDecoder::SmushVideoTrack::handleBlocky16(Common::SeekableReadStream *s
delete[] ptr;
}
-void SmushDecoder::SmushVideoTrack::handleFrameObject(Common::SeekableReadStream *stream, uint32 size) {
- if (_curFrame < _frameStart) {
- return;
- }
-
- assert(!_is16Bit);
- assert(size >= 14);
- byte codec = stream->readByte();
- assert(codec == 47 || codec == 48);
- /* byte codecParam = */ stream->readByte();
- _x = stream->readSint16LE();
- _y = stream->readSint16LE();
- uint16 width = stream->readUint16LE();
- uint16 height = stream->readUint16LE();
- if (width != _width || height != _height) {
- _width = width;
- _height = height;
- _surface.create(_width, _height, _format);
- _codec48->init(_width, _height);
- _blocky8->init(_width, _height);
- }
- stream->readUint16LE();
- stream->readUint16LE();
-
- size -= 14;
- byte *ptr = new byte[size];
- stream->read(ptr, size);
-
- if (codec == 47) {
- _blocky8->decode((byte *)_surface.getPixels(), ptr);
- } else if (codec == 48) {
- _codec48->decode((byte *)_surface.getPixels(), ptr);
- }
- delete[] ptr;
-}
-
-static byte delta_color(byte org_color, int16 delta_color) {
- int t = (org_color * 129 + delta_color) / 128;
- return CLIP(t, 0, 255);
-}
-
-void SmushDecoder::SmushVideoTrack::handleDeltaPalette(Common::SeekableReadStream *stream, int32 size) {
- if (size == 0x300 * 3 + 4) {
- stream->seek(4, SEEK_CUR);
- for (int i = 0; i < 0x300; i++) {
- _deltaPal[i] = stream->readUint16LE();
- }
- stream->read(_palette, 0x300);
- _dirtyPalette = true;
- } else if (size == 6) {
- for (int i = 0; i < 0x300; i++) {
- _palette[i] = delta_color(_palette[i], _deltaPal[i]);
- }
- _dirtyPalette = true;
- } else {
- error("SmushDecoder::handleDeltaPalette() Wrong size for DeltaPalette");
- }
-}
-
Graphics::Surface *SmushDecoder::SmushVideoTrack::decodeNextFrame() {
return &_surface;
}
-void SmushDecoder::SmushVideoTrack::setMsPerFrame(int ms) {
- _frameRate = Common::Rational(1000000, ms);
-}
-SmushDecoder::SmushAudioTrack::SmushAudioTrack(Audio::Mixer::SoundType soundType, bool isVima, int freq, int channels) :
- AudioTrack(soundType) {
- _isVima = isVima;
- _channels = channels;
- _freq = freq;
- _queueStream = Audio::makeQueuingAudioStream(_freq, (_channels == 2));
- _IACTpos = 0;
-}
-
-SmushDecoder::SmushAudioTrack::~SmushAudioTrack() {
- delete _queueStream;
-}
-
-void SmushDecoder::SmushAudioTrack::init() {
- _IACTpos = 0;
-
- if (_isVima) {
- vimaInit(smushDestTable);
- }
-}
-
-void SmushDecoder::SmushAudioTrack::handleVIMA(Common::SeekableReadStream *stream, uint32 size) {
- if (size < 8)
- return;
- int decompressedSize = stream->readUint32BE();
- if (decompressedSize == MKTAG('P', 'S', 'A', 'D')) {
- decompressedSize = stream->readUint32BE();
- if (decompressedSize > (int)size - 8)
- decompressedSize = size - 8;
- if (decompressedSize < 10)
- return;
- stream->skip(10);
- decompressedSize -= 10;
- byte *src = (byte *)malloc(decompressedSize);
- stream->read(src, decompressedSize);
-
- int flags = Audio::FLAG_16BITS | Audio::FLAG_LITTLE_ENDIAN;
- if (_channels == 2) {
- flags |= Audio::FLAG_STEREO;
- }
-
- if (!_queueStream) {
- _queueStream = Audio::makeQueuingAudioStream(_freq, (_channels == 2));
- }
- _queueStream->queueBuffer(src, decompressedSize, DisposeAfterUse::YES, flags);
-
- return;
- }
- if (decompressedSize < 0) {
- stream->readUint32BE();
- decompressedSize = stream->readUint32BE();
- }
-
- byte *src = new byte[size];
- stream->read(src, size);
-
- // this will be deleted using free() by the stream, so allocate it using malloc().
- int16 *dst = (int16 *)malloc(decompressedSize * _channels * 2);
- decompressVima(src, dst, decompressedSize * _channels * 2, smushDestTable, true);
-
- int flags = Audio::FLAG_16BITS | Audio::FLAG_LITTLE_ENDIAN;
- if (_channels == 2) {
- flags |= Audio::FLAG_STEREO;
- }
-
- if (!_queueStream) {
- _queueStream = Audio::makeQueuingAudioStream(_freq, (_channels == 2));
- }
- _queueStream->queueBuffer((byte *)dst, decompressedSize * _channels * 2, DisposeAfterUse::YES, flags);
- delete[] src;
-}
-
-void SmushDecoder::SmushAudioTrack::handleIACT(Common::SeekableReadStream *stream, int32 size) {
- byte *src = new byte[size];
- stream->read(src, size);
-
- int32 bsize = size - 18;
- const byte *d_src = src + 18;
-
- while (bsize > 0) {
- if (_IACTpos >= 2) {
- int32 len = READ_BE_UINT16(_IACToutput) + 2;
- len -= _IACTpos;
- if (len > bsize) {
- memcpy(_IACToutput + _IACTpos, d_src, bsize);
- _IACTpos += bsize;
- bsize = 0;
- } else {
- // this will be deleted using free() by the stream, so allocate it using malloc().
- byte *output_data = (byte *)malloc(4096);
- memcpy(_IACToutput + _IACTpos, d_src, len);
- byte *dst = output_data;
- byte *d_src2 = _IACToutput;
- d_src2 += 2;
- int32 count = 1024;
- byte variable1 = *d_src2++;
- byte variable2 = variable1 / 16;
- variable1 &= 0x0f;
- do {
- byte value;
- value = *(d_src2++);
- if (value == 0x80) {
- *dst++ = *d_src2++;
- *dst++ = *d_src2++;
- } else {
- int16 val = (int8)value << variable2;
- *dst++ = val >> 8;
- *dst++ = (byte)(val);
- }
- value = *(d_src2++);
- if (value == 0x80) {
- *dst++ = *d_src2++;
- *dst++ = *d_src2++;
- } else {
- int16 val = (int8)value << variable1;
- *dst++ = val >> 8;
- *dst++ = (byte)(val);
- }
- } while (--count);
-
- if (!_queueStream) {
- _queueStream = Audio::makeQueuingAudioStream(22050, true);
- }
- _queueStream->queueBuffer(output_data, 0x1000, DisposeAfterUse::YES, Audio::FLAG_STEREO | Audio::FLAG_16BITS);
-
- bsize -= len;
- d_src += len;
- _IACTpos = 0;
- }
- } else {
- if (bsize > 1 && _IACTpos == 0) {
- *(_IACToutput + 0) = *d_src++;
- _IACTpos = 1;
- bsize--;
- }
- *(_IACToutput + _IACTpos) = *d_src++;
- _IACTpos++;
- bsize--;
- }
- }
- delete[] src;
-}
-
-bool SmushDecoder::SmushAudioTrack::seek(const Audio::Timestamp &time) {
- return true;
-}
-
-void SmushDecoder::SmushAudioTrack::skipSamples(int sampleCount) {
- if (sampleCount <= 0)
- return;
-
- if (_queueStream->isStereo())
- sampleCount *= 2;
-
- int16 *tempBuffer = new int16[sampleCount];
- _queueStream->readBuffer(tempBuffer, sampleCount);
- delete[] tempBuffer;
-}
-
} // end of namespace Grim
diff --git a/engines/grim/movie/codecs/smush_decoder.h b/engines/grim/movie/codecs/smush_decoder.h
index 87c2005a558..34bc7ee0608 100644
--- a/engines/grim/movie/codecs/smush_decoder.h
+++ b/engines/grim/movie/codecs/smush_decoder.h
@@ -22,20 +22,14 @@
#ifndef GRIM_SMUSH_DECODER_H
#define GRIM_SMUSH_DECODER_H
-#include "audio/audiostream.h"
+#include "common/stream.h"
#include "video/video_decoder.h"
#include "graphics/surface.h"
-namespace Audio {
-class QueuingAudioStream;
-}
-
namespace Grim {
-class Codec48Decoder;
-class Blocky8;
class Blocky16;
class SmushWave;
@@ -44,9 +38,6 @@ public:
SmushDecoder();
~SmushDecoder();
- int getX() const { return _videoTrack->_x; }
- int getY() const { return _videoTrack->_y; }
- void setLooping(bool l);
void setWave(SmushWave *wave) { _wave = wave; }
int getFrameDelay() const { return _frameDelay; }
Common::SeekableReadStream *getFile() { return _file; }
@@ -62,16 +53,12 @@ public:
protected:
bool readHeader();
- void handleFrameDemo();
- void handleFrame();
bool handleFramesHeader();
- void handleFRME(Common::SeekableReadStream *stream, uint32 size);
void init();
void close() override;
- const Graphics::Surface *decodeNextFrame() override;
class SmushVideoTrack : public FixedRateVideoTrack {
public:
- SmushVideoTrack(int width, int height, int fps, int numFrames, bool is16Bit);
+ SmushVideoTrack(int width, int height, int fps, int numFrames);
~SmushVideoTrack();
uint16 getWidth() const override { return _width; }
@@ -81,67 +68,26 @@ protected:
void setCurFrame(int frame) { _curFrame = frame; }
int getFrameCount() const override { return _nbframes; }
Common::Rational getFrameRate() const override { return _frameRate; }
- void setMsPerFrame(int ms);
void finishFrame();
bool isSeekable() const override { return true; }
bool seek(const Audio::Timestamp &time) override { return true; }
- void setFrameStart(int frame);
- void handleBlocky16(Common::SeekableReadStream *stream, uint32 size, int flags = 0);
- void handleFrameObject(Common::SeekableReadStream *stream, uint32 size);
- void handleDeltaPalette(Common::SeekableReadStream *stream, int32 size);
+ void handleBlocky16(Common::SeekableReadStream *stream, uint32 size, int flags);
void init();
Graphics::Surface *decodeNextFrame() override;
- const byte *getPalette() const override { _dirtyPalette = false; return _palette; }
- bool hasDirtyPalette() const override { return _dirtyPalette; }
- byte *getPal() { _dirtyPalette = true; return _palette; }
- int _x, _y;
private:
- void convertDemoFrame();
- bool _is16Bit;
int32 _curFrame;
- byte _palette[0x300];
- int16 _deltaPal[0x300];
- mutable bool _dirtyPalette;
int _width, _height;
Graphics::Surface _surface;
Graphics::PixelFormat _format;
Common::Rational _frameRate;
- Blocky8 *_blocky8;
Blocky16 *_blocky16;
- Codec48Decoder *_codec48;
int32 _nbframes;
- int _frameStart;
};
- class SmushAudioTrack : public AudioTrack {
- public:
- SmushAudioTrack(Audio::Mixer::SoundType soundType, bool isVima, int freq = 22050, int channels = -1);
- ~SmushAudioTrack();
-
- Audio::AudioStream *getAudioStream() const override { return _queueStream; }
- bool isSeekable() const override { return true; }
- bool seek(const Audio::Timestamp &time) override;
- void skipSamples(int samples);
- inline int getRate() const { return _queueStream->getRate(); }
-
- void handleVIMA(Common::SeekableReadStream *stream, uint32 size);
- void handleIACT(Common::SeekableReadStream *stream, int32 size);
- void init();
- private:
- bool _isVima;
- byte _IACToutput[4096];
- int32 _IACTpos;
- int _channels;
- int _freq;
- Audio::QueuingAudioStream *_queueStream;
- };
private:
- void initFrames();
-
- SmushAudioTrack *_audioTrack;
SmushVideoTrack *_videoTrack;
SmushWave *_wave;
int _blocky16Frames;
@@ -150,16 +96,6 @@ private:
Common::SeekableReadStream *_file;
uint32 _startPos;
-
- bool _videoPause;
- bool _videoLooping;
- struct Frame {
- int frame;
- int pos;
- bool keyframe;
- };
- Frame *_frames;
- static bool _demo;
};
} // end of namespace Grim
diff --git a/engines/grim/movie/smush.cpp b/engines/grim/movie/smush.cpp
index 295e85ff4fc..d8c994396cf 100644
--- a/engines/grim/movie/smush.cpp
+++ b/engines/grim/movie/smush.cpp
@@ -19,6 +19,8 @@
*
*/
+#include "common/file.h"
+
#include "engines/grim/movie/codecs/smush_decoder.h"
#include "engines/grim/movie/smush.h"
#include "engines/grim/movie/smush_animator.h"
@@ -127,7 +129,6 @@ SmushPlayer::SmushPlayer(bool demo) : MoviePlayer(), _demo(demo) {
warning("VideoTheoraPlayer::initialize - Theora support not compiled in, video will be skipped");
_theoraDecoder = nullptr;
#endif
- //_smushDecoder->setDemo(_demo);
}
bool SmushPlayer::loadFile(const Common::String &filename) {
@@ -137,7 +138,7 @@ bool SmushPlayer::loadFile(const Common::String &filename) {
if (!_demo)
success = _videoDecoder->loadStream(g_resourceloader->openNewStreamFile(filename.c_str()));
else
- success = _videoDecoder->loadFile(Common::Path(filename));
+ success = Common::File::exists(Common::Path(filename));
if (!success) {
#if defined (USE_THEORADEC)
@@ -162,9 +163,6 @@ bool SmushPlayer::loadFile(const Common::String &filename) {
}
void SmushPlayer::init() {
- if (!_currentVideoIsTheora && !_demo)
- _smushDecoder->setLooping(_videoLooping);
-
MoviePlayer::init();
if (_demo && !_currentVideoIsTheora) {
@@ -355,12 +353,6 @@ void SmushPlayer::handleFrame() {
_videoFinished = true;
g_grim->setMode(GrimEngine::NormalMode);
deinit();
- return;
- } else {
- if (!_currentVideoIsTheora) {
- _smushDecoder->rewind(); // This doesn't handle if looping fails.
- _smushDecoder->start();
- }
}
}
}
@@ -412,12 +404,8 @@ void SmushPlayer::restore(SaveGame *state) {
}
if (isPlaying() && !_currentVideoIsTheora) {
- if (_smushAnim) {
- _frame = -1;
- _movieTime = 0;
- } else {
- _smushDecoder->seek((uint32)_movieTime);
- }
+ _frame = -1;
+ _movieTime = 0;
_smushDecoder->start();
timerCallback(this);
diff --git a/engines/grim/movie/smush_animator.cpp b/engines/grim/movie/smush_animator.cpp
index af78bd05618..a2b6c63308a 100644
--- a/engines/grim/movie/smush_animator.cpp
+++ b/engines/grim/movie/smush_animator.cpp
@@ -25,6 +25,7 @@
#include "common/debug.h"
#include "engines/grim/movie/codecs/blocky8.h"
+#include "engines/grim/movie/codecs/codec48.h"
#include "engines/grim/movie/codecs/smush_decoder.h"
#include "engines/grim/movie/smush_animator.h"
@@ -520,6 +521,7 @@ SmushDemoAnim::SmushDemoAnim(SmushDemoStream *stream, SmushDemoClient *client) {
_stream = stream;
_client = client;
_blocky8 = nullptr;
+ _codec48 = nullptr;
_refCount = 0;
_bitmap = nullptr;
_rect.x = 0;
@@ -979,6 +981,9 @@ int SmushDemoAnim::anmFrameObject(int x, int y, int flags, const byte *chunk, in
drawY -= height >> 1;
}
+ if (chunk[0] == 48 && _codec48)
+ return _codec48->decode(_bitmap, &_rect, drawX, drawY, chunk, flags, _client);
+
if (chunk[0] != 47 || !_blocky8)
return -1;
@@ -1016,12 +1021,16 @@ int SmushDemoAnim::anmFrameObject(int x, int y, int flags, const byte *chunk, in
}
int SmushDemoAnim::anmRegisterCodecs(int width, int height) {
+ _codec48 = new Codec48Decoder();
+ _codec48->init(width, height);
_blocky8 = new Blocky8();
_blocky8->init(width, height);
return 0;
}
void SmushDemoAnim::anmUnregisterCodecs() {
+ delete _codec48;
+ _codec48 = nullptr;
delete _blocky8;
_blocky8 = nullptr;
}
diff --git a/engines/grim/movie/smush_animator.h b/engines/grim/movie/smush_animator.h
index 02204cbdc6d..eafed133e01 100644
--- a/engines/grim/movie/smush_animator.h
+++ b/engines/grim/movie/smush_animator.h
@@ -33,6 +33,7 @@ namespace Grim {
class SmushDecoder;
class Blocky8;
+class Codec48Decoder;
struct SmushDemoBitmap {
byte *pixels;
@@ -107,6 +108,7 @@ private:
SmushDemoStream *_stream;
SmushDemoClient *_client;
Blocky8 *_blocky8;
+ Codec48Decoder *_codec48;
int _refCount;
SmushDemoBitmap *_bitmap;
Commit: 50b0d7e66eb5afbe5531f55cfe1ef8e5abc69aca
https://github.com/scummvm/scummvm/commit/50b0d7e66eb5afbe5531f55cfe1ef8e5abc69aca
Author: AndywinXp (andywinxp at gmail.com)
Date: 2026-11-01T22:18:07+01:00
Commit Message:
GRIM: SMUSH: Fix video speed for remastered
Changed paths:
engines/grim/movie/smush.cpp
engines/grim/movie/smush_mixer.cpp
engines/grim/movie/smush_mixer.h
diff --git a/engines/grim/movie/smush.cpp b/engines/grim/movie/smush.cpp
index d8c994396cf..ddf05e62e35 100644
--- a/engines/grim/movie/smush.cpp
+++ b/engines/grim/movie/smush.cpp
@@ -111,7 +111,7 @@ SmushPlayer::SmushPlayer(bool demo) : MoviePlayer(), _demo(demo) {
if (!_demo) {
_smushTimer = new SmushTimer();
- _smushDriver = new SmushDriver(_smushTimer);
+ _smushDriver = new SmushDriver(_smushTimer, g_grim->isRemastered());
_smushSound = new SmushSound(_smushTimer, _smushDriver);
_smushWave = new SmushWave(_smushSound);
_smushAnim = new SmushAnimPlay(_smushTimer, _smushSound);
diff --git a/engines/grim/movie/smush_mixer.cpp b/engines/grim/movie/smush_mixer.cpp
index 518a22ccbee..3b4e29111a7 100644
--- a/engines/grim/movie/smush_mixer.cpp
+++ b/engines/grim/movie/smush_mixer.cpp
@@ -609,8 +609,13 @@ int SmushDriverStream::readBuffer(int16 *buffer, const int numSamples) {
return _driver->ringRead(buffer, numSamples);
}
-SmushDriver::SmushDriver(SmushTimer *timer) {
+int SmushDriverStream::getRate() const {
+ return _driver->sndDrivGetRate();
+}
+
+SmushDriver::SmushDriver(SmushTimer *timer, bool remastered) {
_timer = timer;
+ _remastered = remastered;
_sound = nullptr;
_system = g_system->getMixer();
_ring = nullptr;
@@ -629,9 +634,15 @@ SmushDriver::~SmushDriver() {
}
int SmushDriver::sndDrivInitialize(int rate, int channelCount, SmushSound *sound) {
- if (rate < 22050)
+ // The disasm of the remastered ignores the requested rate and always runs at 24000
+ if (_remastered)
+ rate = 24000;
+ else if (rate < 22050)
rate = 22050;
+ // The stream reads the rate as soon as it is opened
+ _driverRate = rate;
+
if (sndDrivOpenRing())
return -1;
@@ -639,7 +650,6 @@ int SmushDriver::sndDrivInitialize(int rate, int channelCount, SmushSound *sound
return -1;
_sound = sound;
- _driverRate = rate;
_fillTimer = _timer->sysAlloc(sndDrivFillThreadCallback, 1193180 / (rate / 1024), SYS_TIMER_ONE_SHOT, this);
return 0;
diff --git a/engines/grim/movie/smush_mixer.h b/engines/grim/movie/smush_mixer.h
index 69b904f7e0c..cefba4a54f2 100644
--- a/engines/grim/movie/smush_mixer.h
+++ b/engines/grim/movie/smush_mixer.h
@@ -75,7 +75,7 @@ public:
int readBuffer(int16 *buffer, const int numSamples) override;
bool isStereo() const override { return true; }
- int getRate() const override { return 22050; }
+ int getRate() const override;
bool endOfData() const override { return false; }
};
@@ -94,6 +94,7 @@ private:
int _ringPlayPosition;
int _lastBlock;
int _driverRate;
+ bool _remastered;
int _driverVolume;
int _fillTimer;
int _volumeTable[128];
@@ -107,7 +108,7 @@ private:
int sndDrivMixerEnd(int16 *destBuf, int frameCount);
public:
- SmushDriver(SmushTimer *timer);
+ SmushDriver(SmushTimer *timer, bool remastered);
~SmushDriver();
int sndDrivInitialize(int rate, int channelCount, SmushSound *sound);
Commit: 821d07da11a7df7103afeda5ec0c8c8c199b64cb
https://github.com/scummvm/scummvm/commit/821d07da11a7df7103afeda5ec0c8c8c199b64cb
Author: AndywinXp (andywinxp at gmail.com)
Date: 2026-11-01T22:18:07+01:00
Commit Message:
GRIM: SMUSH: Fix out of bounds read of the FLHD audio header
Changed paths:
engines/grim/movie/codecs/smush_decoder.cpp
diff --git a/engines/grim/movie/codecs/smush_decoder.cpp b/engines/grim/movie/codecs/smush_decoder.cpp
index 5e13b48c5c4..39bcd291ee8 100644
--- a/engines/grim/movie/codecs/smush_decoder.cpp
+++ b/engines/grim/movie/codecs/smush_decoder.cpp
@@ -114,16 +114,20 @@ bool SmushDecoder::handleFramesHeader() {
byte *f_header = new byte[size];
_file->read(f_header, size);
- do {
+ // The disasm stops with 4 bytes or less left: the Wave header is declared 8 bytes
+ // long, but it is followed by the total size of the audio, which is part of FLHD
+ while (size - pos > 4) {
+ int blockSize = READ_BE_UINT32(f_header + pos + 4);
+
if (READ_BE_UINT32(f_header + pos) == MKTAG('B', 'l', '1', '6')) {
- pos += READ_BE_UINT32(f_header + pos + 4) + 8;
+ pos += blockSize + 8;
} else if (READ_BE_UINT32(f_header + pos) == MKTAG('W', 'a', 'v', 'e')) {
_wave->flobdecDecodeHeader(f_header + pos + 8);
- pos += 20;
+ pos += blockSize + 8;
} else {
error("SmushDecoder::handleFramesHeader() unknown tag");
}
- } while (pos < size);
+ }
delete[] f_header;
return true;
Commit: 7d3474257a76a42cb9a9ed9e4d0d69a6d8eb25db
https://github.com/scummvm/scummvm/commit/7d3474257a76a42cb9a9ed9e4d0d69a6d8eb25db
Author: AndywinXp (andywinxp at gmail.com)
Date: 2026-11-01T22:18:07+01:00
Commit Message:
GRIM: SMUSH: Remove unused timer and Wave module code
Changed paths:
engines/grim/movie/smush_timer.cpp
engines/grim/movie/smush_timer.h
engines/grim/movie/smush_wave.cpp
engines/grim/movie/smush_wave.h
diff --git a/engines/grim/movie/smush_timer.cpp b/engines/grim/movie/smush_timer.cpp
index c480220c7d2..5a800fb3cf6 100644
--- a/engines/grim/movie/smush_timer.cpp
+++ b/engines/grim/movie/smush_timer.cpp
@@ -31,17 +31,6 @@ SmushTimer::SmushTimer() {
_sysTickIncrement = 0;
_timerThreadLastTime = 0;
_sysInitCount = 0;
- _sysDisableFlag = 0;
- _sysAbortFlag = 0;
- _sysQueueFlag = 0;
- _sysTickBusy = 0;
- _sysDispatcherDisable = 0;
- _timerHandlerCoreIsReentrant = 0;
- _dispatcherAbortRequest = 0;
- _dispatcherActive = 0;
- _dispatcherSkip = 0;
- _dispatcherLast = 0;
- _dispatcherSaved = 0;
}
SmushTimer::~SmushTimer() {
@@ -81,12 +70,6 @@ void SmushTimer::timerThread() {
void SmushTimer::timerTick() {
_sysGlobalTimer += _sysTickIncrement;
- if (_sysDisableFlag)
- return;
-
- _sysTickBusy = 1;
- _sysQueueFlag = _sysAbortFlag;
- _sysAbortFlag = 0;
for (int i = 1; i <= SYS_MAX_TIMERS; i++) {
SmushTimerSlot *slot = &_timers[i];
@@ -95,27 +78,11 @@ void SmushTimer::timerTick() {
slot->accumulator += _sysTickIncrement;
slot->extSync += _sysTickIncrement;
-
- if ((slot->flags & 5) != 1 || !slot->period || slot->accumulator < slot->period)
- continue;
-
- while (true) {
- slot->accumulator -= slot->period;
- if (slot->flags & SYS_TIMER_ONE_SHOT) {
- slot->period = 0;
- slot->callback(i, slot->context);
- break;
- }
-
- slot->callback(i, slot->context);
- if (slot->accumulator < slot->period)
- break;
- }
}
for (int i = 1; i <= SYS_MAX_TIMERS; i++) {
SmushTimerSlot *slot = &_timers[i];
- if (!slot->callback || (slot->flags & 5) != 0 || !slot->period || slot->reentrant)
+ if (!slot->callback || !slot->period || slot->reentrant)
continue;
slot->reentrant = 1;
@@ -135,61 +102,6 @@ void SmushTimer::timerTick() {
}
slot->reentrant = 0;
}
-
- _sysAbortFlag = _sysQueueFlag;
- _sysTickBusy = 0;
-
- if (!_timerHandlerCoreIsReentrant && !_sysDispatcherDisable) {
- _timerHandlerCoreIsReentrant = 1;
- sysTimerDispatcher();
- _timerHandlerCoreIsReentrant = 0;
- }
-}
-
-void SmushTimer::sysTimerDispatcher() {
- if (_dispatcherSkip) {
- _dispatcherSkip--;
- if (_dispatcherSkip)
- return;
- }
-
- if (_dispatcherAbortRequest) {
- _dispatcherAbortRequest = 0;
- _dispatcherActive = 1;
- return;
- }
-
- _dispatcherAbortRequest = 0;
- _dispatcherActive = 1;
- sysTimerDispatcherLoop();
- _dispatcherActive = 0;
- _dispatcherLast = _dispatcherSaved;
-
- _sysAbortFlag = 0;
-}
-
-void SmushTimer::sysTimerDispatcherLoop() {
- for (int i = 1; i <= SYS_MAX_TIMERS; i++) {
- SmushTimerSlot *slot = &_timers[i];
- slot->busy = 1;
-
- if (slot->callback && (slot->flags & 5) == 4 && slot->period && slot->accumulator >= slot->period) {
- while (true) {
- slot->accumulator -= slot->period;
- if (slot->flags & SYS_TIMER_ONE_SHOT) {
- slot->period = 0;
- slot->callback(i, slot->context);
- break;
- }
-
- slot->callback(i, slot->context);
- if (slot->accumulator < slot->period)
- break;
- }
- }
-
- slot->busy = 0;
- }
}
int SmushTimer::sysAlloc(SmushTimerCallback callback, int period, int flags, void *context) {
@@ -207,7 +119,6 @@ int SmushTimer::sysAlloc(SmushTimerCallback callback, int period, int flags, voi
slot->flags = flags;
slot->period = period;
slot->callback = callback;
- slot->busy = 0;
slot->extSync = 0;
return i;
@@ -218,14 +129,6 @@ void SmushTimer::sysFree(int timer) {
_timers[timer].callback = nullptr;
}
-void SmushTimer::sysDisable() {
- _sysDisableFlag++;
-}
-
-void SmushTimer::sysEnable() {
- _sysDisableFlag--;
-}
-
void SmushTimer::sysReset(int timer, int period) {
if (timer > 0 && timer <= SYS_MAX_TIMERS) {
_timers[timer].period = period;
@@ -233,28 +136,10 @@ void SmushTimer::sysReset(int timer, int period) {
}
}
-void SmushTimer::sysDelay(int ticks) {
- int start = _sysGlobalTimer;
- int end = ticks + _sysGlobalTimer;
- int now = _sysGlobalTimer;
-
- do {
- if (now >= end)
- break;
-
- sysGetTime();
- now = _sysGlobalTimer;
- } while (_sysGlobalTimer >= start);
-}
-
int SmushTimer::sysGetTime() {
return _sysGlobalTimer;
}
-int SmushTimer::sysGetCount() {
- return _sysTickIncrement;
-}
-
void SmushTimer::sysExtSync(int value) {
for (int i = 1; i <= SYS_MAX_TIMERS; i++) {
SmushTimerSlot *slot = &_timers[i];
diff --git a/engines/grim/movie/smush_timer.h b/engines/grim/movie/smush_timer.h
index 78f17197965..9553c9dc401 100644
--- a/engines/grim/movie/smush_timer.h
+++ b/engines/grim/movie/smush_timer.h
@@ -40,7 +40,6 @@ struct SmushTimerSlot {
void *context;
int reentrant;
int accumulator;
- int busy;
int extSync;
};
@@ -52,21 +51,8 @@ private:
int _sysTickIncrement;
uint32 _timerThreadLastTime;
int _sysInitCount;
- int _sysDisableFlag;
- int _sysAbortFlag;
- int _sysQueueFlag;
- int _sysTickBusy;
- int _sysDispatcherDisable;
- int _timerHandlerCoreIsReentrant;
- int _dispatcherAbortRequest;
- int _dispatcherActive;
- int _dispatcherSkip;
- int _dispatcherLast;
- int _dispatcherSaved;
void timerTick();
- void sysTimerDispatcher();
- void sysTimerDispatcherLoop();
public:
SmushTimer();
@@ -78,12 +64,8 @@ public:
int sysAlloc(SmushTimerCallback callback, int period, int flags, void *context);
void sysFree(int timer);
- void sysDisable();
- void sysEnable();
void sysReset(int timer, int period);
- void sysDelay(int ticks);
int sysGetTime();
- int sysGetCount();
void sysExtSync(int value);
};
diff --git a/engines/grim/movie/smush_wave.cpp b/engines/grim/movie/smush_wave.cpp
index 856da7ef005..fb54c9ccfd0 100644
--- a/engines/grim/movie/smush_wave.cpp
+++ b/engines/grim/movie/smush_wave.cpp
@@ -53,38 +53,16 @@ int SmushWave::moduleShutdown() {
return 0;
}
-const char *SmushWave::flobdecInfo(int index) {
- switch (index) {
- case 0:
- return "Audio";
- case 1:
- return "Decompresses VBR-ADPCM 16-bit audio.";
- case 2:
- return "Decompresses Variable Bit Rate ADPCM 16-bit audio.";
- default:
- return "";
- }
-}
-
-uint32 SmushWave::flobdecGetID() {
- return MKTAG('W', 'a', 'v', 'e');
-}
-
-int SmushWave::flobdecGetType() {
- return 4;
-}
-
void SmushWave::flobdecReset() {
_chunkCounter = 0;
}
-int SmushWave::flobdecDecodeHeader(const byte *header) {
+void SmushWave::flobdecDecodeHeader(const byte *header) {
_chunkCounter = 0;
_totalSize = READ_LE_UINT32(header + 8);
- return 0;
}
-int SmushWave::flobdecDecodeBody(const byte *body) {
+void SmushWave::flobdecDecodeBody(const byte *body) {
const byte *data = body + 4;
int sampleCount = READ_BE_INT32(body);
if (sampleCount < 0) {
@@ -144,19 +122,6 @@ int SmushWave::flobdecDecodeBody(const byte *body) {
_sound->souEngineStart(packet, -1, 127, 0, 64);
_chunkCounter++;
}
-
- if (_chunkCounter && _totalSize > 100000)
- return 0;
-
- return 128;
-}
-
-int SmushWave::flobdecSetVal() {
- return -1;
-}
-
-int SmushWave::flobdecGetVal() {
- return -1;
}
} // End of namespace Grim
diff --git a/engines/grim/movie/smush_wave.h b/engines/grim/movie/smush_wave.h
index bbf6a362009..3c4f7cce368 100644
--- a/engines/grim/movie/smush_wave.h
+++ b/engines/grim/movie/smush_wave.h
@@ -48,14 +48,9 @@ public:
int moduleStartup();
int moduleShutdown();
- const char *flobdecInfo(int index);
- uint32 flobdecGetID();
- int flobdecGetType();
void flobdecReset();
- int flobdecDecodeHeader(const byte *header);
- int flobdecDecodeBody(const byte *body);
- int flobdecSetVal();
- int flobdecGetVal();
+ void flobdecDecodeHeader(const byte *header);
+ void flobdecDecodeBody(const byte *body);
};
Commit: 1f04abaee6694da733d324232331f3379d7dc963
https://github.com/scummvm/scummvm/commit/1f04abaee6694da733d324232331f3379d7dc963
Author: AndywinXp (andywinxp at gmail.com)
Date: 2026-11-01T22:18:07+01:00
Commit Message:
GRIM: SMUSH: Remove unused animator code
Changed paths:
engines/grim/movie/smush.cpp
engines/grim/movie/smush_anim.cpp
engines/grim/movie/smush_anim.h
engines/grim/movie/smush_animator.cpp
engines/grim/movie/smush_animator.h
diff --git a/engines/grim/movie/smush.cpp b/engines/grim/movie/smush.cpp
index ddf05e62e35..ab0ea6fdf91 100644
--- a/engines/grim/movie/smush.cpp
+++ b/engines/grim/movie/smush.cpp
@@ -179,7 +179,7 @@ void SmushPlayer::init() {
if (_smushAnim && !_currentVideoIsTheora) {
_smushAnim->anmStop();
_smushAnim->anmStart(15, 24589);
- _smushAnimator->animatorNewAnim(24589, -1, -1);
+ _smushAnimator->animatorNewAnim(24589);
}
}
@@ -235,7 +235,7 @@ bool SmushPlayer::prepareFrame() {
// The disasm restarts looping movies with the frame rate of the header
int delay = _smushDecoder->getFrameDelay();
_smushAnim->anmStart(delay ? (delay / 2 + 1000000) / delay : 15, 24589);
- _smushAnimator->animatorNewAnim(24589, -1, -1);
+ _smushAnimator->animatorNewAnim(24589);
} else {
_videoFinished = true;
}
@@ -246,7 +246,7 @@ bool SmushPlayer::prepareFrame() {
int SmushPlayer::animatorRender(int flags) {
int state = _smushAnimator->animatorRender(flags);
- if (state < 0 || state == 5)
+ if (state < 0)
return state;
_internalSurface = _smushDecoder->getFrameSurface();
diff --git a/engines/grim/movie/smush_anim.cpp b/engines/grim/movie/smush_anim.cpp
index 0145be58e29..72da947b2cd 100644
--- a/engines/grim/movie/smush_anim.cpp
+++ b/engines/grim/movie/smush_anim.cpp
@@ -98,15 +98,6 @@ int SmushAnimPlay::anmRender(SmushAnimClient *client) {
}
int SmushAnimPlay::anmRenderBody(SmushAnimClient *client) {
- if (_state == 5) {
- int result = client->animatorRender(_flags);
- _state = result;
- if (result < 0)
- _finished = 1;
-
- return result;
- }
-
if (!_framesDue)
return 6;
@@ -117,7 +108,7 @@ int SmushAnimPlay::anmRenderBody(SmushAnimClient *client) {
_lateTicks--;
_framesDue = 0;
- if (drift > 0 || _state == 2 || _state == 3 || _state == 4) {
+ if (drift > 0 || _state == 3 || _state == 4) {
if (_currentDelay > _nominalDelay)
_currentDelay = _nominalDelay;
@@ -151,7 +142,7 @@ int SmushAnimPlay::anmRenderBody(SmushAnimClient *client) {
}
int flags = _flags;
- if (_skipFlag && !(flags & 2))
+ if (_skipFlag)
flags |= 0x8000;
_state = client->animatorRender(flags);
@@ -163,26 +154,6 @@ int SmushAnimPlay::anmRenderBody(SmushAnimClient *client) {
return _state;
}
-void SmushAnimPlay::anmSetRate(int fps) {
- _nominalDelay = 1193180 / fps;
- _currentDelay = 1193180 / fps;
- _timer->sysReset(_frameTimer, 1193180 / fps);
- _timer->sysReset(_syncTimer, _nominalDelay);
-}
-
-void SmushAnimPlay::anmSyncUp() {
- _lateTicks = 0;
- _timer->sysReset(_frameTimer, _currentDelay);
-}
-
-bool SmushAnimPlay::anmRenderQuery() {
- return _state == 5 || _framesDue;
-}
-
-int SmushAnimPlay::anmGetTime() {
- return 1000 * _currentDelay / 1193180;
-}
-
void SmushAnimPlay::frameTimer(int timer, void *context) {
((SmushAnimPlay *)context)->_framesDue++;
}
@@ -193,21 +164,12 @@ void SmushAnimPlay::syncTimer(int timer, void *context) {
if (!anim->_started)
return;
- if (anim->_flags & 1) {
- if (!anim->_syncPaused && anim->_lateTicks >= 7) {
- anim->_syncPaused = 1;
- anim->_sound->souEnginePause();
- } else if (anim->_syncPaused) {
- if (anim->_lateTicks < 0) {
- anim->_syncPaused = 0;
- anim->_sound->souEngineResume();
- }
- }
- } else if (anim->_syncPaused) {
- if (anim->_lateTicks < 0) {
- anim->_syncPaused = 0;
- anim->_sound->souEngineResume();
- }
+ if (!anim->_syncPaused && anim->_lateTicks >= 7) {
+ anim->_syncPaused = 1;
+ anim->_sound->souEnginePause();
+ } else if (anim->_syncPaused && anim->_lateTicks < 0) {
+ anim->_syncPaused = 0;
+ anim->_sound->souEngineResume();
}
if (anim->_lateTicks < 20 && !anim->_syncPaused)
diff --git a/engines/grim/movie/smush_anim.h b/engines/grim/movie/smush_anim.h
index c5eaf37aa6e..310564d7c72 100644
--- a/engines/grim/movie/smush_anim.h
+++ b/engines/grim/movie/smush_anim.h
@@ -185,10 +185,6 @@ public:
int anmStart(int fps, int flags);
void anmStop();
int anmRender(SmushAnimClient *client);
- void anmSetRate(int fps);
- void anmSyncUp();
- bool anmRenderQuery();
- int anmGetTime();
};
diff --git a/engines/grim/movie/smush_animator.cpp b/engines/grim/movie/smush_animator.cpp
index a2b6c63308a..c93a11cb75c 100644
--- a/engines/grim/movie/smush_animator.cpp
+++ b/engines/grim/movie/smush_animator.cpp
@@ -34,7 +34,6 @@ namespace Grim {
#define SMUSH_TAG_NULL MKTAG('n', 'u', 'l', 'l')
#define SMUSH_TAG_END MKTAG('e', 'n', 'd', ' ')
#define SMUSH_TAG_PAUSE MKTAG('p', 'a', 'u', 's')
-#define SMUSH_TAG_WAIT MKTAG('w', 'a', 'i', 't')
SmushAnimator::SmushAnimator(SmushDecoder *decoder) {
_decoder = decoder;
@@ -50,61 +49,24 @@ SmushAnimator::~SmushAnimator() {
free(_buffer);
}
-int SmushAnimator::animatorInitialize() {
+void SmushAnimator::animatorInitialize() {
_flags = 0;
_frame = 0;
_blockSize = 0;
_tag = SMUSH_TAG_NULL;
_frameCount = 1;
- _fileMode = 0;
- _quit = 0;
_pause = 0;
- _seeking = 0;
- _skipping = 0;
- _skipStart = -1;
- _skipBase = 0;
- _skipEnd = -1;
- return 0;
}
-int SmushAnimator::animatorNewAnim(int flags, int skipLength, int skipUntil) {
+void SmushAnimator::animatorNewAnim(int flags) {
animatorInitialize();
_frameCount = _decoder->getFrameCount();
- _seeking = 0;
_flags = flags;
- _fileMode = 1;
-
- _skipStart = skipUntil;
- _skipBase = _frame;
- _skipEnd = skipLength;
- return 0;
}
-int SmushAnimator::animatorQuery() {
- return _fileMode;
-}
-
-void SmushAnimator::animatorQuit() {
- _quit = 1;
-}
-
-int SmushAnimator::animatorPause(int mode) {
- if (mode == 2) {
- _pause = (_pause == 0);
- return _pause;
- }
-
- if (mode == 4) {
- if (_pause) {
- _pause = 3;
- return _pause;
- }
- } else if (mode != 5) {
- _pause = mode;
- }
-
- return _pause;
+void SmushAnimator::animatorPause(bool pause) {
+ _pause = pause;
}
void SmushAnimator::readBlock(int size) {
@@ -114,7 +76,6 @@ void SmushAnimator::readBlock(int size) {
_block = nullptr;
_blockStatus = 0;
_frame = -1;
- _fileMode = 0;
return;
}
@@ -134,22 +95,6 @@ void SmushAnimator::readBlock(int size) {
_blockStatus = 1;
}
-int SmushAnimator::checkSkip() {
- if (!_skipping) {
- if (_fileMode) {
- if (_skipEnd >= 0 && _frame > _skipEnd + _skipBase)
- return 1;
- } else if (_skipStart >= 0) {
- if (_frame < _skipStart)
- return 1;
-
- _skipStart = -1;
- }
- }
-
- return 0;
-}
-
void SmushAnimator::decodeBlocks(int flags) {
int allFlags = _flags | flags;
int decodeFlags = ((allFlags >> 13) & 4) | (allFlags & 0x30);
@@ -162,11 +107,6 @@ bool SmushAnimator::moreData() {
}
int SmushAnimator::animatorRender(int flags) {
- if (_quit) {
- _quit = 0;
- return -1;
- }
-
while (true) {
switch (_tag) {
case MKTAG('A', 'N', 'N', 'O'):
@@ -184,48 +124,25 @@ int SmushAnimator::animatorRender(int flags) {
return -2;
_tag = SMUSH_TAG_NULL;
- if (_block) {
- _frame++;
- if (_skipping || !checkSkip()) {
- decodeBlocks(flags);
-
- if (_frame < _frameCount - 1 || _skipping) {
- if (_pause == 1)
- _tag = SMUSH_TAG_PAUSE;
- else if (_pause == 3)
- _pause = 1;
- } else if (moreData() || _seeking) {
- _tag = SMUSH_TAG_NULL;
- } else {
- _tag = SMUSH_TAG_END;
- }
+ if (!_block)
+ return -2;
- if (_skipping)
- return 2;
+ _frame++;
+ decodeBlocks(flags);
- // The disasm never renders the last frame
- if (_frame >= 10)
- return _frame < _frameCount - 1 ? 0 : 4;
+ if (_frame < _frameCount - 1) {
+ if (_pause)
+ _tag = SMUSH_TAG_PAUSE;
+ } else if (!moreData()) {
+ _tag = SMUSH_TAG_END;
+ }
- return 1;
- }
+ // The disasm never renders the last frame
+ if (_frame >= 10)
+ return _frame < _frameCount - 1 ? 0 : 4;
- if (_frame >= _frameCount - 1) {
- if (moreData() || _seeking)
- _tag = SMUSH_TAG_NULL;
- else
- _tag = SMUSH_TAG_END;
- }
- } else {
- return -2;
- }
- break;
+ return 1;
case SMUSH_TAG_NULL:
- if (_seeking) {
- _seeking = 0;
- _skipping = 1;
- }
-
readBlock(8);
if (_blockStatus == -2 || !_block)
return -2;
@@ -235,26 +152,16 @@ int SmushAnimator::animatorRender(int flags) {
break;
case SMUSH_TAG_END:
_frame++;
- checkSkip();
- if (moreData() || _seeking)
+ if (moreData())
_tag = SMUSH_TAG_NULL;
return 4;
case SMUSH_TAG_PAUSE:
- if (_pause != 1) {
- if (_pause == 3)
- _pause = 1;
-
+ if (!_pause)
_tag = SMUSH_TAG_NULL;
- }
return 3;
- case SMUSH_TAG_WAIT:
- if (moreData() || _seeking)
- _tag = SMUSH_TAG_NULL;
-
- return 5;
default:
return -2;
}
diff --git a/engines/grim/movie/smush_animator.h b/engines/grim/movie/smush_animator.h
index eafed133e01..203ece0e5fd 100644
--- a/engines/grim/movie/smush_animator.h
+++ b/engines/grim/movie/smush_animator.h
@@ -201,18 +201,11 @@ private:
int _frameCount;
int _blockSize;
uint32 _tag;
- int _fileMode;
int _flags;
- int _seeking;
- int _skipping;
- int _skipStart;
- int _skipBase;
- int _skipEnd;
- int _quit;
int _pause;
+ void animatorInitialize();
void readBlock(int size);
- int checkSkip();
void decodeBlocks(int flags);
bool moreData();
@@ -220,12 +213,9 @@ public:
SmushAnimator(SmushDecoder *decoder);
~SmushAnimator();
- int animatorInitialize();
- int animatorNewAnim(int flags, int skipLength, int skipUntil);
+ void animatorNewAnim(int flags);
int animatorRender(int flags);
- int animatorQuery();
- void animatorQuit();
- int animatorPause(int mode);
+ void animatorPause(bool pause);
};
Commit: a11b8d30503ebdcad80f5641d350aa375624f514
https://github.com/scummvm/scummvm/commit/a11b8d30503ebdcad80f5641d350aa375624f514
Author: AndywinXp (andywinxp at gmail.com)
Date: 2026-11-01T22:18:07+01:00
Commit Message:
GRIM: SMUSH: Remove unused SOU code
Changed paths:
engines/grim/movie/smush_sound.cpp
engines/grim/movie/smush_sound.h
engines/grim/movie/smush_wave.cpp
diff --git a/engines/grim/movie/smush_sound.cpp b/engines/grim/movie/smush_sound.cpp
index ee7087287d5..7ced0c738eb 100644
--- a/engines/grim/movie/smush_sound.cpp
+++ b/engines/grim/movie/smush_sound.cpp
@@ -37,11 +37,7 @@ SmushSound::SmushSound(SmushTimer *timer, SmushDriver *driver) {
memset(_channels, 0, sizeof(_channels));
memset(_playback, 0, sizeof(_playback));
_channelCount = 0;
- _masterVolume = 127;
_sampleRate = 22050;
- _duckFloor = 64;
- _duckRate = 256;
- _duckLevel = 256;
_resetFlag = 1;
_bytesPerFrame[0] = 1;
_bytesPerFrame[1] = 2;
@@ -49,16 +45,11 @@ SmushSound::SmushSound(SmushTimer *timer, SmushDriver *driver) {
_bytesPerFrame[3] = 4;
_bytesPerFrame[4] = 2;
_bytesPerFrame[5] = 4;
- memset(_groupVolumes, 0, sizeof(_groupVolumes));
_pauseCount = 0;
_engineOk = 0;
_syncMark = 0;
_syncAccumulator = 0.0;
_fillCounter = 0;
- _musicParam = 0;
- _voiceParam = 0;
- _normalParam = 0;
- _panInvert = 0;
_driverRate = 22050;
_reserveRam = 64000;
memset(_bufferSizes, 0, sizeof(_bufferSizes));
@@ -79,8 +70,6 @@ int SmushSound::moduleShutdown() {
int SmushSound::engineInitialize(int rate, int reserveRam, const int *bufferSizes) {
_channelCount = 0;
- _masterVolume = 127;
- memset(_groupVolumes, 0, sizeof(_groupVolumes));
int size = bufferSizes[0];
int index = 1;
@@ -97,9 +86,6 @@ int SmushSound::engineInitialize(int rate, int reserveRam, const int *bufferSize
_engineOk = (_driver->sndDrivInitialize(rate, _channelCount, this) == 0);
_sampleRate = _driver->sndDrivGetRate();
_fillCounter = 0;
- _musicParam = 1;
- _voiceParam = 1;
- _normalParam = 1;
_timer->sysExtSync(0);
return 0;
@@ -129,7 +115,6 @@ int SmushSound::engineAllocChannel(int size, int reserveRam) {
chan->state = 0;
chan->consumedPosition = 0;
chan->soundClass = 0;
- chan->soundId = SOU_FREE_ID;
chan->dataBuffer = nullptr;
chan->stagingBuffer = nullptr;
chan->capacity = size & ~3;
@@ -174,47 +159,23 @@ int SmushSound::engineAllocChannel(int size, int reserveRam) {
return -2;
}
-int SmushSound::souEngineStart(const byte *data, int soundId, int volume, int pan, int soundClass) {
+int SmushSound::souEngineStart(const byte *data, int volume, int pan, int soundClass) {
if (!_engineOk)
return 0;
- uint32 tag = READ_BE_UINT32(data);
- if (tag == MKTAG('P', 'S', 'A', 'D') || tag == MKTAG('P', 'V', 'O', 'C')) {
- if (data[8] || data[9] || data[12] || data[13] || data[16] || data[17]) {
- int8 panByte = (int8)data[17];
- int newPan;
- if (panByte == -128)
- newPan = 128;
- else
- newPan = panByte + pan;
-
- return souEngineStartStream(data + 18, READ_BE_UINT32(data + 4) - 10, soundId, (volume * data[16]) >> 7, newPan,
- soundClass | READ_LE_INT16(data + 14), READ_LE_INT16(data + 8), READ_LE_INT16(data + 10), READ_LE_INT16(data + 12), 1);
- }
-
- return souEngineStartStream(data + 20, READ_BE_UINT32(data + 4) - 12, soundId, volume, pan, soundClass,
- READ_BE_UINT16(data + 10), READ_BE_UINT16(data + 14), READ_BE_UINT16(data + 18), 1);
- }
-
- if (tag == MKTAG('C', 'r', 'e', 'a')) {
- int offset = data[20] + (data[21] << 8);
- // The disasm builds the size by repeating a single byte three times
- return souEngineStartCreative(data + offset + 6, 65793 * data[offset + 1], soundId, volume, pan, soundClass);
- }
-
- if (tag == MKTAG('S', 'A', 'U', 'D'))
- return souEngineStartStream(data, READ_BE_UINT32(data + 4) + 8, soundId, volume, pan, soundClass, 0, 0, 1, 0);
+ // The only packets which reach the engine are the ones SmushWave builds
+ if (READ_BE_UINT32(data) != MKTAG('P', 'S', 'A', 'D'))
+ return 0;
- return 0;
-}
+ int8 panByte = (int8)data[17];
+ int newPan;
+ if (panByte == -128)
+ newPan = 128;
+ else
+ newPan = panByte + pan;
-void SmushSound::souEngineStop(int soundId) {
- for (int i = 0; i < _channelCount; i++) {
- if (_channels[i].soundId == soundId) {
- _channels[i].state = 0;
- _channels[i].soundId = SOU_FREE_ID;
- }
- }
+ return souEngineStartStream(data + 18, READ_BE_UINT32(data + 4) - 10, (volume * data[16]) >> 7, newPan,
+ soundClass | READ_LE_INT16(data + 14), READ_LE_INT16(data + 8), READ_LE_INT16(data + 10), READ_LE_INT16(data + 12));
}
void SmushSound::souEnginePause() {
@@ -229,72 +190,8 @@ int SmushSound::souEngineResume() {
}
void SmushSound::souEngineStopAll() {
- for (int i = 0; i < _channelCount; i++) {
+ for (int i = 0; i < _channelCount; i++)
_channels[i].state = 0;
- _channels[i].soundId = SOU_FREE_ID;
- }
-}
-
-void SmushSound::souEngineSetVol(int soundId, int volume) {
- if (soundId == SOU_FREE_ID) {
- _masterVolume = volume;
- return;
- }
-
- for (int i = 0; i < _channelCount; i++) {
- if (_channels[i].soundId == soundId)
- _channels[i].volume = volume;
- }
-}
-
-int SmushSound::souEngineGetVol(int soundId) {
- if (soundId == SOU_FREE_ID)
- return _masterVolume;
-
- for (int i = 0; i < _channelCount; i++) {
- if (_channels[i].soundId == soundId)
- return _channels[i].volume;
- }
-
- return -1;
-}
-
-void SmushSound::souEngineSetPan(int soundId, int pan) {
- for (int i = 0; i < _channelCount; i++) {
- if (_channels[i].soundId == soundId)
- _channels[i].pan = pan;
- }
-}
-
-int SmushSound::souEngineGetPan(int soundId) {
- for (int i = 0; i < _channelCount; i++) {
- if (_channels[i].soundId == soundId)
- return _channels[i].pan;
- }
-
- return -1;
-}
-
-int SmushSound::souEngineSetHVol(int volume) {
- if (_driver)
- return _driver->sndDrivSetVol(volume);
-
- return 0;
-}
-
-int SmushSound::souEngineGetHVol() {
- if (_driver)
- return _driver->sndDrivGetVol();
-
- return -1;
-}
-
-int SmushSound::souEngineSetRate(int rate) {
- _sampleRate = rate;
- if (_driver)
- return _driver->sndDrivDummy(rate);
-
- return rate;
}
int SmushSound::souEngineGetRate() {
@@ -304,34 +201,6 @@ int SmushSound::souEngineGetRate() {
return -1;
}
-int SmushSound::souEngineQuery(int soundId) {
- if (soundId == SOU_FREE_ID) {
- for (int i = 0; i < _channelCount; i++) {
- if (_channels[i].state)
- return _channels[i].consumedPosition + 1;
- }
-
- return 0;
- }
-
- if (soundId >= 0 || -1 - soundId >= _channelCount) {
- for (int i = 0; i < _channelCount; i++) {
- if (_channels[i].soundId == soundId) {
- if (_channels[i].state)
- return _channels[i].consumedPosition + 1;
-
- break;
- }
- }
- } else {
- SmushSoundChannel *chan = &_channels[-1 - soundId];
- if (chan->state)
- return chan->consumedPosition + 1;
- }
-
- return 0;
-}
-
int SmushSound::souEngineQueryTime(int channelIndex) {
int samples = _channels[channelIndex].consumedPosition;
@@ -352,77 +221,6 @@ int SmushSound::souEngineQueryTime(int channelIndex) {
return (int)(int64)((double)samples / ((double)souEngineGetRate() * 0.001));
}
-int SmushSound::souEngineSetParam(int soundId, int param, int value1, int value2) {
- switch (param) {
- case 1:
- setGroupVolume(value1, value2);
- return 0;
- case 2:
- if (value1)
- _duckFloor = value1;
- if (!value2)
- return 0;
-
- _duckRate = value2;
- return 0;
- case 3:
- _musicParam = value1;
- return 0;
- case 4:
- _voiceParam = value1;
- return 0;
- case 5:
- _normalParam = value1;
- return 0;
- default:
- return -1;
- }
-}
-
-int SmushSound::souEngineGetParam(int soundId, int param, int *value1, int *value2) {
- switch (param) {
- case 1:
- // The disasm passes the group index in the pointer slot
- if (value1)
- *value1 = getGroupVolume((uint8)(intptr)value2);
- return 0;
- case 2:
- if (value1)
- *value1 = _duckFloor;
- if (value2)
- *value2 = _duckRate;
- return 0;
- case 3:
- if (value1)
- *value1 = _musicParam;
- return 0;
- case 4:
- if (value1)
- *value1 = _voiceParam;
- return 0;
- case 5:
- if (value1)
- *value1 = _normalParam;
- return 0;
- default:
- return -1;
- }
-}
-
-uint8 SmushSound::getGroupVolume(uint8 index) {
- if (index == 0xFF)
- return _masterVolume;
-
- return _groupVolumes[index];
-}
-
-void SmushSound::setGroupVolume(uint8 index, uint8 volume) {
- if (index == 0xFF)
- _masterVolume = volume;
-
- _groupVolumes[index] = volume;
-}
-
int SmushSound::souEngineMarkSyncTime() {
_syncMark = _timer->sysGetTime();
return _syncMark;
@@ -448,99 +246,7 @@ int SmushSound::souEngineExternalSync(double ticks) {
return result;
}
-int SmushSound::souEngineStartCreative(const byte *data, int size, int soundId, int volume, int pan, int soundClass) {
- int largest = 0;
- for (int i = _channelCount - 1; i >= 0; i--) {
- if (_channels[i].capacity > largest)
- largest = _channels[i].capacity;
- }
-
- int bestDistance = 10000000;
- for (int i = _channelCount - 1; i >= 0; i--) {
- int capacity = _channels[i].capacity;
- int distance = capacity - size;
- if (distance < 0)
- distance = size - capacity;
-
- if (distance < bestDistance && size < capacity) {
- bestDistance = distance;
- largest = capacity;
- }
- }
-
- int scores[SOU_MAX_CHANNELS] = {};
- for (int i = _channelCount - 1; i >= 0; i--) {
- SmushSoundChannel *chan = &_channels[i];
- scores[i] = 0;
-
- if (chan->capacity != largest)
- continue;
-
- if (chan->state && chan->state != 3 && chan->soundClass > soundClass)
- continue;
-
- if (!chan->state)
- scores[i] = 4096;
- if (chan->soundClass < soundClass)
- scores[i] += 2048;
- if (chan->soundClass == soundClass)
- scores[i] += 1024;
- if (chan->state == 3)
- scores[i] += 512;
- scores[i] += chan->age + 1;
- }
-
- int best = -1;
- int bestScore = 0;
- for (int i = _channelCount - 1; i >= 0; i--) {
- if (scores[i] > bestScore) {
- best = i;
- bestScore = scores[i];
- }
- }
-
- if (best < 0)
- return 0;
-
- for (int i = _channelCount - 1; i >= 0; i--) {
- if (_channels[i].age < 256)
- _channels[i].age++;
- }
-
- SmushSoundChannel *chan = &_channels[best];
- chan->age = 0;
- chan->state = 0;
-
- byte *buffer = chan->dataBuffer;
- buffer[0] = 1;
- buffer[1] = 8;
- buffer[2] = 0;
- buffer[3] = 0;
- buffer[4] = 0;
- buffer[5] = 0;
- buffer[6] = size >> 24;
- buffer[7] = size >> 16;
- buffer[8] = size >> 8;
- buffer[9] = size;
- buffer[10] = 0;
- buffer[11] = 0;
- memcpy(buffer + 12, data, size);
-
- chan->scriptPointer = buffer;
- chan->dataArea = buffer + 12;
- chan->dataAreaSize = chan->capacity - 12;
- chan->writePosition = size;
- chan->soundId = soundId;
- chan->volume = volume;
- chan->pan = pan;
- chan->soundClass = soundClass;
- chan->consumedPosition = 0;
- chan->state = 2;
-
- return pan;
-}
-
-int SmushSound::souEngineStartStream(const byte *data, int size, int soundId, uint32 volume, int pan, int soundClass, int tag1, int chunkCounter, int tag2, int copyFlag) {
+int SmushSound::souEngineStartStream(const byte *data, int size, uint32 volume, int pan, int soundClass, int tag1, int chunkCounter, int tag2) {
if (chunkCounter) {
int index = _channelCount - 1;
if (index < 0)
@@ -572,36 +278,15 @@ int SmushSound::souEngineStartStream(const byte *data, int size, int soundId, ui
return pan;
}
- int streamSize = chunkCounter;
- if (tag2 != 1 || copyFlag)
- streamSize = READ_BE_UINT32(data + 4);
-
int largest = 0;
for (int i = _channelCount - 1; i >= 0; i--) {
if (_channels[i].capacity > largest)
largest = _channels[i].capacity;
}
- int bestDistance = 10000000;
- if ((soundClass & 0xC0) == 0x40) {
- if (tag2 > 1)
- souEngineMarkSyncTime();
- } else {
- for (int i = _channelCount - 1; i >= 0; i--) {
- int capacity = _channels[i].capacity;
- int distance = capacity - streamSize;
- if (distance < 0)
- distance = streamSize - capacity;
-
- if (distance < bestDistance && capacity > 2 * streamSize / 3) {
- int needed = (tag2 <= 1) ? size : 2 * size;
- if (capacity > needed) {
- bestDistance = distance;
- largest = capacity;
- }
- }
- }
- }
+ // Only the music class is started
+ if (tag2 > 1)
+ souEngineMarkSyncTime();
int scores[SOU_MAX_CHANNELS] = {};
for (int i = _channelCount - 1; i >= 0; i--) {
@@ -652,28 +337,17 @@ int SmushSound::souEngineStartStream(const byte *data, int size, int soundId, ui
int headerSize = READ_BE_UINT32(data + 12);
int totalSize = READ_BE_UINT32(data + headerSize + 20);
- int copiedSize;
- int bufferSize;
- if (tag2 != 1 || copyFlag) {
- int copySize = chan->capacity;
- copiedSize = size;
- if (size < copySize)
- copySize = size;
-
- memcpy(chan->dataBuffer, data, copySize);
- chan->scriptPointer = chan->dataBuffer + 16;
- bufferSize = chan->capacity;
- } else {
- copiedSize = size;
- chan->scriptPointer = (byte *)data + 16;
- bufferSize = size;
- }
+ int copySize = chan->capacity;
+ if (size < copySize)
+ copySize = size;
+
+ memcpy(chan->dataBuffer, data, copySize);
+ chan->scriptPointer = chan->dataBuffer + 16;
- chan->dataAreaSize = bufferSize - headerSize - 24;
+ chan->dataAreaSize = chan->capacity - headerSize - 24;
chan->dataArea = chan->scriptPointer + headerSize + 8;
- chan->writePosition = copiedSize - headerSize - 24;
+ chan->writePosition = size - headerSize - 24;
chan->totalSize = totalSize;
- chan->soundId = soundId;
chan->volume = 127;
chan->pan = 0;
if (volume < 0x80)
@@ -686,24 +360,12 @@ int SmushSound::souEngineStartStream(const byte *data, int size, int soundId, ui
chan->state = 2;
uint32 formatTag = READ_BE_UINT32(chan->dataArea - 8);
- if (formatTag == MKTAG('S', '6', 'S', 'Z')) {
+ if (formatTag == MKTAG('S', '6', 'S', 'Z'))
chan->format = 5;
- } else if (formatTag == MKTAG('S', '6', 'M', 'Z')) {
+ else if (formatTag == MKTAG('S', '6', 'M', 'Z'))
chan->format = 4;
- } else if (formatTag == MKTAG('S', 'D', '2', 'S')) {
- chan->format = 1;
- } else if (formatTag > MKTAG('S', 'D', '2', 'S')) {
- if (formatTag == MKTAG('S', 'D', '6', 'M'))
- chan->format = 2;
- else if (formatTag == MKTAG('S', 'D', '6', 'M') + 6)
- chan->format = 3;
- else if (formatTag == MKTAG('S', 'D', '6', 'M') + 518)
- chan->format = 1;
- else
- chan->format = 0;
- } else {
+ else
chan->format = 0;
- }
return formatTag;
}
@@ -717,24 +379,15 @@ void SmushSound::souEngineFill(int frameCount, SmushDriver *driver) {
if (_resetFlag) {
_resetFlag = 0;
- for (int i = 0; i < SOU_MAX_CHANNELS; i++) {
+ for (int i = 0; i < SOU_MAX_CHANNELS; i++)
_playback[i].tailBytes = 0;
- _playback[i].loopRemaining = 0;
- }
}
- int voicePlaying = 0;
-
for (int i = 0; i < _channelCount; i++) {
SmushSoundChannel *chan = &_channels[i];
SmushSoundPlayback *rt = &_playback[i];
- int soundClass = chan->soundClass & 0xC0;
- bool enabled = (soundClass == 128 && _voiceParam) || (soundClass == 64 && _musicParam) || (!soundClass && _normalParam);
- int scaledVolume = chan->volume * _masterVolume;
- int volume = scaledVolume / 127;
- if (soundClass == 64 && _voiceParam)
- volume = (_duckLevel * (scaledVolume / 127)) >> 8;
+ int volume = chan->volume;
if (chan->state == 2 && rt->previousState == 1) {
int position = rt->dataSize ? rt->readPosition % rt->dataSize : 0;
@@ -796,14 +449,8 @@ void SmushSound::souEngineFill(int frameCount, SmushDriver *driver) {
else
chunkFrames = _sampleRate * (chunkBytes / _bytesPerFrame[rt->previousFormat]) / rt->previousRate;
- if (enabled) {
- int pan = chan->pan;
- if (_panInvert)
- pan = -pan;
-
- driver->sndDrivMix(chan->stagingBuffer - rt->tailBytes + 6144, destOffset, chunkFrames, chunkBytes, 1,
- rt->previousVolume * rt->tailBytes * _masterVolume / 780288, pan, rt->previousFormat);
- }
+ driver->sndDrivMix(chan->stagingBuffer - rt->tailBytes + 6144, destOffset, chunkFrames, chunkBytes, 1,
+ rt->previousVolume * rt->tailBytes * SOU_MASTER_VOLUME / 780288, chan->pan, rt->previousFormat);
destOffset += chunkFrames;
bool done = remainingBytes - chunkBytes < 4;
@@ -826,7 +473,6 @@ void SmushSound::souEngineFill(int frameCount, SmushDriver *driver) {
rt->dataSize = chan->dataAreaSize;
rt->format = chan->format;
rt->remaining = 0;
- rt->loopRemaining = 0;
chan->state = 1;
}
@@ -854,8 +500,6 @@ void SmushSound::souEngineFill(int frameCount, SmushDriver *driver) {
chunkBytes = 1024;
chan->state = 1;
- if ((chan->soundClass & 0xC0) == 0x80)
- voicePlaying = 1;
} else {
if (gain)
rt->gain = gain - 1;
@@ -875,14 +519,8 @@ void SmushSound::souEngineFill(int frameCount, SmushDriver *driver) {
else
chunkFrames = _sampleRate * (chunkBytes / _bytesPerFrame[rt->format]) / rt->rate;
- if (enabled) {
- int pan = chan->pan;
- if (_panInvert)
- pan = -pan;
-
- driver->sndDrivMix(rt->dataStart + ringPosition, destOffset, chunkFrames, chunkBytes, 1,
- (rt->gain * volume) >> 4, pan, rt->format);
- }
+ driver->sndDrivMix(rt->dataStart + ringPosition, destOffset, chunkFrames, chunkBytes, 1,
+ (rt->gain * volume) >> 4, chan->pan, rt->format);
rt->remaining -= chunkBytes;
rt->readPosition += chunkBytes;
@@ -896,9 +534,9 @@ void SmushSound::souEngineFill(int frameCount, SmushDriver *driver) {
while (framesLeft) {
byte *op = rt->script;
+ // The only script of a movie sets the whole stream up, then ends
switch (op[0]) {
case 1: {
- rt->loopRemaining = 0;
int dataAreaSize = chan->dataAreaSize;
rt->readPosition = READ_BE_UINT32(op + 2);
rt->remaining = READ_BE_UINT32(op + 6);
@@ -914,137 +552,6 @@ void SmushSound::souEngineFill(int frameCount, SmushDriver *driver) {
rt->remaining -= skip;
} else {
chan->state = 0;
- chan->soundId = SOU_FREE_ID;
- framesLeft = 0;
- }
- }
- break;
- }
- case 2:
- case 8:
- case 9:
- case 10:
- case 11: {
- uint8 index = op[4];
- uint8 value;
- switch (index) {
- case 0xFF:
- value = _masterVolume;
- break;
- case 0xFE:
- value = chan->volume;
- break;
- case 0xFD:
- value = chan->pan;
- break;
- default:
- value = _groupVolumes[index];
- break;
- }
-
- switch (op[0]) {
- case 2:
- if (value || !index)
- value = 1;
- break;
- case 8:
- value = op[5] < value;
- break;
- case 9:
- value = value < op[5];
- break;
- case 10:
- value = value == op[5];
- break;
- case 11:
- value = value != op[5];
- break;
- default:
- break;
- }
-
- if (!value)
- rt->script = op + op[1] + 2;
- else
- rt->script = op + READ_BE_UINT16(op + 2);
- break;
- }
- case 3: {
- int8 variable = op[2];
- if (variable == -1) {
- _masterVolume = op[3];
- rt->script = op + op[1] + 2;
- } else if (variable == -2) {
- int newVolume = (int8)op[3];
- int scaled = newVolume * _masterVolume;
- chan->volume = newVolume;
- volume = scaled / 127;
- if ((chan->soundClass & 0xC0) == 0x40 && _voiceParam)
- volume = (_duckLevel * (scaled / 127)) >> 8;
-
- rt->script = op + op[1] + 2;
- } else {
- if (variable == -3)
- chan->pan = op[3];
- else
- _groupVolumes[op[2]] = op[3];
-
- rt->script = op + op[1] + 2;
- }
- break;
- }
- case 4: {
- int8 variable = op[2];
- if (variable == -1)
- _masterVolume += op[3];
- else if (variable == -2)
- chan->volume += op[3];
- else if (variable == -3)
- chan->pan += op[3];
- else
- _groupVolumes[op[2]] += op[3];
-
- rt->script = op + op[1] + 2;
- break;
- }
- case 6:
- rt->loopRemaining = 0;
- rt->readPosition = READ_BE_UINT32(op + 2);
- rt->remaining = READ_BE_UINT32(op + 6);
- rt->rate = READ_BE_UINT32(op + 10);
- rt->script = op + op[1] + 2;
- break;
- case 7: {
- if (!rt->loopRemaining) {
- rt->loopRemaining = READ_BE_UINT32(op + 6);
- rt->repeatAccumulator = 0;
- }
-
- rt->readPosition = rt->repeatAccumulator + READ_BE_UINT32(op + 2);
- uint32 segmentSize = READ_BE_UINT32(op + 14);
- if ((uint32)rt->loopRemaining < segmentSize)
- segmentSize = rt->loopRemaining;
-
- rt->remaining = segmentSize;
- rt->rate = READ_BE_UINT32(op + 10);
- rt->loopRemaining -= rt->remaining;
- rt->repeatAccumulator += rt->remaining;
- if (rt->loopRemaining)
- rt->script = op + op[1] + 2;
- else
- rt->script = op + READ_BE_UINT16(op + 18);
-
- int writePosition = chan->writePosition;
- int dataAreaSize = chan->dataAreaSize;
- int slack = (writePosition >= chan->totalSize) ? 0 : 15000;
- if (rt->readPosition < writePosition + slack - dataAreaSize) {
- int skip = writePosition - rt->readPosition - dataAreaSize + 15000;
- if (skip <= rt->remaining) {
- rt->readPosition += skip;
- rt->remaining -= skip;
- } else {
- chan->state = 0;
- chan->soundId = SOU_FREE_ID;
framesLeft = 0;
}
}
@@ -1052,7 +559,6 @@ void SmushSound::souEngineFill(int frameCount, SmushDriver *driver) {
}
default:
chan->state = 0;
- chan->soundId = SOU_FREE_ID;
framesLeft = 0;
break;
}
@@ -1076,18 +582,6 @@ void SmushSound::souEngineFill(int frameCount, SmushDriver *driver) {
chan->consumedPosition = rt->readPosition;
rt->previousState = chan->state;
}
-
- if (voicePlaying) {
- if (_duckLevel > _duckFloor) {
- _duckLevel -= (_duckRate * frameCount) >> 13;
- if (_duckLevel < _duckFloor)
- _duckLevel = _duckFloor;
- }
- } else if (_duckLevel < 256) {
- _duckLevel += (_duckRate * frameCount) >> 15;
- if (_duckLevel > 256)
- _duckLevel = 256;
- }
}
} // End of namespace Grim
diff --git a/engines/grim/movie/smush_sound.h b/engines/grim/movie/smush_sound.h
index ffcf93e77c8..ec4a968d95b 100644
--- a/engines/grim/movie/smush_sound.h
+++ b/engines/grim/movie/smush_sound.h
@@ -27,7 +27,7 @@
namespace Grim {
#define SOU_MAX_CHANNELS 16
-#define SOU_FREE_ID -255
+#define SOU_MASTER_VOLUME 127
class SmushTimer;
class SmushDriver;
@@ -43,7 +43,6 @@ struct SmushSoundChannel {
uint8 state;
uint8 format;
int soundClass;
- int soundId;
int age;
int dataAreaSize;
int writePosition;
@@ -68,8 +67,6 @@ struct SmushSoundPlayback {
int tailBytes;
uint8 previousFormat;
int gain;
- int repeatAccumulator;
- int loopRemaining;
};
class SmushSound {
@@ -80,23 +77,14 @@ private:
SmushSoundChannel _channels[SOU_MAX_CHANNELS];
SmushSoundPlayback _playback[SOU_MAX_CHANNELS];
int _channelCount;
- int _masterVolume;
int _sampleRate;
- int _duckFloor;
- int _duckRate;
- int _duckLevel;
int _resetFlag;
int _bytesPerFrame[6];
- uint8 _groupVolumes[256];
int _pauseCount;
int _engineOk;
int _syncMark;
double _syncAccumulator;
int _fillCounter;
- int _musicParam;
- int _voiceParam;
- int _normalParam;
- int _panInvert;
int _driverRate;
int _reserveRam;
int _bufferSizes[SOU_MAX_CHANNELS + 1];
@@ -104,11 +92,9 @@ private:
int engineInitialize(int rate, int reserveRam, const int *bufferSizes);
int engineTerminate();
int engineAllocChannel(int size, int reserveRam);
- uint8 getGroupVolume(uint8 index);
- void setGroupVolume(uint8 index, uint8 volume);
int souEngineMarkSyncTime();
int souEngineExternalSync(double ticks);
- int souEngineStartCreative(const byte *data, int size, int soundId, int volume, int pan, int soundClass);
+ int souEngineStartStream(const byte *data, int size, uint32 volume, int pan, int soundClass, int tag1, int chunkCounter, int tag2);
public:
SmushSound(SmushTimer *timer, SmushDriver *driver);
@@ -117,24 +103,12 @@ public:
int moduleStartup();
int moduleShutdown();
- int souEngineStart(const byte *data, int soundId, int volume, int pan, int soundClass);
- void souEngineStop(int soundId);
+ int souEngineStart(const byte *data, int volume, int pan, int soundClass);
void souEnginePause();
int souEngineResume();
void souEngineStopAll();
- void souEngineSetVol(int soundId, int volume);
- int souEngineGetVol(int soundId);
- void souEngineSetPan(int soundId, int pan);
- int souEngineGetPan(int soundId);
- int souEngineSetHVol(int volume);
- int souEngineGetHVol();
- int souEngineSetRate(int rate);
int souEngineGetRate();
- int souEngineQuery(int soundId);
int souEngineQueryTime(int channelIndex);
- int souEngineSetParam(int soundId, int param, int value1, int value2);
- int souEngineGetParam(int soundId, int param, int *value1, int *value2);
- int souEngineStartStream(const byte *data, int size, int soundId, uint32 volume, int pan, int soundClass, int tag1, int chunkCounter, int tag2, int copyFlag);
void souEngineFill(int frameCount, SmushDriver *driver);
};
diff --git a/engines/grim/movie/smush_wave.cpp b/engines/grim/movie/smush_wave.cpp
index fb54c9ccfd0..720a1cc4e0c 100644
--- a/engines/grim/movie/smush_wave.cpp
+++ b/engines/grim/movie/smush_wave.cpp
@@ -119,7 +119,7 @@ void SmushWave::flobdecDecodeBody(const byte *body) {
decompressVima(data, (int16 *)(packet + 54), pcmSize, _vimaTable, true);
}
- _sound->souEngineStart(packet, -1, 127, 0, 64);
+ _sound->souEngineStart(packet, 127, 0, 64);
_chunkCounter++;
}
}
Commit: 9146a2248f3ec868a419ba35d49e5772507ac669
https://github.com/scummvm/scummvm/commit/9146a2248f3ec868a419ba35d49e5772507ac669
Author: AndywinXp (andywinxp at gmail.com)
Date: 2026-11-01T22:18:07+01:00
Commit Message:
GRIM: SMUSH: Remove unused mixer code
Changed paths:
engines/grim/movie/smush_mixer.cpp
engines/grim/movie/smush_mixer.h
engines/grim/movie/smush_sound.cpp
diff --git a/engines/grim/movie/smush_mixer.cpp b/engines/grim/movie/smush_mixer.cpp
index 3b4e29111a7..412ac648c33 100644
--- a/engines/grim/movie/smush_mixer.cpp
+++ b/engines/grim/movie/smush_mixer.cpp
@@ -67,7 +67,7 @@ int SmushMixer::souMixerStart() {
return 0;
}
-void SmushMixer::souMixerMix(const uint8 *srcBuf, int32 inFrameCount, int wordSize, int channelCount, uint32 feedSize, int32 mixBufStartIndex, int volume, int pan, int signedFlag, int swapEndiannessFlag) {
+void SmushMixer::souMixerMix(const uint8 *srcBuf, int32 inFrameCount, int wordSize, int channelCount, uint32 feedSize, int32 mixBufStartIndex, int volume, int pan) {
// The original could divide by zero here, let's avoid that...
if (!_mixBuf || !srcBuf || !inFrameCount || !feedSize)
return;
@@ -86,25 +86,12 @@ void SmushMixer::souMixerMix(const uint8 *srcBuf, int32 inFrameCount, int wordSi
int leftAmp = _mixerTable[17 * volumeIndex + 8 - panOffset];
int rightAmp = _mixerTable[17 * volumeIndex + 8 + panOffset];
- if (wordSize == 8) {
+ if (wordSize == 8)
mixMono8(srcBuf, inFrameCount, feedSize, mixBufStartIndex, leftAmp, rightAmp);
- } else if (signedFlag) {
- if (swapEndiannessFlag)
- mixMono16Swap((const uint16 *)srcBuf, inFrameCount, feedSize, mixBufStartIndex, leftAmp, rightAmp);
- else
- mixMono16((const int16 *)srcBuf, inFrameCount, feedSize, mixBufStartIndex, leftAmp, rightAmp);
- } else {
- mixMono16Unsigned((const uint16 *)srcBuf, inFrameCount, feedSize, mixBufStartIndex, leftAmp, rightAmp);
- }
- } else if (wordSize == 8) {
- mixStereo8(srcBuf, inFrameCount, feedSize, mixBufStartIndex, volume + 1);
- } else if (signedFlag) {
- if (swapEndiannessFlag)
- mixStereo16Swap((const uint16 *)srcBuf, inFrameCount, feedSize, mixBufStartIndex, volume + 1);
else
- mixStereo16((const int16 *)srcBuf, inFrameCount, feedSize, mixBufStartIndex, volume + 1);
+ mixMono16((const int16 *)srcBuf, inFrameCount, feedSize, mixBufStartIndex, leftAmp, rightAmp);
} else {
- mixStereo16Unsigned((const uint16 *)srcBuf, inFrameCount, feedSize, mixBufStartIndex, volume + 1);
+ mixStereo16((const int16 *)srcBuf, inFrameCount, feedSize, mixBufStartIndex, volume + 1);
}
}
@@ -192,67 +179,6 @@ void SmushMixer::mixMono8(const uint8 *srcBuf, int32 inFrameCount, uint32 feedSi
}
}
-void SmushMixer::mixStereo8(const uint8 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int volume) {
- int32 *mixBufCurCell = &_mixBuf[2 * mixBufStartIndex];
- uint32 ratio = ((uint32)inFrameCount << 16) / feedSize;
- uint32 position = 0;
- int32 left, right, interpolatedLeft, interpolatedRight;
-
- switch (ratio) {
- case 0x8000:
- for (int i = 0; i < inFrameCount - 1; i++) {
- left = (srcBuf[0] ^ 0x80) << 8;
- right = (srcBuf[1] ^ 0x80) << 8;
- interpolatedLeft = ((srcBuf[0] + srcBuf[2]) << 7) ^ 0x8000;
- interpolatedRight = ((srcBuf[1] + srcBuf[3]) << 7) ^ 0x8000;
-
- mixBufCurCell[0] += left * volume;
- mixBufCurCell[1] += right * volume;
- mixBufCurCell[2] += interpolatedLeft * volume;
- mixBufCurCell[3] += interpolatedRight * volume;
-
- srcBuf += 2;
- mixBufCurCell += 4;
- }
-
- left = (srcBuf[0] ^ 0x80) << 8;
- right = (srcBuf[1] ^ 0x80) << 8;
- mixBufCurCell[0] += left * volume;
- mixBufCurCell[1] += right * volume;
- mixBufCurCell[2] += left * volume;
- mixBufCurCell[3] += right * volume;
- break;
- case 0x10000:
- for (uint32 i = 0; i < 2 * feedSize; i++) {
- mixBufCurCell[0] += ((srcBuf[0] ^ 0x80) << 8) * volume;
-
- srcBuf++;
- mixBufCurCell++;
- }
- break;
- case 0x20000:
- for (uint32 i = 0; i < feedSize; i++) {
- mixBufCurCell[0] += ((srcBuf[0] ^ 0x80) << 8) * volume;
- mixBufCurCell[1] += ((srcBuf[1] ^ 0x80) << 8) * volume;
-
- srcBuf += 4;
- mixBufCurCell += 2;
- }
- break;
- default:
- for (uint32 i = 0; i < feedSize; i++) {
- mixBufCurCell[0] += ((srcBuf[0] ^ 0x80) << 8) * volume;
- mixBufCurCell[1] += ((srcBuf[1] ^ 0x80) << 8) * volume;
-
- mixBufCurCell += 2;
- position += 2 * ratio;
- srcBuf += position >> 16;
- position &= 0xFFFF;
- }
- break;
- }
-}
-
void SmushMixer::mixMono16(const int16 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int leftAmp, int rightAmp) {
int32 *mixBufCurCell = &_mixBuf[2 * mixBufStartIndex];
uint32 ratio = ((uint32)inFrameCount << 16) / feedSize;
@@ -364,243 +290,6 @@ void SmushMixer::mixStereo16(const int16 *srcBuf, int32 inFrameCount, uint32 fee
}
}
-void SmushMixer::mixMono16Unsigned(const uint16 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int leftAmp, int rightAmp) {
- int32 *mixBufCurCell = &_mixBuf[2 * mixBufStartIndex];
- uint32 ratio = ((uint32)inFrameCount << 16) / feedSize;
- uint32 position = 0;
- int32 sample, interpolated;
-
- switch (ratio) {
- case 0x8000:
- for (int i = 0; i < inFrameCount - 1; i++) {
- sample = (int16)(srcBuf[0] ^ 0x8000);
- // The disasm interpolates with the sample two positions ahead
- interpolated = (sample + (int16)(srcBuf[2] ^ 0x8000)) >> 1;
-
- mixBufCurCell[0] += sample * leftAmp;
- mixBufCurCell[1] += sample * rightAmp;
- mixBufCurCell[2] += interpolated * leftAmp;
- mixBufCurCell[3] += interpolated * rightAmp;
-
- srcBuf++;
- mixBufCurCell += 4;
- }
-
- sample = (int16)(srcBuf[0] ^ 0x8000);
- mixBufCurCell[0] += sample * leftAmp;
- mixBufCurCell[1] += sample * rightAmp;
- mixBufCurCell[2] += sample * leftAmp;
- mixBufCurCell[3] += sample * rightAmp;
- break;
- case 0x10000:
- for (uint32 i = 0; i < feedSize; i++) {
- sample = (int16)(srcBuf[0] ^ 0x8000);
- mixBufCurCell[0] += sample * leftAmp;
- mixBufCurCell[1] += sample * rightAmp;
-
- srcBuf++;
- mixBufCurCell += 2;
- }
- break;
- case 0x20000:
- for (uint32 i = 0; i < feedSize; i++) {
- sample = (int16)(srcBuf[0] ^ 0x8000);
- mixBufCurCell[0] += sample * leftAmp;
- mixBufCurCell[1] += sample * rightAmp;
-
- srcBuf += 2;
- mixBufCurCell += 2;
- }
- break;
- default:
- for (uint32 i = 0; i < feedSize; i++) {
- sample = (int16)(srcBuf[0] ^ 0x8000);
- mixBufCurCell[0] += sample * leftAmp;
- mixBufCurCell[1] += sample * rightAmp;
-
- mixBufCurCell += 2;
- position += ratio;
- srcBuf += position >> 16;
- position &= 0xFFFF;
- }
- break;
- }
-}
-
-void SmushMixer::mixStereo16Unsigned(const uint16 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int volume) {
- int32 *mixBufCurCell = &_mixBuf[2 * mixBufStartIndex];
- uint32 ratio = ((uint32)inFrameCount << 16) / feedSize;
- uint32 position = 0;
- int32 left, right;
-
- switch (ratio) {
- case 0x8000:
- for (int i = 0; i < inFrameCount - 1; i++) {
- left = (int16)(srcBuf[0] ^ 0x8000);
- right = (int16)(srcBuf[1] ^ 0x8000);
-
- mixBufCurCell[0] += left * volume;
- mixBufCurCell[1] += right * volume;
- mixBufCurCell[2] += ((left + (int16)(srcBuf[2] ^ 0x8000)) >> 1) * volume;
- mixBufCurCell[3] += ((right + (int16)(srcBuf[3] ^ 0x8000)) >> 1) * volume;
-
- srcBuf += 2;
- mixBufCurCell += 4;
- }
-
- left = (int16)(srcBuf[0] ^ 0x8000);
- right = (int16)(srcBuf[1] ^ 0x8000);
- mixBufCurCell[0] += left * volume;
- mixBufCurCell[1] += right * volume;
- mixBufCurCell[2] += left * volume;
- mixBufCurCell[3] += right * volume;
- break;
- case 0x10000:
- for (uint32 i = 0; i < 2 * feedSize; i++) {
- mixBufCurCell[0] += (int16)(srcBuf[0] ^ 0x8000) * volume;
-
- srcBuf++;
- mixBufCurCell++;
- }
- break;
- case 0x20000:
- for (uint32 i = 0; i < feedSize; i++) {
- mixBufCurCell[0] += (int16)(srcBuf[0] ^ 0x8000) * volume;
- mixBufCurCell[1] += (int16)(srcBuf[1] ^ 0x8000) * volume;
-
- srcBuf += 4;
- mixBufCurCell += 2;
- }
- break;
- default:
- for (uint32 i = 0; i < feedSize; i++) {
- mixBufCurCell[0] += (int16)(srcBuf[0] ^ 0x8000) * volume;
- mixBufCurCell[1] += (int16)(srcBuf[1] ^ 0x8000) * volume;
-
- mixBufCurCell += 2;
- position += 2 * ratio;
- srcBuf += position >> 16;
- position &= 0xFFFF;
- }
- break;
- }
-}
-
-void SmushMixer::mixMono16Swap(const uint16 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int leftAmp, int rightAmp) {
- int32 *mixBufCurCell = &_mixBuf[2 * mixBufStartIndex];
- uint32 ratio = ((uint32)inFrameCount << 16) / feedSize;
- uint32 position = 0;
- int32 sample, interpolated;
-
- switch (ratio) {
- case 0x8000:
- for (int i = 0; i < inFrameCount - 1; i++) {
- sample = (uint16)SWAP_BYTES_16(srcBuf[0]);
- interpolated = (int16)SWAP_BYTES_16((uint16)(((int16)srcBuf[0] + (int16)srcBuf[2]) >> 1));
-
- mixBufCurCell[0] += sample * leftAmp;
- mixBufCurCell[1] += sample * rightAmp;
- mixBufCurCell[2] += interpolated * leftAmp;
- mixBufCurCell[3] += interpolated * rightAmp;
-
- srcBuf++;
- mixBufCurCell += 4;
- }
-
- sample = (int16)SWAP_BYTES_16(srcBuf[0]);
- mixBufCurCell[0] += sample * leftAmp;
- mixBufCurCell[1] += sample * rightAmp;
- mixBufCurCell[2] += sample * leftAmp;
- mixBufCurCell[3] += sample * rightAmp;
- break;
- case 0x10000:
- for (uint32 i = 0; i < feedSize; i++) {
- sample = (uint16)SWAP_BYTES_16(srcBuf[0]);
- mixBufCurCell[0] += sample * leftAmp;
- mixBufCurCell[1] += sample * rightAmp;
-
- srcBuf++;
- mixBufCurCell += 2;
- }
- break;
- case 0x20000:
- for (uint32 i = 0; i < feedSize; i++) {
- sample = (uint16)SWAP_BYTES_16(srcBuf[0]);
- mixBufCurCell[0] += sample * leftAmp;
- mixBufCurCell[1] += sample * rightAmp;
-
- srcBuf += 2;
- mixBufCurCell += 2;
- }
- break;
- default:
- for (uint32 i = 0; i < feedSize; i++) {
- sample = (uint16)SWAP_BYTES_16(srcBuf[0]);
- mixBufCurCell[0] += sample * leftAmp;
- mixBufCurCell[1] += sample * rightAmp;
-
- mixBufCurCell += 2;
- position += ratio;
- srcBuf += position >> 16;
- position &= 0xFFFF;
- }
- break;
- }
-}
-
-void SmushMixer::mixStereo16Swap(const uint16 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int volume) {
- int32 *mixBufCurCell = &_mixBuf[2 * mixBufStartIndex];
- uint32 ratio = ((uint32)inFrameCount << 16) / feedSize;
- uint32 position = 0;
-
- switch (ratio) {
- case 0x8000:
- for (int i = 0; i < inFrameCount - 1; i++) {
- mixBufCurCell[0] += volume * (uint16)SWAP_BYTES_16(srcBuf[0]);
- mixBufCurCell[1] += volume * (uint16)SWAP_BYTES_16(srcBuf[1]);
- mixBufCurCell[2] += volume * (uint16)SWAP_BYTES_16((uint16)(((int16)srcBuf[0] + (int16)srcBuf[2]) >> 1));
- mixBufCurCell[3] += volume * (uint16)SWAP_BYTES_16((uint16)(((int16)srcBuf[1] + (int16)srcBuf[3]) >> 1));
-
- srcBuf += 2;
- mixBufCurCell += 4;
- }
-
- mixBufCurCell[0] += volume * (uint16)SWAP_BYTES_16(srcBuf[0]);
- mixBufCurCell[1] += volume * (uint16)SWAP_BYTES_16(srcBuf[1]);
- mixBufCurCell[2] += volume * (uint16)SWAP_BYTES_16(srcBuf[0]);
- mixBufCurCell[3] += volume * (uint16)SWAP_BYTES_16(srcBuf[1]);
- break;
- case 0x10000:
- for (uint32 i = 0; i < 2 * feedSize; i++) {
- mixBufCurCell[0] += volume * (uint16)SWAP_BYTES_16(srcBuf[0]);
-
- srcBuf++;
- mixBufCurCell++;
- }
- break;
- case 0x20000:
- for (uint32 i = 0; i < feedSize; i++) {
- mixBufCurCell[0] += volume * (uint16)SWAP_BYTES_16(srcBuf[0]);
- mixBufCurCell[1] += volume * (uint16)SWAP_BYTES_16(srcBuf[1]);
-
- srcBuf += 4;
- mixBufCurCell += 2;
- }
- break;
- default:
- for (uint32 i = 0; i < feedSize; i++) {
- mixBufCurCell[0] += volume * (uint16)SWAP_BYTES_16(srcBuf[0]);
- mixBufCurCell[1] += volume * (uint16)SWAP_BYTES_16(srcBuf[1]);
-
- mixBufCurCell += 2;
- position += 2 * ratio;
- srcBuf += position >> 16;
- position &= 0xFFFF;
- }
- break;
- }
-}
-
SmushDriverStream::SmushDriverStream(SmushDriver *driver) {
_driver = driver;
}
@@ -623,10 +312,7 @@ SmushDriver::SmushDriver(SmushTimer *timer, bool remastered) {
_ringPlayPosition = 0;
_lastBlock = -1;
_driverRate = 0;
- _driverVolume = 0;
_fillTimer = 0;
- memset(_volumeTable, 0, sizeof(_volumeTable));
- _volumeTableBuilt = false;
}
SmushDriver::~SmushDriver() {
@@ -664,63 +350,10 @@ void SmushDriver::sndDrivTerminate() {
sndDrivCloseRing();
}
-int SmushDriver::sndDrivSetVol(int volume) {
- if (!_volumeTableBuilt) {
- _volumeTable[0] = -10000;
- _volumeTable[1] = -7000;
- _volumeTable[2] = -6000;
- _volumeTable[3] = -5200;
- _volumeTable[4] = -5000;
- _volumeTable[5] = -4500;
- _volumeTable[6] = -4200;
- _volumeTable[7] = -4100;
- _volumeTable[8] = -4000;
-
- int position = 16000;
- for (int i = 8; i < 32; i++) {
- int value = (int)(((uint64)2987803337LL * (uint64)(position - 16000)) >> 32) >> 4;
- position += 2000;
- _volumeTable[i] = value + (value >> 31) - 4000;
- }
-
- position = 32000;
- for (int i = 32; i < 64; i++) {
- int value = (int)(((uint64)2216757315LL * (uint64)(position - 32000)) >> 32) >> 4;
- position += 1000;
- _volumeTable[i] = value + (value >> 31) - 2000;
- }
-
- position = 64000;
- for (int i = 64; i < 128; i++) {
- int value = (int)(((uint64)2181570691LL * (uint64)(position - 64000)) >> 32) >> 5;
- position += 1000;
- _volumeTable[i] = value + (value >> 31) - 1000;
- }
-
- _volumeTableBuilt = true;
- }
-
- _driverVolume = volume & 0x7F;
-
- int centibels = _volumeTable[_driverVolume];
- int hostVolume = (int)(Audio::Mixer::kMaxChannelVolume * pow(10.0, centibels / 2000.0) + 0.5);
- _system->setChannelVolume(_soundHandle, hostVolume);
-
- return 0;
-}
-
-int SmushDriver::sndDrivGetVol() {
- return _driverVolume;
-}
-
int SmushDriver::sndDrivGetRate() {
return _driverRate;
}
-int SmushDriver::sndDrivDummy(int rate) {
- return rate;
-}
-
void SmushDriver::sndDrivFillThread() {
int playBlock = sndDrivGetPlayBlock();
if (playBlock != -1) {
@@ -746,48 +379,21 @@ void SmushDriver::sndDrivFillThread() {
_timer->sysReset(_fillTimer, 1193180 / (_driverRate / 1024));
}
-void SmushDriver::sndDrivMix(const uint8 *srcBuf, int32 mixBufStartIndex, uint32 feedSize, int32 srcByteCount, int reserved, int volume, int pan, int format) {
- int channelCount, wordSize, signedFlag;
+void SmushDriver::sndDrivMix(const uint8 *srcBuf, int32 mixBufStartIndex, uint32 feedSize, int32 srcByteCount, int volume, int pan, int format) {
+ int channelCount;
+ int wordSize;
- switch (format) {
- case 0:
+ // The disasm mixes the tail of a first stream, which has no format yet, as 8 bit mono
+ if (format == 0) {
channelCount = 1;
wordSize = 8;
- signedFlag = 1;
- break;
- case 1:
- channelCount = 2;
- wordSize = 8;
- signedFlag = 1;
- break;
- case 2:
- channelCount = 1;
- wordSize = 16;
- signedFlag = 0;
- break;
- case 3:
- channelCount = 2;
- wordSize = 16;
- signedFlag = 0;
- break;
- case 4:
- channelCount = 1;
- wordSize = 16;
- signedFlag = 1;
- break;
- case 5:
- channelCount = 2;
+ } else {
+ // Movies only have signed 16 bit audio: format 4 is mono, format 5 is stereo
+ channelCount = (format == 5) ? 2 : 1;
wordSize = 16;
- signedFlag = 1;
- break;
- default:
- channelCount = format;
- wordSize = format;
- signedFlag = format;
- break;
}
- _mixer.souMixerMix(srcBuf, srcByteCount / (channelCount * (wordSize >> 3)), wordSize, channelCount, feedSize, mixBufStartIndex, volume, (pan + 127) / 2, signedFlag, 0);
+ _mixer.souMixerMix(srcBuf, srcByteCount / (channelCount * (wordSize >> 3)), wordSize, channelCount, feedSize, mixBufStartIndex, volume, (pan + 127) / 2);
}
void SmushDriver::sndDrivFillThreadCallback(int timer, void *context) {
diff --git a/engines/grim/movie/smush_mixer.h b/engines/grim/movie/smush_mixer.h
index cefba4a54f2..299d82c8db1 100644
--- a/engines/grim/movie/smush_mixer.h
+++ b/engines/grim/movie/smush_mixer.h
@@ -43,13 +43,8 @@ private:
byte _mixerTable[17 * 17];
void mixMono8(const uint8 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int leftAmp, int rightAmp);
- void mixStereo8(const uint8 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int volume);
void mixMono16(const int16 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int leftAmp, int rightAmp);
void mixStereo16(const int16 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int volume);
- void mixMono16Unsigned(const uint16 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int leftAmp, int rightAmp);
- void mixStereo16Unsigned(const uint16 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int volume);
- void mixMono16Swap(const uint16 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int leftAmp, int rightAmp);
- void mixStereo16Swap(const uint16 *srcBuf, int32 inFrameCount, uint32 feedSize, int32 mixBufStartIndex, int volume);
public:
SmushMixer();
@@ -58,7 +53,7 @@ public:
int souMixerInitialize(int outChannelCount, int mixBufSize);
int souMixerTerminate();
int souMixerStart();
- void souMixerMix(const uint8 *srcBuf, int32 inFrameCount, int wordSize, int channelCount, uint32 feedSize, int32 mixBufStartIndex, int volume, int pan, int signedFlag, int swapEndiannessFlag);
+ void souMixerMix(const uint8 *srcBuf, int32 inFrameCount, int wordSize, int channelCount, uint32 feedSize, int32 mixBufStartIndex, int volume, int pan);
int souMixerEnd(int16 *destBuf, int frameCount);
};
@@ -95,10 +90,7 @@ private:
int _lastBlock;
int _driverRate;
bool _remastered;
- int _driverVolume;
int _fillTimer;
- int _volumeTable[128];
- bool _volumeTableBuilt;
int sndDrivOpenRing();
void sndDrivCloseRing();
@@ -113,12 +105,9 @@ public:
int sndDrivInitialize(int rate, int channelCount, SmushSound *sound);
void sndDrivTerminate();
- int sndDrivSetVol(int volume);
- int sndDrivGetVol();
int sndDrivGetRate();
- int sndDrivDummy(int rate);
void sndDrivFillThread();
- void sndDrivMix(const uint8 *srcBuf, int32 mixBufStartIndex, uint32 feedSize, int32 srcByteCount, int reserved, int volume, int pan, int format);
+ void sndDrivMix(const uint8 *srcBuf, int32 mixBufStartIndex, uint32 feedSize, int32 srcByteCount, int volume, int pan, int format);
static void sndDrivFillThreadCallback(int timer, void *context);
int ringRead(int16 *buffer, int numSamples);
diff --git a/engines/grim/movie/smush_sound.cpp b/engines/grim/movie/smush_sound.cpp
index 7ced0c738eb..a3fdd0447e0 100644
--- a/engines/grim/movie/smush_sound.cpp
+++ b/engines/grim/movie/smush_sound.cpp
@@ -449,7 +449,7 @@ void SmushSound::souEngineFill(int frameCount, SmushDriver *driver) {
else
chunkFrames = _sampleRate * (chunkBytes / _bytesPerFrame[rt->previousFormat]) / rt->previousRate;
- driver->sndDrivMix(chan->stagingBuffer - rt->tailBytes + 6144, destOffset, chunkFrames, chunkBytes, 1,
+ driver->sndDrivMix(chan->stagingBuffer - rt->tailBytes + 6144, destOffset, chunkFrames, chunkBytes,
rt->previousVolume * rt->tailBytes * SOU_MASTER_VOLUME / 780288, chan->pan, rt->previousFormat);
destOffset += chunkFrames;
@@ -519,7 +519,7 @@ void SmushSound::souEngineFill(int frameCount, SmushDriver *driver) {
else
chunkFrames = _sampleRate * (chunkBytes / _bytesPerFrame[rt->format]) / rt->rate;
- driver->sndDrivMix(rt->dataStart + ringPosition, destOffset, chunkFrames, chunkBytes, 1,
+ driver->sndDrivMix(rt->dataStart + ringPosition, destOffset, chunkFrames, chunkBytes,
(rt->gain * volume) >> 4, chan->pan, rt->format);
rt->remaining -= chunkBytes;
Commit: a3300abb9f2fd327a94f6e4d9a6c44166adb61cd
https://github.com/scummvm/scummvm/commit/a3300abb9f2fd327a94f6e4d9a6c44166adb61cd
Author: AndywinXp (andywinxp at gmail.com)
Date: 2026-11-01T22:18:07+01:00
Commit Message:
GRIM: SMUSH: Remove unused demo animator code
Changed paths:
engines/grim/movie/smush_anim.cpp
engines/grim/movie/smush_anim.h
engines/grim/movie/smush_animator.cpp
engines/grim/movie/smush_animator.h
diff --git a/engines/grim/movie/smush_anim.cpp b/engines/grim/movie/smush_anim.cpp
index 72da947b2cd..1cdab9886cd 100644
--- a/engines/grim/movie/smush_anim.cpp
+++ b/engines/grim/movie/smush_anim.cpp
@@ -202,7 +202,6 @@ SmushDemoPlayer::SmushDemoPlayer(SmushDemoVideo *video, SmushDemoDriver *driver)
_lastResult = 0;
_frameReady = 0;
_paletteFlag = 0;
- _annotation = 0;
_staged = 0;
_audioPaused = 0;
_audioStartFrame = 0;
@@ -334,10 +333,6 @@ int SmushDemoPlayer::chunkCallback(const byte *chunk, int size, int flags) {
}
}
-void SmushDemoPlayer::setAnnotation(int value) {
- _annotation = value;
-}
-
void SmushDemoPlayer::setFrameRate(int fps) {
_fps = (int16)fps;
if (_startTime)
@@ -536,14 +531,7 @@ int SmushDemoPlayer::initialize(int bufferBytes, int width, int height) {
return 0;
}
- if (_anim->anmBuffersInitialize(0, 0)) {
- _anim->anmTerminate();
- _stream->streamTerminate();
- return 0;
- }
-
if (_anim->anmRegisterCodecs(width, height)) {
- _anim->anmBuffersTerminate();
_anim->anmTerminate();
_stream->streamTerminate();
return 0;
@@ -554,13 +542,12 @@ int SmushDemoPlayer::initialize(int bufferBytes, int width, int height) {
void SmushDemoPlayer::terminate() {
_anim->anmUnregisterCodecs();
- _anim->anmBuffersTerminate();
_anim->anmTerminate();
_stream->streamTerminate();
}
int SmushDemoPlayer::open(const Common::String &name, int flags, int offset) {
- if (!name.empty() && !Common::File::exists(Common::Path(name)))
+ if (!Common::File::exists(Common::Path(name)))
return 1;
memset(&_audio, 0, sizeof(_audio));
@@ -570,12 +557,8 @@ int SmushDemoPlayer::open(const Common::String &name, int flags, int offset) {
return 1;
_anim->animatorInitialize();
- _anim->animatorNewAnim(name, 0, -1, -1, 0, offset);
-
- if (!name.empty())
- _stream->streamFill(0x20000);
- else
- _stream->streamInitMemory();
+ _anim->animatorNewAnim(name, offset);
+ _stream->streamFill(0x20000);
memset(_palette, 0, sizeof(_palette));
_paused = 0;
@@ -584,7 +567,6 @@ int SmushDemoPlayer::open(const Common::String &name, int flags, int offset) {
_lastResult = 0;
_frameReady = 0;
_paletteFlag = 0;
- _annotation = 0;
_staged = 0;
_audioPaused = 0;
_audioStartFrame = 0;
@@ -604,7 +586,7 @@ int SmushDemoPlayer::open(const Common::String &name, int flags, int offset) {
_fadeStart = 0;
_fadeRemaining = 0;
_fadeSteps = 1;
- _streamStop = name.empty() ? 1 : 0;
+ _streamStop = 0;
return 0;
}
@@ -617,10 +599,9 @@ int SmushDemoPlayer::close() {
return 0;
}
-int SmushDemoPlayer::update(int *frameDecoded, int *finished, int *annotation, int *paletteChanged, const uint32 **palette) {
+int SmushDemoPlayer::update(int *frameDecoded, int *finished, int *paletteChanged, const uint32 **palette) {
*finished = 0;
*frameDecoded = 0;
- *annotation = 0;
*paletteChanged = 0;
*palette = nullptr;
@@ -629,12 +610,7 @@ int SmushDemoPlayer::update(int *frameDecoded, int *finished, int *annotation, i
return 0;
if (!_frameReady) {
- int saved = _annotation;
- _annotation = 0;
stepAnimation();
- if (!_frameReady && !_annotation)
- _annotation = saved;
-
audioFill();
}
@@ -662,7 +638,6 @@ int SmushDemoPlayer::update(int *frameDecoded, int *finished, int *annotation, i
*paletteChanged = _paletteFlag;
*palette = _palette;
- *annotation = _annotation;
_paletteFlag = 0;
_frameReady = 0;
_lastUpdate = time;
@@ -730,11 +705,10 @@ int SmushDemoPlayer::smushPlayStartMovie(const Common::String &name, int flags,
int SmushDemoPlayer::smushPlayUpdateMovie(int *finished) {
int frameDecoded;
- int annotation;
int paletteChanged;
const uint32 *palette;
- update(&frameDecoded, finished, &annotation, &paletteChanged, &palette);
+ update(&frameDecoded, finished, &paletteChanged, &palette);
if (!*finished && frameDecoded) {
if (paletteChanged)
_video->videoSetPalette(palette, 0, 256);
diff --git a/engines/grim/movie/smush_anim.h b/engines/grim/movie/smush_anim.h
index 310564d7c72..028ac98a671 100644
--- a/engines/grim/movie/smush_anim.h
+++ b/engines/grim/movie/smush_anim.h
@@ -81,7 +81,6 @@ private:
int _lastResult;
int _frameReady;
int _paletteFlag;
- int _annotation;
int _staged;
int _audioPaused;
int _audioStartFrame;
@@ -123,7 +122,6 @@ public:
int postFrame(int frame, int frameCount) override;
int frameCallback(int flags, bool paletteChanged, bool display, int paletteState) override;
int chunkCallback(const byte *chunk, int size, int flags) override;
- void setAnnotation(int value) override;
void setFrameRate(int fps) override;
void setFrameBuffer(byte *buffer, int x, int y, int sourceX, int sourceY, int width, int height) override;
@@ -131,7 +129,7 @@ public:
void terminate();
int open(const Common::String &name, int flags, int offset);
int close();
- int update(int *frameDecoded, int *finished, int *annotation, int *paletteChanged, const uint32 **palette);
+ int update(int *frameDecoded, int *finished, int *paletteChanged, const uint32 **palette);
void pause();
void resume();
void getFrameBuffer(const byte **pixels, int *width, int *height);
diff --git a/engines/grim/movie/smush_animator.cpp b/engines/grim/movie/smush_animator.cpp
index c93a11cb75c..589dff59b01 100644
--- a/engines/grim/movie/smush_animator.cpp
+++ b/engines/grim/movie/smush_animator.cpp
@@ -178,10 +178,8 @@ SmushDemoStream::SmushDemoStream() {
_rewindPos = 0;
_alignSkip = 0;
_file = nullptr;
- _fileIsMemory = false;
_fileOffset = 0;
_fileSize = 0;
- _busy = 0;
_restart = 0;
_hasData = 0;
_released = 0;
@@ -220,20 +218,6 @@ void SmushDemoStream::streamTerminate() {
_ring = nullptr;
}
-void SmushDemoStream::streamInitMemory() {
- _restart = 1;
- _hasData = 1;
- _busy = 0;
- _rewindPos = 0;
- _alignSkip = 0;
- _writePos = _ringSize - 1;
- _readPos = 0;
- _held = 0;
- _fileIsMemory = true;
- _fileOffset = 0;
- _fileSize = _ringSize - 1;
-}
-
void SmushDemoStream::streamReset() {
_writePos = 0;
_readPos = 0;
@@ -245,9 +229,6 @@ void SmushDemoStream::streamReset() {
delete _file;
_file = nullptr;
}
-
- _fileIsMemory = false;
- _busy = 0;
}
void SmushDemoStream::streamOpen(const Common::String &name, int offset) {
@@ -260,20 +241,17 @@ int SmushDemoStream::streamDoOpen() {
if (!_openPending)
return -1;
- if (!_openName.empty()) {
- Common::File *file = new Common::File();
- if (!file->open(Common::Path(_openName))) {
- delete file;
- _openPending = false;
- return -2;
- }
-
- delete _file;
- _file = file;
- _fileIsMemory = false;
- _fileSize = _file->size();
+ Common::File *file = new Common::File();
+ if (!file->open(Common::Path(_openName))) {
+ delete file;
+ _openPending = false;
+ return -2;
}
+ delete _file;
+ _file = file;
+ _fileSize = _file->size();
+
_file->seek(_openOffset & 0xFFFFF800);
_alignSkip = _openOffset & 0x7FF;
_fileOffset = _openOffset & 0xFFFFF800;
@@ -355,7 +333,7 @@ bool SmushDemoStream::streamFill(int limit) {
int SmushDemoStream::streamRead(int size, const byte **data) {
*data = nullptr;
- if (_busy || (!_file && !_fileIsMemory))
+ if (!_file)
return -1;
int wanted = size;
@@ -375,16 +353,6 @@ int SmushDemoStream::streamRead(int size, const byte **data) {
_released = 1;
}
- if (size < 0) {
- if (_restart) {
- _held = 0;
- _readPos = _rewindPos;
- }
-
- _released = 0;
- return -1;
- }
-
if (_restart) {
streamRewind();
_released = 0;
@@ -441,33 +409,11 @@ SmushDemoAnim::SmushDemoAnim(SmushDemoStream *stream, SmushDemoClient *client) {
_frameCount = 1;
_blockSize = 0;
_tag = 0;
- _originX = 0;
- _originY = 0;
- _newAnim = 0;
- _options = 0;
- _seeking = 0;
- _skipping = 0;
_quit = 0;
- _pause = 0;
- _dirty = 0;
- _skipUntil = 0;
- _skipBase = 0;
- _skipLength = 0;
- _flags = 0;
- _overlay = nullptr;
- _preload = nullptr;
- _overlaySize = 0;
- _preloadSize = 0;
- _preloadUsed = 0;
- _preloadCursor = 8;
- _preloadLast = 0;
- _preloadFirst = 0;
- memset(_updateFlags, 0, sizeof(_updateFlags));
memset(_palette, 0, sizeof(_palette));
memset(_paletteAccum, 0, sizeof(_paletteAccum));
memset(_paletteDelta, 0, sizeof(_paletteDelta));
_paletteState = 0;
- _paletteRefresh = 0;
_noDisplay = 0;
_displayPending = 1;
}
@@ -478,9 +424,6 @@ SmushDemoAnim::~SmushDemoAnim() {
delete[] _bitmap->pixels;
delete _bitmap;
}
-
- delete[] _overlay;
- delete[] _preload;
}
int SmushDemoAnim::anmInitialize(int width, int height) {
@@ -511,142 +454,55 @@ int SmushDemoAnim::anmTerminate() {
}
void SmushDemoAnim::animatorInitialize() {
- anmClearUpdateFlags(0);
_stream->streamReset();
- _options = 0;
_frame = 0;
_frameCount = 1;
_tag = 0;
- _originX = 0;
- _originY = 0;
- _newAnim = 0;
- _dirty = 0;
_quit = 0;
- _pause = 0;
- _seeking = 0;
- _skipping = 0;
animatorResetView();
}
-void SmushDemoAnim::animatorSetOrigin(int x, int y) {
- _originX = x;
- _originY = y;
-}
-
void SmushDemoAnim::animatorResetView() {
_rect.x = 0;
_rect.y = 0;
_rect.width = _bitmap ? _bitmap->width : 320;
_rect.height = _bitmap ? _bitmap->height : 200;
- animatorSetOrigin(0, 0);
- anmClearUpdateFlags(0);
- _dirty = 0;
}
-void SmushDemoAnim::animatorNewAnim(const Common::String &name, int options, int skipLength, int skipUntil, int seeking, int offset) {
- _seeking = seeking;
- _flags = options & 0xFFFF;
+void SmushDemoAnim::animatorNewAnim(const Common::String &name, int offset) {
_stream->streamOpen(name, offset + 8);
- _newAnim = 1;
- animatorSetSkip(skipLength, skipUntil);
-}
-
-void SmushDemoAnim::animatorSetSkip(int skipLength, int skipUntil) {
- _skipBase = _frame;
- _skipUntil = skipUntil;
- _skipLength = skipLength;
-}
-
-int SmushDemoAnim::animatorPause(int mode) {
- if (mode == 2) {
- _pause = (_pause == 0);
- return _pause;
- }
-
- if (mode == 4) {
- if (_pause) {
- _pause = 3;
- return 3;
- }
- } else if (mode != 5) {
- _pause = mode;
- }
-
- return _pause;
}
int SmushDemoAnim::animatorReadBlock(int size) {
- if (_skipping) {
- _block = anmPreloadGet(size);
- const byte *discard;
- _stream->streamRead(-1, &discard);
- _blockStatus = _block ? 1 : 0;
- if (!_block) {
- _skipping = 0;
- _skipLength = 0;
- _skipBase = 0;
- _tag = 'w';
- }
- } else {
- _blockStatus = _stream->streamRead(size, &_block);
- if (_blockStatus == 0) {
- _frame = -1;
- _newAnim = 0;
- }
- }
+ _blockStatus = _stream->streamRead(size, &_block);
+ if (_blockStatus == 0)
+ _frame = -1;
return _blockStatus;
}
-int SmushDemoAnim::animatorCheckSkip() {
- if (!_skipping) {
- if (_newAnim) {
- if (_skipLength >= 0 && _frame > _skipBase + _skipLength)
- return 1;
- } else if (_skipUntil >= 0) {
- if (_frame < _skipUntil)
- return 1;
+void SmushDemoAnim::animatorDecodeBlocks(int flags) {
+ _client->preFrame(_frame, _frameCount);
- _skipUntil = -1;
- }
- }
-
- return 0;
-}
-
-int SmushDemoAnim::animatorDecodeBlocks(int x, int y, int flags) {
- if (_client)
- _client->preFrame(_frame, _frameCount);
-
- if (anmDecodeBody(x, y, _block, _blockSize, _options | flags))
- _quit = 1;
-
- if (_client)
- _client->postFrame(_frame, _frameCount);
- else if (_frame == _frameCount)
+ if (anmDecodeBody(_block, _blockSize, flags))
_quit = 1;
- anmDisplay(_options | flags);
- if (_dirty)
- animatorResetView();
+ _client->postFrame(_frame, _frameCount);
- return 0;
+ anmDisplay(flags);
}
void SmushDemoAnim::animatorHandleHeader(const byte *header) {
- _options = _flags & 0xFFFF;
- if (!(_flags & 0x400))
- anmSetPalette(header + 6, 1);
+ anmSetPalette(header + 6, 1);
_frameCount = READ_LE_UINT16(header + 2);
if (header[0] > 1) {
int fps = READ_LE_UINT16(header + 774);
- if (fps && !(_options & 8))
+ if (fps)
_client->setFrameRate(fps);
}
- if (!(_options & 0x40))
- animatorResetView();
+ animatorResetView();
}
int SmushDemoAnim::animatorRender(int flags) {
@@ -658,11 +514,6 @@ int SmushDemoAnim::animatorRender(int flags) {
while (true) {
switch (_tag) {
case 0:
- if (_seeking) {
- _seeking = 0;
- _skipping = 1;
- }
-
animatorReadBlock(8);
if (_blockStatus == -1)
return 11;
@@ -699,54 +550,16 @@ int SmushDemoAnim::animatorRender(int flags) {
break;
_frame++;
- if (_skipping || !animatorCheckSkip()) {
- animatorDecodeBlocks(_originX, _originY, flags);
- if (_frame < _frameCount - 1 || _skipping) {
- if (_pause == 1)
- _tag = 'p';
- else if (_pause == 3)
- _pause = 1;
- } else if (_stream->streamIsReady() || _seeking) {
- _tag = 0;
- } else {
- _tag = 'e';
- }
-
- if (_skipping)
- return 1;
+ animatorDecodeBlocks(flags);
+ if (_frame >= _frameCount - 1 && !_stream->streamIsReady())
+ _tag = 'e';
- return _frame >= 10 ? 0 : 2;
- }
-
- if (_frame >= _frameCount - 1) {
- if (_stream->streamIsReady() || _seeking)
- _tag = 0;
- else
- _tag = 'e';
- }
-
- break;
+ return _frame >= 10 ? 0 : 2;
case 'e':
_frame++;
- if (!animatorCheckSkip())
- animatorDecodeBlocks(_originX, _originY, flags);
-
- if (_stream->streamIsReady() || _seeking)
- _tag = 0;
+ animatorDecodeBlocks(flags);
- return 1;
- case 'p':
- animatorDecodeBlocks(_originX, _originY, flags);
- if (_pause == 1)
- return 1;
-
- if (_pause == 3)
- _pause = 1;
-
- _tag = 0;
- return 1;
- case 'w':
- if (_stream->streamIsReady() || _seeking)
+ if (_stream->streamIsReady())
_tag = 0;
return 1;
@@ -762,94 +575,13 @@ int SmushDemoAnim::animatorRender(int flags) {
}
}
-int SmushDemoAnim::anmBuffersInitialize(int overlaySize, int preloadSize) {
- _overlaySize = overlaySize;
- _preloadSize = preloadSize;
- anmClearUpdateFlags(0);
-
- if (overlaySize)
- _overlay = new byte[overlaySize]();
-
- if (preloadSize)
- _preload = new byte[preloadSize]();
-
- return 0;
-}
-
-void SmushDemoAnim::anmBuffersTerminate() {
- if (_overlaySize) {
- delete[] _overlay;
- _overlay = nullptr;
- }
-
- if (_preloadSize) {
- delete[] _preload;
- _preload = nullptr;
- }
-}
-
-void SmushDemoAnim::anmClearUpdateFlags(int index) {
- if (index > 0) {
- int bit = index - 1;
- if ((bit & 0xF8) < 1024)
- _updateFlags[bit >> 3] &= ~(128 >> (bit & 7));
- } else {
- memset(_updateFlags, 0, sizeof(_updateFlags));
- }
-}
-
-bool SmushDemoAnim::anmTestUpdateFlag(int index) {
- int bit = index - 1;
- return (bit & 0xF8) < 1024 && (_updateFlags[bit >> 3] & (128 >> (bit & 7))) != 0;
-}
-
-void SmushDemoAnim::anmPreloadPut(const byte *chunk, int size) {
- int part = READ_LE_UINT16(chunk + 2);
- int first = READ_LE_UINT16(chunk);
- int used;
-
- if (part) {
- used = _preloadUsed;
- } else {
- used = 0;
- _preloadCursor = 8;
- _preloadUsed = 0;
- _preloadLast = -1;
- _preloadFirst = first;
- }
-
- if (part == _preloadLast + 1) {
- if (size + used < _preloadSize) {
- memcpy(_preload + used, chunk + 10, size - 10);
- _preloadUsed += size - 10;
- }
-
- _preloadLast = part;
- }
-}
-
-const byte *SmushDemoAnim::anmPreloadGet(int size) {
- int next = size + _preloadCursor;
- if (next > _preloadUsed) {
- _preloadCursor = 8;
- return nullptr;
- }
-
- _preloadCursor += size;
- return _preload + next - size;
-}
-
void SmushDemoAnim::anmDisplay(int flags) {
if (!_displayPending)
return;
_displayPending = 0;
- bool display = !_noDisplay && (flags & 0x8000) == 0;
- _client->frameCallback(flags, _paletteState >= 2, display, _paletteState);
- if (_paletteState) {
- _paletteRefresh = 1;
- _paletteState = 0;
- }
+ _client->frameCallback(flags, _paletteState >= 2, !_noDisplay, _paletteState);
+ _paletteState = 0;
}
void SmushDemoAnim::anmSetPalette(const byte *palette, int mode) {
@@ -859,34 +591,11 @@ void SmushDemoAnim::anmSetPalette(const byte *palette, int mode) {
_paletteState = (mode != 0) + 1;
}
-void SmushDemoAnim::anmStoreObject(byte *destination, const byte *chunk) {
- int size = READ_BE_UINT32(chunk + 4) + 8;
- if (size < _overlaySize)
- memcpy(destination, chunk, size);
-}
-
-void SmushDemoAnim::anmDrawObject(int x, int y, const byte *object, int flags) {
- anmFrameObject(x, y, flags, object + 8, READ_BE_UINT32(object + 4));
-}
-
-int SmushDemoAnim::anmFrameObject(int x, int y, int flags, const byte *chunk, int size) {
- int drawX;
- int drawY;
-
- if (flags & 1) {
- drawX = x;
- drawY = y;
- } else {
- drawX = (int16)(READ_LE_UINT16(chunk + 2) + x);
- drawY = (int16)(READ_LE_UINT16(chunk + 4) + y);
- }
-
+int SmushDemoAnim::anmFrameObject(int flags, const byte *chunk) {
+ int drawX = (int16)READ_LE_UINT16(chunk + 2);
+ int drawY = (int16)READ_LE_UINT16(chunk + 4);
int width = (int16)READ_LE_UINT16(chunk + 6);
int height = (int16)READ_LE_UINT16(chunk + 8);
- if (flags & 2) {
- drawX -= width >> 1;
- drawY -= height >> 1;
- }
if (chunk[0] == 48 && _codec48)
return _codec48->decode(_bitmap, &_rect, drawX, drawY, chunk, flags, _client);
@@ -942,167 +651,68 @@ void SmushDemoAnim::anmUnregisterCodecs() {
_blocky8 = nullptr;
}
-int SmushDemoAnim::anmDecodeBody(int x, int y, const byte *block, int size, int flags) {
+int SmushDemoAnim::anmDecodeBody(const byte *block, int size, int flags) {
_displayPending = 1;
- int handled = 0;
- bool skipNext = false;
- bool storePending = false;
- uint32 previousTag = 0;
- const byte *lastObject = nullptr;
const byte *start = block;
_noDisplay = 0;
while (size > 4) {
- handled = 0;
-
if (((block - start) & 1) != 0 && !*block) {
block++;
size--;
}
- if (storePending && _overlay) {
- if (READ_BE_UINT32(block) != MKTAG('I', 'A', 'C', 'T')) {
- anmStoreObject(_overlay, block);
- anmDrawObject(x, y, _overlay, flags);
- storePending = false;
- handled = 1;
- }
- }
-
uint32 tag = READ_BE_UINT32(block);
uint32 chunkSize = READ_BE_UINT32(block + 4);
const byte *payload = block + 8;
- if (skipNext) {
- skipNext = false;
- } else {
- switch (tag) {
- case MKTAG('F', 'T', 'C', 'H'):
- if (_overlay) {
- int offsetX;
- int offsetY;
- if (chunkSize == 12) {
- offsetX = READ_BE_UINT16(payload + 6);
- offsetY = READ_BE_UINT16(payload + 10);
- } else {
- offsetX = (int16)READ_LE_UINT16(payload + 2);
- offsetY = (int16)READ_LE_UINT16(payload + 4);
- }
-
- anmDrawObject(x + offsetX, y + offsetY, _overlay, flags);
- }
+ switch (tag) {
+ case MKTAG('F', 'O', 'B', 'J'):
+ if ((int16)anmFrameObject(flags, payload) > 0)
+ _noDisplay = 1;
- break;
- case MKTAG('F', 'O', 'B', 'J'):
- lastObject = payload;
- if (handled)
- break;
-
- if (previousTag == MKTAG('S', 'K', 'I', 'P')) {
- if ((uint16)anmFrameObject(x, y, flags, payload, chunkSize))
- _noDisplay = 1;
- } else if ((int16)anmFrameObject(x, y, flags, payload, chunkSize) > 0) {
- _noDisplay = 1;
+ break;
+ case MKTAG('I', 'A', 'C', 'T'):
+ _client->chunkCallback(payload, chunkSize, flags);
+ break;
+ case MKTAG('X', 'P', 'A', 'L'): {
+ int mode = READ_LE_UINT16(payload + 2);
+ if (mode == 0 || mode == 512) {
+ if (mode == 512)
+ memcpy(_palette, payload + 1540, 768);
+
+ for (int i = 0; i < 768; i++) {
+ _paletteAccum[i] = _palette[i] << 7;
+ _paletteDelta[i] = (int16)READ_LE_UINT16(payload + 4 + i * 2);
+ }
+ } else {
+ for (int i = 0; i < 768; i++) {
+ _paletteAccum[i] += _paletteDelta[i];
+ _palette[i] = _paletteAccum[i] >> 7;
}
- break;
- case MKTAG('G', 'O', 'S', 'T'): {
- int mode = 0;
- if (READ_LE_UINT16(payload) == 0)
- mode = 0x2000;
- else if (READ_LE_UINT16(payload) == 1)
- mode = 0x4000;
- else if (READ_LE_UINT16(payload) == 2)
- mode = 0x6000;
-
- int offsetX = (int16)READ_LE_UINT16(payload + 2);
- int offsetY = (int16)READ_LE_UINT16(payload + 4);
- anmFrameObject(x + offsetX, y + offsetY, flags | mode, lastObject, chunkSize);
- break;
+ _paletteState = 1;
}
- case MKTAG('I', 'A', 'C', 'T'):
- _client->chunkCallback(payload, chunkSize, flags);
- break;
- case MKTAG('N', 'P', 'A', 'L'):
- if (animatorPause(5) != 1)
- anmSetPalette(payload, 1);
-
- break;
- case MKTAG('L', 'O', 'A', 'D'):
- anmPreloadPut(payload, chunkSize);
- break;
- case MKTAG('S', 'A', 'U', 'D'):
- case MKTAG('P', 'S', 'A', 'D'):
- break;
- case MKTAG('S', 'T', 'O', 'R'):
- storePending = true;
- break;
- case MKTAG('S', 'K', 'I', 'P'): {
- int second = READ_LE_UINT16(payload + 2);
- int first = READ_LE_UINT16(payload);
- if (second)
- skipNext = anmTestUpdateFlag(first) != anmTestUpdateFlag(second);
- else
- skipNext = anmTestUpdateFlag(first) != 0;
- break;
+ break;
+ }
+ default: {
+ bool printable = true;
+ for (int i = 0; i < 4; i++) {
+ byte c = (tag >> (i * 8)) & 0xFF;
+ if (c < 0x30 || c > 0x5A)
+ printable = false;
}
- case MKTAG('V', 'M', 'S', 'K'):
- if (animatorPause(5) != 1)
- _noDisplay = 1;
- break;
- case MKTAG('X', 'P', 'A', 'L'): {
- int mode = READ_LE_UINT16(payload + 2);
- if (mode == 0 || mode == 512) {
- if (mode == 512)
- memcpy(_palette, payload + 1540, 768);
-
- for (int i = 0; i < 768; i++) {
- _paletteAccum[i] = _palette[i] << 7;
- _paletteDelta[i] = (int16)READ_LE_UINT16(payload + 4 + i * 2);
- }
- } else if (animatorPause(5) != 1) {
- for (int i = 0; i < 768; i++) {
- _paletteAccum[i] += _paletteDelta[i];
- _palette[i] = _paletteAccum[i] >> 7;
- }
-
- _paletteRefresh = 1;
- _paletteState = 1;
- }
-
- break;
+ if (!printable) {
+ warning("Bad IFF Header found %x at %x", tag, (uint32)(block - start));
+ return -1;
}
- case MKTAG('T', 'R', 'E', 'S'):
- case MKTAG('T', 'E', 'X', 'T'):
- if (flags & 0x200) {
- const byte pseudo[2] = { 9, 0 };
- _client->chunkCallback(pseudo, chunkSize, flags);
- } else if (tag == MKTAG('T', 'R', 'E', 'S')) {
- _client->setAnnotation((int16)READ_LE_UINT16(payload + 16));
- }
-
- break;
- default: {
- bool printable = true;
- for (int i = 0; i < 4; i++) {
- byte c = (tag >> (i * 8)) & 0xFF;
- if (c < 0x30 || c > 0x5A)
- printable = false;
- }
- if (!printable) {
- warning("Bad IFF Header found %x at %x", tag, (uint32)(block - start));
- return -1;
- }
-
- break;
- }
- }
+ break;
+ }
}
- previousTag = tag;
block = payload + chunkSize;
size -= 8 + chunkSize;
}
diff --git a/engines/grim/movie/smush_animator.h b/engines/grim/movie/smush_animator.h
index 203ece0e5fd..d6648c73a9c 100644
--- a/engines/grim/movie/smush_animator.h
+++ b/engines/grim/movie/smush_animator.h
@@ -57,7 +57,6 @@ public:
virtual int postFrame(int frame, int frameCount) = 0;
virtual int frameCallback(int flags, bool paletteChanged, bool display, int paletteState) = 0;
virtual int chunkCallback(const byte *chunk, int size, int flags) = 0;
- virtual void setAnnotation(int value) = 0;
virtual void setFrameRate(int fps) = 0;
virtual void setFrameBuffer(byte *buffer, int x, int y, int sourceX, int sourceY, int width, int height) = 0;
};
@@ -73,10 +72,8 @@ private:
int _rewindPos;
int _alignSkip;
Common::SeekableReadStream *_file;
- bool _fileIsMemory;
int _fileOffset;
int _fileSize;
- int _busy;
int _restart;
int _hasData;
int _released;
@@ -94,7 +91,6 @@ public:
int streamInitialize(int ringSize, int maxRead);
void streamTerminate();
- void streamInitMemory();
void streamReset();
void streamOpen(const Common::String &name, int offset);
bool streamFill(int limit);
@@ -119,47 +115,19 @@ private:
int _frameCount;
int _blockSize;
int _tag;
- int _originX;
- int _originY;
- int _newAnim;
- int _options;
- int _seeking;
- int _skipping;
int _quit;
- int _pause;
- int _dirty;
- int _skipUntil;
- int _skipBase;
- int _skipLength;
- int _flags;
-
- byte *_overlay;
- byte *_preload;
- int _overlaySize;
- int _preloadSize;
- int _preloadUsed;
- int _preloadCursor;
- int _preloadLast;
- int _preloadFirst;
- byte _updateFlags[128];
byte _palette[768];
uint16 _paletteAccum[768];
uint16 _paletteDelta[768];
int _paletteState;
- int _paletteRefresh;
int _noDisplay;
int _displayPending;
int animatorReadBlock(int size);
- int animatorCheckSkip();
- int animatorDecodeBlocks(int x, int y, int flags);
+ void animatorDecodeBlocks(int flags);
void animatorHandleHeader(const byte *header);
- int anmFrameObject(int x, int y, int flags, const byte *chunk, int size);
- void anmStoreObject(byte *destination, const byte *chunk);
- void anmDrawObject(int x, int y, const byte *object, int flags);
- void anmPreloadPut(const byte *chunk, int size);
- bool anmTestUpdateFlag(int index);
+ int anmFrameObject(int flags, const byte *chunk);
public:
SmushDemoAnim(SmushDemoStream *stream, SmushDemoClient *client);
@@ -169,16 +137,9 @@ public:
int anmTerminate();
void animatorInitialize();
int animatorRender(int flags);
- void animatorSetOrigin(int x, int y);
- void animatorNewAnim(const Common::String &name, int options, int skipLength, int skipUntil, int seeking, int offset);
- void animatorSetSkip(int skipLength, int skipUntil);
+ void animatorNewAnim(const Common::String &name, int offset);
void animatorResetView();
- int animatorPause(int mode);
- int anmBuffersInitialize(int overlaySize, int preloadSize);
- void anmBuffersTerminate();
- void anmClearUpdateFlags(int index);
- int anmDecodeBody(int x, int y, const byte *block, int size, int flags);
- const byte *anmPreloadGet(int size);
+ int anmDecodeBody(const byte *block, int size, int flags);
void anmDisplay(int flags);
void anmSetPalette(const byte *palette, int mode);
const byte *anmGetPalette() const { return _palette; }
Commit: 6f273f11c04f8c927b44e8b12a4034ea9886c310
https://github.com/scummvm/scummvm/commit/6f273f11c04f8c927b44e8b12a4034ea9886c310
Author: AndywinXp (andywinxp at gmail.com)
Date: 2026-11-01T22:18:07+01:00
Commit Message:
GRIM: SMUSH: Remove unused demo player code
Changed paths:
engines/grim/movie/smush_anim.cpp
engines/grim/movie/smush_anim.h
diff --git a/engines/grim/movie/smush_anim.cpp b/engines/grim/movie/smush_anim.cpp
index 1cdab9886cd..f0db719ac74 100644
--- a/engines/grim/movie/smush_anim.cpp
+++ b/engines/grim/movie/smush_anim.cpp
@@ -184,11 +184,8 @@ SmushDemoPlayer::SmushDemoPlayer(SmushDemoVideo *video, SmushDemoDriver *driver)
memset(&_audio, 0, sizeof(_audio));
_useDriver = true;
_playing = 0;
- _playFlags = 0;
memset(_staging, 0, sizeof(_staging));
memset(_palette, 0, sizeof(_palette));
- memset(_paletteSaved, 0, sizeof(_paletteSaved));
- memset(_paletteTarget, 0, sizeof(_paletteTarget));
_frameSource = nullptr;
_frameWidth = 0;
_frameHeight = 0;
@@ -198,7 +195,6 @@ SmushDemoPlayer::SmushDemoPlayer(SmushDemoVideo *video, SmushDemoDriver *driver)
_sourceY = 0;
_paused = 0;
_done = 0;
- _flags = 0;
_lastResult = 0;
_frameReady = 0;
_paletteFlag = 0;
@@ -215,9 +211,6 @@ SmushDemoPlayer::SmushDemoPlayer(SmushDemoVideo *video, SmushDemoDriver *driver)
_frameCount = 1;
_dropCount = 0;
_behind = 0;
- _fadeStart = 0;
- _fadeSteps = 1;
- _fadeRemaining = 0;
_firstUpdate = 1;
_streamStop = 1;
}
@@ -255,25 +248,12 @@ int SmushDemoPlayer::chunkCallback(const byte *chunk, int size, int flags) {
if (READ_LE_UINT16(chunk) != 8)
return 0;
- int part = (int16)READ_LE_UINT16(chunk + 6);
const byte *data = chunk + 18;
int remaining = size - 18;
- if (part) {
- if (part <= 0 || part > 5)
- return 0;
-
- memcpy(_paletteSaved, _palette, sizeof(_palette));
- for (int i = 0; i < 256; i++) {
- _paletteTarget[i] = data[0] | (READ_LE_UINT16(data + 1) << 8);
- data += 3;
- }
-
- _fadeSteps = 5 * part;
- _fadeRemaining = 5 * part;
- _fadeStart = _framesShown;
+ // The disasm fades the palette with the other parts, which no movie has
+ if (READ_LE_UINT16(chunk + 6))
return 0;
- }
_audioStartFrame = _frame;
if (!remaining)
@@ -384,27 +364,6 @@ void SmushDemoPlayer::decodeIACT(const byte *block) {
_audio.produced += 4096;
}
-void SmushDemoPlayer::fadePalette() {
- if (!_fadeRemaining)
- return;
-
- int steps = _fadeSteps;
- int remaining = _fadeStart + steps - _frame;
- int elapsed = _frame - _fadeStart;
-
- for (int i = 0; i < 256; i++) {
- uint32 from = _paletteSaved[i];
- uint32 to = _paletteTarget[i];
- uint32 red = (remaining * (from & 0xFF) + elapsed * (to & 0xFF)) / steps;
- uint32 green = (elapsed * ((to >> 8) & 0xFF) + remaining * ((from >> 8) & 0xFF)) / steps;
- uint32 blue = (elapsed * ((to >> 16) & 0xFF) + remaining * ((from >> 16) & 0xFF)) / steps;
- _palette[i] = (red & 0xFF) | ((green & 0xFF) << 8) | ((blue & 0xFF) << 16);
- }
-
- _fadeRemaining--;
- _paletteFlag = 1;
-}
-
int SmushDemoPlayer::audioOpen(int rate, int channels, int bits) {
if (_useDriver)
return _driver->sndDrivOpen(rate, channels, bits, &_audio);
@@ -546,7 +505,7 @@ void SmushDemoPlayer::terminate() {
_stream->streamTerminate();
}
-int SmushDemoPlayer::open(const Common::String &name, int flags, int offset) {
+int SmushDemoPlayer::open(const Common::String &name, int offset) {
if (!Common::File::exists(Common::Path(name)))
return 1;
@@ -563,7 +522,6 @@ int SmushDemoPlayer::open(const Common::String &name, int flags, int offset) {
memset(_palette, 0, sizeof(_palette));
_paused = 0;
_done = 0;
- _flags = flags;
_lastResult = 0;
_frameReady = 0;
_paletteFlag = 0;
@@ -583,9 +541,6 @@ int SmushDemoPlayer::open(const Common::String &name, int flags, int offset) {
_dropCount = 0;
_behind = 0;
_firstUpdate = 1;
- _fadeStart = 0;
- _fadeRemaining = 0;
- _fadeSteps = 1;
_streamStop = 0;
return 0;
}
@@ -632,7 +587,6 @@ int SmushDemoPlayer::update(int *frameDecoded, int *finished, int *paletteChange
}
if (due > 0) {
- fadePalette();
if (_frameReady)
*frameDecoded = 1;
@@ -692,13 +646,12 @@ int SmushDemoPlayer::smushPlayStartMovie(const Common::String &name, int flags,
return 1;
}
- if (open(name, flags & 0xFFFFFFF, offset)) {
+ if (open(name, offset)) {
terminate();
_video->videoStop();
return 1;
}
- _playFlags = flags & 0xF0000000;
_playing = 1;
return 0;
}
@@ -754,23 +707,13 @@ int SmushDemoPlayer::smushPlayBlit(SmushDemoTarget *target, const SmushDemoRect
_framesShown = _frame;
if (target && _frameSource) {
- if (!(_flags & 8) && due > 1 && _dropCount < 3) {
+ if (due > 1 && _dropCount < 3) {
_dropCount++;
} else {
_dropCount = 0;
- if (_flags & 1) {
- _flags &= ~6;
- if (_frameWidth <= rect->width / 2)
- _flags |= 4;
- else if (_frameHeight < rect->height)
- _flags |= 2;
- }
- int verticalScale = ((_flags & 6) != 0) + 1;
- int doubleBoth = _flags & 4;
- int horizontalScale = (doubleBoth != 0) + 1;
- int width = _frameWidth * horizontalScale;
- int height = _frameHeight * verticalScale;
+ int width = _frameWidth;
+ int height = _frameHeight;
int x = _sourceX;
int y = _sourceY;
@@ -790,20 +733,13 @@ int SmushDemoPlayer::smushPlayBlit(SmushDemoTarget *target, const SmushDemoRect
if (y + height > rect->height + rect->y)
height = rect->height + rect->y - y;
- int copyWidth = width / horizontalScale;
- int copyHeight = height / verticalScale;
- int alignedX = (x & ~7) * horizontalScale;
- int alignedWidth = ((copyWidth + x + 7) & ~7) - (x & ~7);
+ int alignedX = x & ~7;
+ int alignedWidth = ((width + x + 7) & ~7) - alignedX;
- const byte *source = _frameSource + alignedX + y * _frameWidth * verticalScale;
- byte *destination = target->pixels + alignedX + y * target->width * verticalScale;
+ const byte *source = _frameSource + alignedX + y * _frameWidth;
+ byte *destination = target->pixels + alignedX + y * target->width;
- if (doubleBoth)
- smushPlayBlitDouble(source, _frameWidth, destination, target->width, alignedWidth, copyHeight);
- else if (_flags & 2)
- smushPlayBlitDoubleRows(source, _frameWidth, destination, target->width, alignedWidth, copyHeight);
- else
- smushPlayBlitCopy(source, _frameWidth, destination, target->width, alignedWidth, copyHeight);
+ smushPlayBlitCopy(source, _frameWidth, destination, target->width, alignedWidth, height);
}
}
@@ -818,46 +754,6 @@ int SmushDemoPlayer::smushPlayBlit(SmushDemoTarget *target, const SmushDemoRect
return 0;
}
-void SmushDemoPlayer::smushPlayBlitDouble(const byte *source, int sourcePitch, byte *destination, int destinationPitch, int width, int rows) {
- for (int row = 0; row < rows; row++) {
- const byte *in = source;
- byte *out = destination;
-
- for (int i = 0; i < (width >> 2); i++) {
- byte p0 = in[0];
- byte p1 = in[1];
- byte p2 = in[2];
- byte p3 = in[3];
-
- out[0] = p0;
- out[1] = p0;
- out[2] = p1;
- out[3] = p1;
- out[4] = p2;
- out[5] = p2;
- out[6] = p3;
- out[7] = p3;
- memcpy(out + destinationPitch, out, 8);
-
- in += 4;
- out += 8;
- }
-
- source += sourcePitch;
- destination += destinationPitch * 2;
- }
-}
-
-void SmushDemoPlayer::smushPlayBlitDoubleRows(const byte *source, int sourcePitch, byte *destination, int destinationPitch, int width, int rows) {
- for (int row = 0; row < rows; row++) {
- memcpy(destination, source, (width >> 2) * 4);
- memcpy(destination + destinationPitch, source, (width >> 2) * 4);
-
- source += sourcePitch;
- destination += destinationPitch * 2;
- }
-}
-
void SmushDemoPlayer::smushPlayBlitCopy(const byte *source, int sourcePitch, byte *destination, int destinationPitch, int width, int rows) {
for (int row = 0; row < rows; row++) {
memcpy(destination, source, (width >> 2) * 4);
diff --git a/engines/grim/movie/smush_anim.h b/engines/grim/movie/smush_anim.h
index 028ac98a671..3c50d9af529 100644
--- a/engines/grim/movie/smush_anim.h
+++ b/engines/grim/movie/smush_anim.h
@@ -62,12 +62,9 @@ private:
bool _useDriver;
int _playing;
- int _playFlags;
byte _staging[4096 + 64];
uint32 _palette[256];
- uint32 _paletteSaved[256];
- uint32 _paletteTarget[256];
byte *_frameSource;
int _frameWidth;
int _frameHeight;
@@ -77,7 +74,6 @@ private:
int _sourceY;
int _paused;
int _done;
- int _flags;
int _lastResult;
int _frameReady;
int _paletteFlag;
@@ -94,9 +90,6 @@ private:
int _frameCount;
int _dropCount;
int _behind;
- int _fadeStart;
- int _fadeSteps;
- int _fadeRemaining;
int _firstUpdate;
int _streamStop;
@@ -108,10 +101,7 @@ private:
int audioGetTime();
int getFramesDue();
int stepAnimation();
- void fadePalette();
void decodeIACT(const byte *block);
- void smushPlayBlitDouble(const byte *source, int sourcePitch, byte *destination, int destinationPitch, int width, int rows);
- void smushPlayBlitDoubleRows(const byte *source, int sourcePitch, byte *destination, int destinationPitch, int width, int rows);
void smushPlayBlitCopy(const byte *source, int sourcePitch, byte *destination, int destinationPitch, int width, int rows);
public:
@@ -127,7 +117,7 @@ public:
int initialize(int bufferBytes, int width, int height);
void terminate();
- int open(const Common::String &name, int flags, int offset);
+ int open(const Common::String &name, int offset);
int close();
int update(int *frameDecoded, int *finished, int *paletteChanged, const uint32 **palette);
void pause();
Commit: f3bc5a22c467bd6324e65fd10ff1586b23b59293
https://github.com/scummvm/scummvm/commit/f3bc5a22c467bd6324e65fd10ff1586b23b59293
Author: AndywinXp (andywinxp at gmail.com)
Date: 2026-11-01T22:18:07+01:00
Commit Message:
GRIM: SMUSH: Remove unused codec 48 code
Changed paths:
engines/grim/movie/codecs/codec48.cpp
engines/grim/movie/codecs/codec48.h
engines/grim/movie/smush_animator.cpp
diff --git a/engines/grim/movie/codecs/codec48.cpp b/engines/grim/movie/codecs/codec48.cpp
index 81de683b1d3..03b901b6424 100644
--- a/engines/grim/movie/codecs/codec48.cpp
+++ b/engines/grim/movie/codecs/codec48.cpp
@@ -73,7 +73,7 @@ Codec48Decoder::~Codec48Decoder() {
delete[] _interTable;
}
-int Codec48Decoder::decode(const SmushDemoBitmap *bitmap, const SmushDemoRect *rect, int x, int y, const byte *chunk, int flags, SmushDemoClient *client) {
+int Codec48Decoder::decode(const SmushDemoRect *rect, int x, int y, const byte *chunk, SmushDemoClient *client) {
if (!_initialized)
return -1;
@@ -129,28 +129,15 @@ int Codec48Decoder::decode(const SmushDemoBitmap *bitmap, const SmushDemoRect *r
// Raw frame
memcpy(_deltaBuf[_curBuf], gfxData, READ_LE_UINT32(header + 4));
break;
- case 2:
- // Blast object
- bompDecodeLine(_deltaBuf[_curBuf], gfxData, READ_LE_UINT32(header + 4));
- break;
case 3:
// 8x8 block encoding
if (!(seqNb && seqNb != _prevSeqNb + 1)) {
- if (seqNb & 1 || !(header[12] & 1) || header[12] & 0x10)
+ if (seqNb & 1 || !(header[12] & 1))
_curBuf ^= 1;
- if ((seqNb & 1) && (flags & 0x10) && (header[12] & 1) && !(header[12] & 0x30)) {
- _prevSeqNb = seqNb;
- return 1;
- }
-
decode3(_deltaBuf[_curBuf], gfxData, _deltaBuf[_curBuf ^ 1] - _deltaBuf[_curBuf]);
}
break;
- case 5:
- // Frame stored at half size, scaled up with the interpolation table
- decode5(_deltaBuf[_curBuf], gfxData, _blockX * 8, _blockY * 8);
- break;
default:
break;
}
@@ -178,90 +165,10 @@ int Codec48Decoder::decode(const SmushDemoBitmap *bitmap, const SmushDemoRect *r
if (y + height > rect->y + rect->height)
height = rect->y + rect->height - y;
- if (header[12] & 2)
- return 0;
-
- if (header[12] & 0x10) {
- drawBlended(bitmap->pixels, bitmap->width, _deltaBuf[_curBuf ^ 1], _deltaBuf[_curBuf], x, y, sourceX, sourceY, width, height, _pitch);
- return 0;
- }
-
client->setFrameBuffer(_deltaBuf[_curBuf], x, y, sourceX, sourceY, width, height);
return 0;
}
-void Codec48Decoder::drawBlended(byte *bitmap, int bitmapPitch, const byte *previous, const byte *current, int x, int y, int sourceX, int sourceY, int width, int height, int sourcePitch) {
- int offset = sourceY * sourcePitch + sourceX;
- blendFrames(bitmap + y * bitmapPitch + x, previous + offset, current + offset, width, height, bitmapPitch, sourcePitch);
-}
-
-void Codec48Decoder::blendFrames(byte *dst, const byte *previous, const byte *current, int width, int height, int dstPitch, int sourcePitch) {
- for (int row = 0; row < height; row++) {
- for (int col = 0; col < width; col++)
- dst[col] = _interTable[previous[col] | (current[col] << 8)];
-
- dst += dstPitch;
- previous += sourcePitch;
- current += sourcePitch;
- }
-}
-
-void Codec48Decoder::decode5(byte *dst, const byte *src, int width, int height) {
- byte *out = dst + width;
- int rows = height;
-
- do {
- byte pixel = *src++;
- out[0] = pixel;
- out[1] = pixel;
- out += 2;
-
- int count = width - 2;
- do {
- byte previous = pixel;
- pixel = *src++;
- out[0] = _interTable[(previous << 8) | pixel];
- out[1] = pixel;
- out += 2;
- count -= 2;
- } while (count > 0);
-
- out += width;
- rows -= 2;
- } while (rows > 0);
-
- memcpy(dst, dst + width, width);
-
- out = dst + width * 2;
- rows = height - 2;
- do {
- for (int i = 0; i < width; i++)
- out[i] = _interTable[out[i - width] | (out[i + width] << 8)];
-
- out += width * 2;
- rows -= 2;
- } while (rows > 0);
-}
-
-void Codec48Decoder::bompDecodeLine(byte *dst, const byte *src, int len) {
- do {
- byte code = *src++;
- int num = (code >> 1) + 1;
-
- len -= num;
-
- if (code & 1) {
- byte color = *src++;
- memset(dst, color, num);
- } else {
- memcpy(dst, src, num);
- src += num;
- }
-
- dst += num;
- } while (len != 0);
-}
-
void Codec48Decoder::makeTable(int pitch, int index) {
// codec48's table is codec47's table appended by the first
// part of codec37's table
diff --git a/engines/grim/movie/codecs/codec48.h b/engines/grim/movie/codecs/codec48.h
index 42275e1b541..44052ed482b 100644
--- a/engines/grim/movie/codecs/codec48.h
+++ b/engines/grim/movie/codecs/codec48.h
@@ -26,7 +26,6 @@
namespace Grim {
-struct SmushDemoBitmap;
struct SmushDemoRect;
class SmushDemoClient;
@@ -36,17 +35,12 @@ public:
~Codec48Decoder();
int init(int width, int height);
void deinit();
- int decode(const SmushDemoBitmap *bitmap, const SmushDemoRect *rect, int x, int y, const byte *chunk, int flags, SmushDemoClient *client);
+ int decode(const SmushDemoRect *rect, int x, int y, const byte *chunk, SmushDemoClient *client);
private:
void makeTable(int pitch, int index);
- void drawBlended(byte *bitmap, int bitmapPitch, const byte *previous, const byte *current, int x, int y, int sourceX, int sourceY, int width, int height, int sourcePitch);
- void blendFrames(byte *dst, const byte *previous, const byte *current, int width, int height, int dstPitch, int sourcePitch);
-
- void bompDecodeLine(byte *dst, const byte *src, int len);
void decode3(byte *dst, const byte *src, int bufOffset);
- void decode5(byte *dst, const byte *src, int width, int height);
void scaleBlock(byte *dst, const byte *src);
void copyBlock(byte *dst, int deltaBufOffset, int offset);
diff --git a/engines/grim/movie/smush_animator.cpp b/engines/grim/movie/smush_animator.cpp
index 589dff59b01..9d3ddc40d18 100644
--- a/engines/grim/movie/smush_animator.cpp
+++ b/engines/grim/movie/smush_animator.cpp
@@ -598,7 +598,7 @@ int SmushDemoAnim::anmFrameObject(int flags, const byte *chunk) {
int height = (int16)READ_LE_UINT16(chunk + 8);
if (chunk[0] == 48 && _codec48)
- return _codec48->decode(_bitmap, &_rect, drawX, drawY, chunk, flags, _client);
+ return _codec48->decode(&_rect, drawX, drawY, chunk, _client);
if (chunk[0] != 47 || !_blocky8)
return -1;
Commit: 834f76ff35c3523285450a8eb263243ca5a8b562
https://github.com/scummvm/scummvm/commit/834f76ff35c3523285450a8eb263243ca5a8b562
Author: AndywinXp (andywinxp at gmail.com)
Date: 2026-11-01T22:18:07+01:00
Commit Message:
GRIM: SMUSH: Address PR comments and remove most magic numbers
Changed paths:
engines/grim/movie/smush_anim.cpp
engines/grim/movie/smush_animator.cpp
engines/grim/movie/smush_mixer.cpp
engines/grim/movie/smush_mixer.h
engines/grim/movie/smush_sound.cpp
engines/grim/movie/smush_timer.h
diff --git a/engines/grim/movie/smush_anim.cpp b/engines/grim/movie/smush_anim.cpp
index f0db719ac74..2ad0f20aa10 100644
--- a/engines/grim/movie/smush_anim.cpp
+++ b/engines/grim/movie/smush_anim.cpp
@@ -31,6 +31,15 @@
namespace Grim {
+// The audio ring holds 120 blocks, about 5.5 seconds
+#define SMUSH_DEMO_AUDIO_BLOCKS 120
+
+// The file ring is read in CD sectors (2048 bytes): 12 per call, 64 as the
+// first read; the biggest read is a frame plus 200 KB
+#define SMUSH_DEMO_STREAM_CHUNK 24576
+#define SMUSH_DEMO_STREAM_PREFETCH 0x20000
+#define SMUSH_DEMO_STREAM_SLACK 204800
+
SmushAnimPlay::SmushAnimPlay(SmushTimer *timer, SmushSound *sound) {
_timer = timer;
_sound = sound;
@@ -59,8 +68,8 @@ int SmushAnimPlay::anmStart(int fps, int flags) {
_skipFlag = 0;
_started = 0;
_state = 0;
- _nominalDelay = 1193180 / fps;
- _currentDelay = 1193180 / fps;
+ _nominalDelay = SMUSH_TIMER_FREQUENCY / fps;
+ _currentDelay = SMUSH_TIMER_FREQUENCY / fps;
_frameTimer = _timer->sysAlloc(frameTimer, 1, SYS_TIMER_ONE_SHOT, this);
if (!_frameTimer)
@@ -104,6 +113,7 @@ int SmushAnimPlay::anmRenderBody(SmushAnimClient *client) {
if (!_started)
client->resetFrameCounter();
+ // Frames are 66.67 ms long (15 fps); the result is in frames
int drift = (int)(int64)(((double)_sound->souEngineQueryTime(0) - (double)client->getFrameCounter() * 66.66666666667) * 0.0149999999999925);
_lateTicks--;
_framesDue = 0;
@@ -335,7 +345,7 @@ void SmushDemoPlayer::decodeIACT(const byte *block) {
byte *destination = _audio.data + _audio.produced % _audio.size;
const byte *cursor = block + 3;
- for (int i = 0; i < 1024; i++) {
+ for (int i = 0; i < SMUSH_DEMO_AUDIO_BLOCK_SIZE / 4; i++) {
int16 sample;
if (*cursor == 128) {
@@ -361,7 +371,7 @@ void SmushDemoPlayer::decodeIACT(const byte *block) {
destination += 2;
}
- _audio.produced += 4096;
+ _audio.produced += SMUSH_DEMO_AUDIO_BLOCK_SIZE;
}
int SmushDemoPlayer::audioOpen(int rate, int channels, int bits) {
@@ -413,14 +423,14 @@ void SmushDemoPlayer::audioFill() {
if (_frame - _audioStartFrame > 5) {
int produced = _audio.produced;
- if (produced * _fps / 88200 >= _frameCount - 1) {
+ if (produced * _fps / SMUSH_DEMO_AUDIO_BYTES_PER_SECOND >= _frameCount - 1) {
int space = _audio.size + _audio.consumed - produced;
- if (space > 4096) {
- int blocks = (space - 1) >> 12;
+ if (space > SMUSH_DEMO_AUDIO_BLOCK_SIZE) {
+ int blocks = (space - 1) / SMUSH_DEMO_AUDIO_BLOCK_SIZE;
do {
- memset(_audio.data + _audio.produced % _audio.size, 0, 4096);
+ memset(_audio.data + _audio.produced % _audio.size, 0, SMUSH_DEMO_AUDIO_BLOCK_SIZE);
blocks--;
- _audio.produced += 4096;
+ _audio.produced += SMUSH_DEMO_AUDIO_BLOCK_SIZE;
} while (blocks);
}
}
@@ -460,7 +470,7 @@ int SmushDemoPlayer::stepAnimation() {
if ((int16)result != 11)
break;
- if (!_stream->streamFill(24576))
+ if (!_stream->streamFill(SMUSH_DEMO_STREAM_CHUNK))
break;
}
@@ -475,14 +485,15 @@ bool SmushDemoPlayer::streamStep() {
return false;
bool read = false;
- while (_stream->streamFill(24576))
+ while (_stream->streamFill(SMUSH_DEMO_STREAM_CHUNK))
read = true;
return read;
}
int SmushDemoPlayer::initialize(int bufferBytes, int width, int height) {
- if (_stream->streamInitialize(bufferBytes - (width * height + 204800), width * height + 204800))
+ int maxRead = width * height + SMUSH_DEMO_STREAM_SLACK;
+ if (_stream->streamInitialize(bufferBytes - maxRead, maxRead))
return 0;
if (_anim->anmInitialize(width, height)) {
@@ -510,14 +521,14 @@ int SmushDemoPlayer::open(const Common::String &name, int offset) {
return 1;
memset(&_audio, 0, sizeof(_audio));
- _audio.size = 491520;
+ _audio.size = SMUSH_DEMO_AUDIO_BLOCKS * SMUSH_DEMO_AUDIO_BLOCK_SIZE;
- if (audioOpen(22050, 2, 16))
+ if (audioOpen(SMUSH_DEMO_AUDIO_RATE, 2, 16))
return 1;
_anim->animatorInitialize();
_anim->animatorNewAnim(name, offset);
- _stream->streamFill(0x20000);
+ _stream->streamFill(SMUSH_DEMO_STREAM_PREFETCH);
memset(_palette, 0, sizeof(_palette));
_paused = 0;
diff --git a/engines/grim/movie/smush_animator.cpp b/engines/grim/movie/smush_animator.cpp
index 9d3ddc40d18..2469a894c8b 100644
--- a/engines/grim/movie/smush_animator.cpp
+++ b/engines/grim/movie/smush_animator.cpp
@@ -564,12 +564,12 @@ int SmushDemoAnim::animatorRender(int flags) {
return 1;
default:
- warning("Read invalid frame header -- CD may be dirty or damaged.");
+ warning("Read invalid frame header");
return -3;
}
if (_tag > 0x77) {
- warning("Read invalid frame header -- CD may be dirty or damaged.");
+ warning("Read invalid frame header");
return -3;
}
}
diff --git a/engines/grim/movie/smush_mixer.cpp b/engines/grim/movie/smush_mixer.cpp
index 412ac648c33..4cc6b92a2bb 100644
--- a/engines/grim/movie/smush_mixer.cpp
+++ b/engines/grim/movie/smush_mixer.cpp
@@ -336,7 +336,7 @@ int SmushDriver::sndDrivInitialize(int rate, int channelCount, SmushSound *sound
return -1;
_sound = sound;
- _fillTimer = _timer->sysAlloc(sndDrivFillThreadCallback, 1193180 / (rate / 1024), SYS_TIMER_ONE_SHOT, this);
+ _fillTimer = _timer->sysAlloc(sndDrivFillThreadCallback, SMUSH_TIMER_FREQUENCY / (rate / 1024), SYS_TIMER_ONE_SHOT, this);
return 0;
}
@@ -376,7 +376,7 @@ void SmushDriver::sndDrivFillThread() {
}
}
- _timer->sysReset(_fillTimer, 1193180 / (_driverRate / 1024));
+ _timer->sysReset(_fillTimer, SMUSH_TIMER_FREQUENCY / (_driverRate / 1024));
}
void SmushDriver::sndDrivMix(const uint8 *srcBuf, int32 mixBufStartIndex, uint32 feedSize, int32 srcByteCount, int volume, int pan, int format) {
@@ -514,7 +514,7 @@ int SmushDemoDriver::sndDrivOpen(int rate, int channels, int bits, SmushDemoAudi
audio->data = new byte[audio->size]();
_blockAlign = (bits >> 3) * channels;
- _bufferSize = 5000 * _rate * _blockAlign / 1000;
+ _bufferSize = SMUSH_DEMO_DRIVER_BUFFER_MS * _rate * _blockAlign / 1000;
{
Common::StackLock lock(_mutex);
@@ -535,7 +535,7 @@ int SmushDemoDriver::sndDrivOpen(int rate, int channels, int bits, SmushDemoAudi
_waiting = 1;
_paused = 0;
_msPerByte = 1000.0 / (double)(_rate * _blockAlign);
- _threshold = 1000 * _rate * _blockAlign / 1000;
+ _threshold = SMUSH_DEMO_DRIVER_START_MS * _rate * _blockAlign / 1000;
return 0;
}
@@ -580,11 +580,11 @@ int SmushDemoDriver::sndDrivGetTime(SmushDemoAudio *audio) {
_lastPosition = position;
if (!_waiting) {
- if (_written < played + 500 * _rate * _blockAlign / 1000) {
+ if (_written < played + SMUSH_DEMO_DRIVER_LOW_MS * _rate * _blockAlign / 1000) {
Common::StackLock lock(_mutex);
_playing = false;
_waiting = 1;
- _threshold = played + 500 * _rate * _blockAlign / 1000;
+ _threshold = played + SMUSH_DEMO_DRIVER_LOW_MS * _rate * _blockAlign / 1000;
}
}
}
@@ -626,8 +626,10 @@ void SmushDemoDriver::sndDrivFill(SmushDemoAudio *audio) {
int ringSize = _bufferSize;
int playedAbsolute = _lastPosition + ringSize * _wrapCount;
+ // The disasm never reads the last 1024 bytes of the produced audio
+ // and keeps a block of the ring free
int available = audio->produced - audio->consumed - 1024;
- int space = playedAbsolute - _written + ringSize - 4096;
+ int space = playedAbsolute - _written + ringSize - SMUSH_DEMO_AUDIO_BLOCK_SIZE;
if (space >= available)
space = available;
diff --git a/engines/grim/movie/smush_mixer.h b/engines/grim/movie/smush_mixer.h
index 299d82c8db1..da6920be096 100644
--- a/engines/grim/movie/smush_mixer.h
+++ b/engines/grim/movie/smush_mixer.h
@@ -31,6 +31,17 @@ namespace Grim {
#define SMUSH_RING_BLOCK_SIZE 6144
+// The demo decodes every IACT chunk into 1024 stereo 16 bit samples
+#define SMUSH_DEMO_AUDIO_BLOCK_SIZE 4096
+#define SMUSH_DEMO_AUDIO_RATE 22050
+#define SMUSH_DEMO_AUDIO_BYTES_PER_SECOND (SMUSH_DEMO_AUDIO_RATE * 2 * 2)
+
+// Milliseconds of audio in the demo driver ring, to buffer before playing,
+// and under which the driver stops to buffer again
+#define SMUSH_DEMO_DRIVER_BUFFER_MS 5000
+#define SMUSH_DEMO_DRIVER_START_MS 1000
+#define SMUSH_DEMO_DRIVER_LOW_MS 500
+
class SmushTimer;
class SmushSound;
diff --git a/engines/grim/movie/smush_sound.cpp b/engines/grim/movie/smush_sound.cpp
index a3fdd0447e0..50d1e9ea8e3 100644
--- a/engines/grim/movie/smush_sound.cpp
+++ b/engines/grim/movie/smush_sound.cpp
@@ -233,7 +233,7 @@ int SmushSound::souEngineExternalSync(double ticks) {
int now = _timer->sysGetTime();
if (now > _syncMark) {
int elapsed = now - _syncMark;
- if (elapsed / 5 < 1193180) {
+ if (elapsed / 5 < SMUSH_TIMER_FREQUENCY) {
_syncMark = 0;
_syncAccumulator -= (double)elapsed;
}
@@ -372,7 +372,7 @@ int SmushSound::souEngineStartStream(const byte *data, int size, uint32 volume,
void SmushSound::souEngineFill(int frameCount, SmushDriver *driver) {
_fillCounter++;
- souEngineExternalSync((double)frameCount * 1193180.0 / (double)_sampleRate);
+ souEngineExternalSync((double)frameCount * SMUSH_TIMER_FREQUENCY / (double)_sampleRate);
if (_pauseCount)
return;
diff --git a/engines/grim/movie/smush_timer.h b/engines/grim/movie/smush_timer.h
index 9553c9dc401..868f72f3fe9 100644
--- a/engines/grim/movie/smush_timer.h
+++ b/engines/grim/movie/smush_timer.h
@@ -28,6 +28,9 @@ namespace Grim {
#define SYS_MAX_TIMERS 19
+// Ticks per second of the system timer (the 8253 clock frequency)
+#define SMUSH_TIMER_FREQUENCY 1193180
+
#define SYS_TIMER_ONE_SHOT 2
#define SYS_TIMER_EXT_SYNC 8
Commit: 440d459872f7ebea000c5ae14c5671c1aa6d093b
https://github.com/scummvm/scummvm/commit/440d459872f7ebea000c5ae14c5671c1aa6d093b
Author: AndywinXp (andywinxp at gmail.com)
Date: 2026-11-01T22:18:07+01:00
Commit Message:
GRIM: SMUSH: Improve some magic numbers labeling
Changed paths:
engines/grim/movie/smush_anim.cpp
engines/grim/movie/smush_animator.cpp
engines/grim/movie/smush_animator.h
diff --git a/engines/grim/movie/smush_anim.cpp b/engines/grim/movie/smush_anim.cpp
index 2ad0f20aa10..4235342cbdb 100644
--- a/engines/grim/movie/smush_anim.cpp
+++ b/engines/grim/movie/smush_anim.cpp
@@ -34,11 +34,11 @@ namespace Grim {
// The audio ring holds 120 blocks, about 5.5 seconds
#define SMUSH_DEMO_AUDIO_BLOCKS 120
-// The file ring is read in CD sectors (2048 bytes): 12 per call, 64 as the
-// first read; the biggest read is a frame plus 200 KB
-#define SMUSH_DEMO_STREAM_CHUNK 24576
-#define SMUSH_DEMO_STREAM_PREFETCH 0x20000
-#define SMUSH_DEMO_STREAM_SLACK 204800
+// The file ring is read 12 sectors per call and 64 as the first read;
+// the biggest read is a frame plus 100 sectors
+#define SMUSH_DEMO_STREAM_CHUNK (12 * SMUSH_DEMO_SECTOR_SIZE)
+#define SMUSH_DEMO_STREAM_PREFETCH (64 * SMUSH_DEMO_SECTOR_SIZE)
+#define SMUSH_DEMO_STREAM_SLACK (100 * SMUSH_DEMO_SECTOR_SIZE)
SmushAnimPlay::SmushAnimPlay(SmushTimer *timer, SmushSound *sound) {
_timer = timer;
diff --git a/engines/grim/movie/smush_animator.cpp b/engines/grim/movie/smush_animator.cpp
index 2469a894c8b..e6d172bd42e 100644
--- a/engines/grim/movie/smush_animator.cpp
+++ b/engines/grim/movie/smush_animator.cpp
@@ -199,7 +199,7 @@ int SmushDemoStream::streamInitialize(int ringSize, int maxRead) {
_ring = new byte[maxRead + ringSize]();
_maxRead = maxRead;
- _ringSize = ringSize & 0xFFFFF800;
+ _ringSize = ringSize & ~(SMUSH_DEMO_SECTOR_SIZE - 1);
_file = nullptr;
memset(_ring, 0, _ringSize);
return 0;
@@ -252,9 +252,9 @@ int SmushDemoStream::streamDoOpen() {
_file = file;
_fileSize = _file->size();
- _file->seek(_openOffset & 0xFFFFF800);
- _alignSkip = _openOffset & 0x7FF;
- _fileOffset = _openOffset & 0xFFFFF800;
+ _file->seek(_openOffset & ~(SMUSH_DEMO_SECTOR_SIZE - 1));
+ _alignSkip = _openOffset & (SMUSH_DEMO_SECTOR_SIZE - 1);
+ _fileOffset = _openOffset & ~(SMUSH_DEMO_SECTOR_SIZE - 1);
_rewindPos = _writePos;
_restart = 1;
_hasData = 0;
@@ -283,23 +283,23 @@ bool SmushDemoStream::streamFill(int limit) {
if (_readPos > _writePos) {
if (_readPos != 0) {
- size = (_readPos - _writePos - 2048) & 0xFFFFF800;
- if (size < 2048)
+ size = (_readPos - _writePos - SMUSH_DEMO_SECTOR_SIZE) & ~(SMUSH_DEMO_SECTOR_SIZE - 1);
+ if (size < SMUSH_DEMO_SECTOR_SIZE)
return false;
- } else if (_ringSize - _writePos > 2048) {
- size = _ringSize - _writePos - 2048;
+ } else if (_ringSize - _writePos > SMUSH_DEMO_SECTOR_SIZE) {
+ size = _ringSize - _writePos - SMUSH_DEMO_SECTOR_SIZE;
} else {
- size = (_readPos - _writePos - 2048) & 0xFFFFF800;
- if (size < 2048)
+ size = (_readPos - _writePos - SMUSH_DEMO_SECTOR_SIZE) & ~(SMUSH_DEMO_SECTOR_SIZE - 1);
+ if (size < SMUSH_DEMO_SECTOR_SIZE)
return false;
}
} else if (_readPos != 0) {
size = _ringSize - _writePos;
- } else if (_ringSize - _writePos > 2048) {
- size = _ringSize - _writePos - 2048;
+ } else if (_ringSize - _writePos > SMUSH_DEMO_SECTOR_SIZE) {
+ size = _ringSize - _writePos - SMUSH_DEMO_SECTOR_SIZE;
} else {
- size = (_readPos - _writePos - 2048) & 0xFFFFF800;
- if (size < 2048)
+ size = (_readPos - _writePos - SMUSH_DEMO_SECTOR_SIZE) & ~(SMUSH_DEMO_SECTOR_SIZE - 1);
+ if (size < SMUSH_DEMO_SECTOR_SIZE)
return false;
}
diff --git a/engines/grim/movie/smush_animator.h b/engines/grim/movie/smush_animator.h
index d6648c73a9c..e1bde954eda 100644
--- a/engines/grim/movie/smush_animator.h
+++ b/engines/grim/movie/smush_animator.h
@@ -31,6 +31,9 @@ class SeekableReadStream;
namespace Grim {
+// The demo reads its files in CD sectors
+#define SMUSH_DEMO_SECTOR_SIZE 2048
+
class SmushDecoder;
class Blocky8;
class Codec48Decoder;
Commit: dca72722f39185b316447317701cea8d2a10bf22
https://github.com/scummvm/scummvm/commit/dca72722f39185b316447317701cea8d2a10bf22
Author: AndywinXp (andywinxp at gmail.com)
Date: 2026-11-01T22:18:07+01:00
Commit Message:
GRIM: SMUSH: Remove unused animator flags
Changed paths:
engines/grim/movie/smush.cpp
engines/grim/movie/smush_anim.cpp
engines/grim/movie/smush_anim.h
engines/grim/movie/smush_animator.cpp
engines/grim/movie/smush_animator.h
diff --git a/engines/grim/movie/smush.cpp b/engines/grim/movie/smush.cpp
index ab0ea6fdf91..ba8ed7d7853 100644
--- a/engines/grim/movie/smush.cpp
+++ b/engines/grim/movie/smush.cpp
@@ -178,8 +178,8 @@ void SmushPlayer::init() {
if (_smushAnim && !_currentVideoIsTheora) {
_smushAnim->anmStop();
- _smushAnim->anmStart(15, 24589);
- _smushAnimator->animatorNewAnim(24589);
+ _smushAnim->anmStart(15);
+ _smushAnimator->animatorNewAnim();
}
}
@@ -234,8 +234,8 @@ bool SmushPlayer::prepareFrame() {
_smushDecoder->start();
// The disasm restarts looping movies with the frame rate of the header
int delay = _smushDecoder->getFrameDelay();
- _smushAnim->anmStart(delay ? (delay / 2 + 1000000) / delay : 15, 24589);
- _smushAnimator->animatorNewAnim(24589);
+ _smushAnim->anmStart(delay ? (delay / 2 + 1000000) / delay : 15);
+ _smushAnimator->animatorNewAnim();
} else {
_videoFinished = true;
}
diff --git a/engines/grim/movie/smush_anim.cpp b/engines/grim/movie/smush_anim.cpp
index 4235342cbdb..f2709bdcc3f 100644
--- a/engines/grim/movie/smush_anim.cpp
+++ b/engines/grim/movie/smush_anim.cpp
@@ -45,7 +45,6 @@ SmushAnimPlay::SmushAnimPlay(SmushTimer *timer, SmushSound *sound) {
_sound = sound;
_frameTimer = 0;
_syncTimer = 0;
- _flags = 0;
_finished = 0;
_framesDue = 0;
_lateTicks = 0;
@@ -61,8 +60,7 @@ SmushAnimPlay::~SmushAnimPlay() {
anmStop();
}
-int SmushAnimPlay::anmStart(int fps, int flags) {
- _flags = flags;
+int SmushAnimPlay::anmStart(int fps) {
_lateTicks = 0;
_framesDue = 0;
_skipFlag = 0;
@@ -151,7 +149,7 @@ int SmushAnimPlay::anmRenderBody(SmushAnimClient *client) {
_skipFlag = 1;
}
- int flags = _flags;
+ int flags = 0;
if (_skipFlag)
flags |= 0x8000;
diff --git a/engines/grim/movie/smush_anim.h b/engines/grim/movie/smush_anim.h
index 3c50d9af529..9f8a5e501e6 100644
--- a/engines/grim/movie/smush_anim.h
+++ b/engines/grim/movie/smush_anim.h
@@ -150,7 +150,6 @@ private:
SmushSound *_sound;
int _frameTimer;
int _syncTimer;
- int _flags;
int _finished;
int _framesDue;
int _lateTicks;
@@ -170,7 +169,7 @@ public:
SmushAnimPlay(SmushTimer *timer, SmushSound *sound);
~SmushAnimPlay();
- int anmStart(int fps, int flags);
+ int anmStart(int fps);
void anmStop();
int anmRender(SmushAnimClient *client);
diff --git a/engines/grim/movie/smush_animator.cpp b/engines/grim/movie/smush_animator.cpp
index e6d172bd42e..3bfec4c1cfb 100644
--- a/engines/grim/movie/smush_animator.cpp
+++ b/engines/grim/movie/smush_animator.cpp
@@ -50,7 +50,6 @@ SmushAnimator::~SmushAnimator() {
}
void SmushAnimator::animatorInitialize() {
- _flags = 0;
_frame = 0;
_blockSize = 0;
_tag = SMUSH_TAG_NULL;
@@ -58,11 +57,10 @@ void SmushAnimator::animatorInitialize() {
_pause = 0;
}
-void SmushAnimator::animatorNewAnim(int flags) {
+void SmushAnimator::animatorNewAnim() {
animatorInitialize();
_frameCount = _decoder->getFrameCount();
- _flags = flags;
}
void SmushAnimator::animatorPause(bool pause) {
@@ -96,8 +94,8 @@ void SmushAnimator::readBlock(int size) {
}
void SmushAnimator::decodeBlocks(int flags) {
- int allFlags = _flags | flags;
- int decodeFlags = ((allFlags >> 13) & 4) | (allFlags & 0x30);
+ // Only the "late" bit (0x8000) gets to the decoder, as flag 4
+ int decodeFlags = (flags >> 13) & 4;
_decoder->decodeFrameBlock(_block, _blockSize, decodeFlags);
}
diff --git a/engines/grim/movie/smush_animator.h b/engines/grim/movie/smush_animator.h
index e1bde954eda..36859b92410 100644
--- a/engines/grim/movie/smush_animator.h
+++ b/engines/grim/movie/smush_animator.h
@@ -165,7 +165,6 @@ private:
int _frameCount;
int _blockSize;
uint32 _tag;
- int _flags;
int _pause;
void animatorInitialize();
@@ -177,7 +176,7 @@ public:
SmushAnimator(SmushDecoder *decoder);
~SmushAnimator();
- void animatorNewAnim(int flags);
+ void animatorNewAnim();
int animatorRender(int flags);
void animatorPause(bool pause);
More information about the Scummvm-git-logs
mailing list