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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
/* 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 "audio/audiostream.h"
#include "audio/mixer.h"
#include "audio/decoders/raw.h"
#include "common/stream.h"
#include "common/system.h"
#include "engines/engine.h"
#include "video/flic_decoder.h"
#include "chewy/video/cfo_decoder.h"
namespace Chewy {
enum CustomSubChunk {
kChunkFadeIn = 0,
kChunkFadeOut = 1,
kChunkLoadMusic = 2,
kChunkLoadRaw = 3,
kChunkLoadVoc = 4,
kChunkPlayMusic = 5,
kChunkPlaySeq = 6,
kChunkPlayPattern = 7,
kChunkStopMusic = 8,
kChunkWaitMusicEnd = 9,
kChunkSetMusicVolume = 10,
kChunkSetLoopMode = 11,
kChunkPlayRaw = 12,
kChunkPlayVoc = 13,
kChunkSetSoundVolume = 14,
kChunkSetChannelVolume = 15,
kChunkFreeSoundEffect = 16,
kChunkMusicFadeIn = 17,
kChunkMusicFadeOut = 18,
kChunkSetBalance = 19,
kChunkSetSpeed = 20,
kChunkClearScreen = 21
};
bool CfoDecoder::loadStream(Common::SeekableReadStream *stream) {
close();
if (stream->readUint32BE() != MKTAG('C', 'F', 'O', '\0'))
error("Corrupt video resource");
stream->readUint32LE(); // always 0
uint16 frameCount = stream->readUint16LE();
uint16 width = stream->readUint16LE();
uint16 height = stream->readUint16LE();
addTrack(new CfoVideoTrack(stream, frameCount, width, height));
return true;
}
CfoDecoder::CfoVideoTrack::CfoVideoTrack(Common::SeekableReadStream *stream, uint16 frameCount, uint16 width, uint16 height) :
Video::FlicDecoder::FlicVideoTrack(stream, frameCount, width, height, true) {
readHeader();
for (int i = 0; i < MAX_SOUND_EFFECTS; i++) {
_soundEffects[i] = nullptr;
_soundEffectSize[i] = 0;
}
}
CfoDecoder::CfoVideoTrack::~CfoVideoTrack() {
g_engine->_mixer->stopAll();
for (int i = 0; i < MAX_SOUND_EFFECTS; i++) {
delete[] _soundEffects[i];
}
}
void CfoDecoder::CfoVideoTrack::readHeader() {
_frameDelay = _startFrameDelay = _fileStream->readUint32LE();
_offsetFrame1 = _fileStream->readUint32LE();
_offsetFrame2 = 0; // doesn't exist, as CFO videos aren't rewindable
_fileStream->seek(_offsetFrame1);
}
#define FRAME_TYPE 0xF1FA
#define CUSTOM_FRAME_TYPE 0xFAF1
const Graphics::Surface *CfoDecoder::CfoVideoTrack::decodeNextFrame() {
uint16 frameType;
// Read chunk
/*uint32 frameSize =*/ _fileStream->readUint32LE();
frameType = _fileStream->readUint16LE();
switch (frameType) {
case FRAME_TYPE:
handleFrame();
break;
case CUSTOM_FRAME_TYPE:
handleCustomFrame();
break;
default:
error("CfoDecoder::decodeFrame(): unknown main chunk type (type = 0x%02X)", frameType);
break;
}
_curFrame++;
_nextFrameStartTime += _frameDelay;
return _surface;
}
#define FLI_SETPAL 4
#define FLI_SS2 7
#define FLI_BRUN 15
#define FLI_COPY 16
#define PSTAMP 18
void CfoDecoder::CfoVideoTrack::handleFrame() {
uint16 chunkCount = _fileStream->readUint16LE();
// Read subchunks
for (uint32 i = 0; i < chunkCount; ++i) {
uint32 frameSize = _fileStream->readUint32LE();
uint16 frameType = _fileStream->readUint16LE();
uint8 *data = new uint8[frameSize - 6];
_fileStream->read(data, frameSize - 6);
switch (frameType) {
case FLI_SETPAL:
unpackPalette(data);
_dirtyPalette = true;
break;
case FLI_SS2:
decodeDeltaFLC(data);
break;
case FLI_BRUN:
decodeByteRun(data);
break;
case FLI_COPY:
copyFrame(data);
break;
case PSTAMP:
/* PSTAMP - skip for now */
break;
default:
error("FlicDecoder::decodeNextFrame(): unknown subchunk type (type = 0x%02X)", frameType);
break;
}
delete[] data;
}
}
void CfoDecoder::CfoVideoTrack::handleCustomFrame() {
uint16 chunkCount = _fileStream->readUint16LE();
uint16 delay, number, channel, volume, repeat, balance;
Audio::AudioStream *stream;
// Read subchunks
for (uint32 i = 0; i < chunkCount; ++i) {
uint32 frameSize = _fileStream->readUint32LE();
uint16 frameType = _fileStream->readUint16LE();
switch (frameType) {
case kChunkFadeIn:
delay = _fileStream->readUint16LE();
warning("kChunkFadeIn, delay %d", delay);
// TODO
break;
case kChunkFadeOut:
delay = _fileStream->readUint16LE();
warning("kChunkFadeOut, delay %d", delay);
// TODO
break;
case kChunkLoadMusic:
warning("kChunkLoadMusic");
// TODO
_fileStream->skip(frameSize);
break;
case kChunkLoadRaw:
error("Unused chunk kChunkLoadRaw found");
break;
case kChunkLoadVoc:
number = _fileStream->readUint16LE();
assert(number < MAX_SOUND_EFFECTS);
delete[] _soundEffects[number];
_soundEffectSize[number] = frameSize - 2;
_soundEffects[number] = (byte *)malloc(frameSize - 2);
_fileStream->read(_soundEffects[number], frameSize - 2);
break;
case kChunkPlayMusic:
warning("kChunkPlayMusic");
// TODO
_fileStream->skip(frameSize);
break;
case kChunkPlaySeq:
warning("kChunkPlaySeq");
// TODO
_fileStream->skip(frameSize);
break;
case kChunkPlayPattern:
warning("kChunkPlayPattern");
// TODO
_fileStream->skip(frameSize);
break;
case kChunkStopMusic:
g_engine->_mixer->stopHandle(_musicHandle);
break;
case kChunkWaitMusicEnd:
do {
g_system->delayMillis(10);
} while (g_engine->_mixer->isSoundHandleActive(_musicHandle));
break;
case kChunkSetMusicVolume:
volume = _fileStream->readUint16LE() * Audio::Mixer::kMaxChannelVolume / 63;
g_engine->_mixer->setVolumeForSoundType(Audio::Mixer::SoundType::kMusicSoundType, volume);
break;
case kChunkSetLoopMode:
warning("kChunkSetLoopMode");
// TODO
_fileStream->skip(frameSize);
break;
case kChunkPlayRaw:
error("Unused chunk kChunkPlayRaw found");
break;
case kChunkPlayVoc:
number = _fileStream->readUint16LE();
channel = _fileStream->readUint16LE();
volume = _fileStream->readUint16LE() * Audio::Mixer::kMaxChannelVolume / 63;
repeat = _fileStream->readUint16LE();
assert(number < MAX_SOUND_EFFECTS);
assert(channel < MAX_SOUND_EFFECTS);
stream = Audio::makeLoopingAudioStream(
Audio::makeRawStream(_soundEffects[number],
_soundEffectSize[number], 22050, Audio::FLAG_UNSIGNED,
DisposeAfterUse::NO),
(repeat == 0) ? 1 : repeat);
g_engine->_mixer->setVolumeForSoundType(Audio::Mixer::SoundType::kSFXSoundType, volume);
g_engine->_mixer->playStream(Audio::Mixer::kSFXSoundType, &_soundHandle[channel], stream);
break;
case kChunkSetSoundVolume:
volume = _fileStream->readUint16LE() * Audio::Mixer::kMaxChannelVolume / 63;
g_engine->_mixer->setVolumeForSoundType(Audio::Mixer::SoundType::kSFXSoundType, volume);
break;
case kChunkSetChannelVolume:
channel = _fileStream->readUint16LE();
volume = _fileStream->readUint16LE() * Audio::Mixer::kMaxChannelVolume / 63;
assert(channel < MAX_SOUND_EFFECTS);
g_engine->_mixer->setChannelVolume(_soundHandle[channel], volume);
break;
case kChunkFreeSoundEffect:
number = _fileStream->readUint16LE();
assert(number < MAX_SOUND_EFFECTS);
delete[] _soundEffects[number];
_soundEffects[number] = nullptr;
break;
case kChunkMusicFadeIn:
warning("kChunkMusicFadeIn");
// TODO
_fileStream->skip(frameSize);
break;
case kChunkMusicFadeOut:
warning("kChunkMusicFadeOut");
// TODO
_fileStream->skip(frameSize);
break;
case kChunkSetBalance:
channel = _fileStream->readUint16LE();
balance = (_fileStream->readUint16LE() * 2) - 127;
assert(channel < MAX_SOUND_EFFECTS);
g_engine->_mixer->setChannelBalance(_soundHandle[channel], balance);
break;
case kChunkSetSpeed:
warning("kChunkSetSpeed");
// TODO
_fileStream->skip(frameSize);
break;
case kChunkClearScreen:
g_system->fillScreen(0);
break;
default:
error("Unknown subchunk: %d", frameType);
break;
}
}
}
} // End of namespace Chewy
|