1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
/* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "bladerunner/vqa_player.h"
#include "bladerunner/bladerunner.h"
#include "audio/decoders/raw.h"
#include "common/system.h"
namespace BladeRunner {
bool VQAPlayer::open(const Common::String &name) {
_s = _vm->getResourceStream(name);
if (!_s) {
return false;
}
if (!_decoder.loadStream(_s)) {
delete _s;
_s = nullptr;
return false;
}
_hasAudio = _decoder.hasAudio();
if (_hasAudio) {
_audioStream = Audio::makeQueuingAudioStream(_decoder.frequency(), false);
}
_repeatsCount = 0;
_loop = -1;
_frameBegin = -1;
_frameEnd = _decoder.numFrames() - 1;
_frameEndQueued = -1;
_repeatsCountQueued = -1;
if (_loopInitial >= 0) {
setLoop(_loopInitial, _repeatsCountInitial, kLoopSetModeImmediate, nullptr, nullptr);
} else {
setBeginAndEndFrame(0, _frameEnd, 0, kLoopSetModeJustStart, nullptr, nullptr);
}
return true;
}
void VQAPlayer::close() {
_vm->_mixer->stopHandle(_soundHandle);
delete _s;
_s = nullptr;
}
int VQAPlayer::update() {
uint32 now = 60 * _vm->_system->getMillis();
if (_frameNext < 0) {
_frameNext = _frameBegin;
}
if ((_repeatsCount > 0 || _repeatsCount == -1) && (_frameNext > _frameEnd)) {
int loopEndQueued = _frameEndQueued;
if (_frameEndQueued != -1) {
_frameEnd = _frameEndQueued;
_frameEndQueued = -1;
}
if (_frameNext != _frameBegin) {
_frameNext = _frameBegin;
}
if (loopEndQueued == -1) {
if (_repeatsCount != -1) {
_repeatsCount--;
}
//callback for repeat, it is not used in the blade runner
} else {
_repeatsCount = _repeatsCountQueued;
_repeatsCountQueued = -1;
if (_callbackLoopEnded != nullptr) {
_callbackLoopEnded(_callbackData, 0, _loop);
}
}
_surface = nullptr;
return -1;
}
if (_frameNext > _frameEnd) {
return -3;
}
// TODO: preload audio in better way
int audioPreloadFrames = 3;
if (now >= _frameNextTime) {
int frame = _frameNext;
_decoder.readFrame(_frameNext, 0x2);
_surface = _decoder.decodeVideoFrame();
if (_hasAudio) {
if (!_audioStarted) {
for (int i = 0; i < audioPreloadFrames; i++) {
if (_frameNext + i < _frameEnd) {
_decoder.readFrame(_frameNext + i, 0x1);
queueAudioFrame(_decoder.decodeAudioFrame());
}
}
_vm->_mixer->playStream(Audio::Mixer::kPlainSoundType, &_soundHandle, _audioStream);
_audioStarted = true;
}
if (_frameNext + audioPreloadFrames < _frameEnd) {
_decoder.readFrame(_frameNext + audioPreloadFrames, 0x1);
queueAudioFrame(_decoder.decodeAudioFrame());
}
}
if (_frameNextTime == 0) {
_frameNextTime = now + 60000 / 15;
} else {
_frameNextTime += 60000 / 15;
}
_frameNext++;
return frame;
}
_surface = nullptr;
return -1;
}
const Graphics::Surface *VQAPlayer::getSurface() const {
return _surface;
}
void VQAPlayer::updateZBuffer(ZBuffer *zbuffer) {
_decoder.decodeZBuffer(zbuffer);
}
void VQAPlayer::updateView(View *view) {
_decoder.decodeView(view);
}
void VQAPlayer::updateLights(Lights *lights) {
_decoder.decodeLights(lights);
}
bool VQAPlayer::setLoop(int loop, int repeatsCount, int loopSetMode, void (*callback)(void *, int, int), void *callbackData) {
debug("VQAPlayer::setBeginAndEndFrameFromLoop(%i, %i, %i), streamLoaded = %i", loop, repeatsCount, loopSetMode, _s != nullptr);
if (_s == nullptr) {
_loopInitial = loop;
_repeatsCountInitial = repeatsCount;
return true;
}
int begin, end;
if (!_decoder.getLoopBeginAndEndFrame(loop, &begin, &end)) {
return false;
}
if (setBeginAndEndFrame(begin, end, repeatsCount, loopSetMode, callback, callbackData)) {
_loop = loop;
return true;
}
return false;
}
bool VQAPlayer::setBeginAndEndFrame(int begin, int end, int repeatsCount, int loopSetMode, void (*callback)(void *, int, int), void *callbackData) {
debug("VQAPlayer::setBeginAndEndFrame(%i, %i, %i, %i), streamLoaded = %i", begin, end, repeatsCount, loopSetMode, _s != nullptr);
if (repeatsCount < 0) {
repeatsCount = -1;
}
if (_repeatsCount == 0 && loopSetMode == kLoopSetModeEnqueue) {
loopSetMode = kLoopSetModeImmediate;
}
//TODO: there is code in original game which deals with changing loop at start of loop, is it nescesarry? loc_46EA04
_frameBegin = begin;
if (loopSetMode == kLoopSetModeJustStart) {
_repeatsCount = repeatsCount;
} else if (loopSetMode == kLoopSetModeEnqueue) {
_repeatsCountQueued = repeatsCount;
_frameEndQueued = end;
} else if (loopSetMode == kLoopSetModeImmediate) {
_repeatsCount = repeatsCount;
_frameEnd = end;
seekToFrame(begin);
}
_callbackLoopEnded = callback;
_callbackData = callbackData;
return true;
}
bool VQAPlayer::seekToFrame(int frame) {
_frameNext = frame;
_frameNextTime = 60 * _vm->_system->getMillis();
return true;
}
int VQAPlayer::getLoopBeginFrame(int loop) {
int begin, end;
if (!_decoder.getLoopBeginAndEndFrame(loop, &begin, &end)) {
return -1;
}
return begin;
}
int VQAPlayer::getLoopEndFrame(int loop) {
int begin, end;
if (!_decoder.getLoopBeginAndEndFrame(loop, &begin, &end)) {
return -1;
}
return end;
}
void VQAPlayer::queueAudioFrame(Audio::AudioStream *audioStream) {
int n = _audioStream->numQueuedStreams();
if (n == 0)
warning("numQueuedStreams: %d", n);
_audioStream->queueAudioStream(audioStream, DisposeAfterUse::YES);
}
} // End of namespace BladeRunner
|