aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/video/robot_decoder.cpp
blob: 8dd60cd9a6b7fecbb754f75846128d5cbd5112a3 (plain)
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/* 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.
 *
 * $URL$
 * $Id$
 *
 */

#include "common/debug.h"
#include "common/endian.h"
#include "common/archive.h"
#include "common/stream.h"
#include "common/system.h"
#include "common/util.h"

#include "graphics/surface.h"
#include "sound/decoders/raw.h"

#include "sci/resource.h"
#include "sci/util.h"
#include "sci/sound/audio.h"
#include "sci/video/robot_decoder.h"

namespace Sci {

// TODO:
// - Positioning
// - Proper handling of frame scaling - scaled frames look squashed
//   (probably because both dimensions should be scaled)
// - Transparency support
// - Timing - the arbitrary 100ms delay between each frame is not quite right
// - Proper handling of sound chunks in some cases, so that the frame size
//   table can be ignored (it's only used to determine the correct sound chunk
//   size at the moment, cause it can be wrong in some cases)
// - Fix audio "hiccups" - probably data that shouldn't be in the audio frames


// Some non technical information on robot files, from an interview with
// Greg Tomko-Pavia of Sierra On-Line
// Taken from http://anthonylarme.tripod.com/phantas/phintgtp.html
//
// (...) What we needed was a way of playing video, but have it blend into
// normal room art instead of occupying its own rectangular area. Room art 
// consists of a background pic overlaid with various animating cels 
// (traditional lingo: sprites). The cels each have a priority that determines 
// who is on top and who is behind in the drawing order. Cels are read from 
// *.v56 files (another proprietary format). A Robot is video frames with 
// transparent background including priority and x,y information. Thus, it is 
// like a cel, except it comes from an RBT - not a v56. Because it blends into
// our graphics engine, it looks just like a part of the room. A RBT can move 
// around the screen and go behind other objects. (...)

#ifdef ENABLE_SCI32

enum robotPalTypes {
	kRobotPalVariable = 0,
	kRobotPalConstant = 1
};

RobotDecoder::RobotDecoder(Audio::Mixer *mixer, bool isBigEndian) {
	_surface = 0;
	_width = 0;
	_height = 0;
	_fileStream = 0;
	_audioStream = 0;
	_dirtyPalette = false;
	_pos = Common::Point(0, 0);
	_mixer = mixer;
	_isBigEndian = isBigEndian;
}

RobotDecoder::~RobotDecoder() {
	close();
}

bool RobotDecoder::load(GuiResourceId id) {
	Common::String fileName = Common::String::format("%d.rbt", id);
	Common::SeekableReadStream *stream = SearchMan.createReadStreamForMember(fileName);

	if (!stream) {
		warning("Unable to open robot file %s", fileName.c_str());
		return false;
	}

	return loadStream(stream);
}

bool RobotDecoder::loadStream(Common::SeekableReadStream *stream) {
	close();

	_fileStream = new Common::SeekableSubReadStreamEndian(stream, 0, stream->size(), _isBigEndian, DisposeAfterUse::YES);
	_surface = new Graphics::Surface();

	readHeaderChunk();
	
	// There are several versions of robot files, ranging from 3 to 6.
	// v3: no known examples
	// v4: PQ:SWAT demo
	// v5: SCI2.1 and SCI3 games
	// v6: SCI3 games
	if (_header.version < 4 || _header.version > 6)
		error("Unknown robot version: %d", _header.version);

	if (_header.hasSound) {
		_audioStream = Audio::makeQueuingAudioStream(11025, false);
		_mixer->playStream(Audio::Mixer::kMusicSoundType, &_audioHandle, _audioStream);
	}

	readPaletteChunk(_header.paletteDataSize);
	readFrameSizesChunk();
	calculateVideoDimensions();
	_surface->create(_width, _height, 1);

	return true;
}

void RobotDecoder::readHeaderChunk() {
	// Header (60 bytes)
	_fileStream->skip(6);
	_header.version = _fileStream->readUint16();
	_header.audioChunkSize = _fileStream->readUint16();
	_header.audioSilenceSize = _fileStream->readUint16();
	_fileStream->skip(2);
	_header.frameCount = _fileStream->readUint16();
	_header.paletteDataSize = _fileStream->readUint16();
	_header.unkChunkDataSize = _fileStream->readUint16();
	_fileStream->skip(5);
	_header.hasSound = _fileStream->readByte();
	_fileStream->skip(34);

	// Some videos (e.g. robot 1305 in Phantasmagoria and
	// robot 184 in Lighthouse) have an unknown chunk before
	// the palette chunk (probably used for sound preloading).
	// Skip it here.
	if (_header.unkChunkDataSize)
		_fileStream->skip(_header.unkChunkDataSize);
}

void RobotDecoder::readPaletteChunk(uint16 chunkSize) {
	byte *paletteData = new byte[chunkSize];
	_fileStream->read(paletteData, chunkSize);

	// SCI1.1 palette
	byte palFormat = paletteData[32];
	uint16 palColorStart = paletteData[25];
	uint16 palColorCount = READ_SCI11ENDIAN_UINT16(paletteData + 29);

	int palOffset = 37;
	memset(_palette, 0, 256 * 3);

	for (uint16 colorNo = palColorStart; colorNo < palColorStart + palColorCount; colorNo++) {
		if (palFormat == kRobotPalVariable)
			palOffset++;
		_palette[colorNo * 3 + 0] = paletteData[palOffset++];
		_palette[colorNo * 3 + 1] = paletteData[palOffset++];
		_palette[colorNo * 3 + 2] = paletteData[palOffset++];
	}

	_dirtyPalette = true;
	delete[] paletteData;
}


void RobotDecoder::readFrameSizesChunk() {
	// The robot video file contains 2 tables, with one entry for each frame:
	// - A table containing the size of the image in each video frame
	// - A table containing the total size of each video frame.
	// In v5 robots, the tables contain 16-bit integers, whereas in v6 robots,
	// they contain 32-bit integers.

	_frameTotalSize = new uint32[_header.frameCount];

	// TODO: The table reading code can probably be removed once the
	// audio chunk size is figured out (check the TODO inside processNextFrame())
#if 0
	// We don't need any of the two tables to play the video, so we ignore
	// both of them.
	uint16 wordSize = _header.version == 6 ? 4 : 2;
	_fileStream->skip(_header.frameCount * wordSize * 2);
#else
	switch (_header.version) {
	case 4:
	case 5:		// sizes are 16-bit integers
		// Skip table with frame image sizes, as we don't need it
		_fileStream->skip(_header.frameCount * 2);
		for (int i = 0; i < _header.frameCount; ++i)
			_frameTotalSize[i] = _fileStream->readUint16();
		break;
	case 6:		// sizes are 32-bit integers
		// Skip table with frame image sizes, as we don't need it
		_fileStream->skip(_header.frameCount * 4);
		for (int i = 0; i < _header.frameCount; ++i)
			_frameTotalSize[i] = _fileStream->readUint32();
		break;
	default:
		error("Can't yet handle index table for robot version %d", _header.version);
	}
#endif

	// 2 more unknown tables
	_fileStream->skip(1024 + 512);

	// Pad to nearest 2 kilobytes
	uint32 curPos = _fileStream->pos();
	if (curPos & 0x7ff)
		_fileStream->seek((curPos & ~0x7ff) + 2048);
}

void RobotDecoder::calculateVideoDimensions() {
	// This is an O(n) operation, as each frame has a different size.
	// We need to know the actual frame size to have a constant video size.
	uint32 pos = _fileStream->pos();
	
	for (uint32 curFrame = 0; curFrame < _header.frameCount; curFrame++) {
		_fileStream->skip(4);
		uint16 frameWidth = _fileStream->readUint16();
		uint16 frameHeight = _fileStream->readUint16();
		if (frameWidth > _width)
			_width = frameWidth;
		if (frameHeight > _height)
			_height = frameHeight;
		_fileStream->skip(_frameTotalSize[curFrame] - 8);
	}

	_fileStream->seek(pos);
}

const Graphics::Surface *RobotDecoder::decodeNextFrame() {
	// Read frame image header (24 bytes)
	_fileStream->skip(3);
	byte frameScale = _fileStream->readByte();
	uint16 frameWidth = _fileStream->readUint16();
	uint16 frameHeight = _fileStream->readUint16();
	_fileStream->skip(4); // unknown, almost always 0
	uint16 frameX = _fileStream->readUint16();
	uint16 frameY = _fileStream->readUint16();
	uint16 compressedSize = _fileStream->readUint16();
	uint16 frameFragments = _fileStream->readUint16();
	_fileStream->skip(4); // unknown
	uint32 decompressedSize = frameWidth * frameHeight * frameScale / 100;
	// FIXME: A frame's height + position can go off limits... why? With the
	// following, we cut the contents to fit the frame
	uint16 scaledHeight = CLIP<uint16>(decompressedSize / frameWidth, 0, _height - frameY);
	// FIXME: Same goes for the frame's width + position. In this case, we
	// modify the position to fit the contents on screen.
	if (frameWidth + frameX > _width)
		frameX = _width - frameWidth;
	assert (frameWidth + frameX <= _width && scaledHeight + frameY <= _height);

	DecompressorLZS lzs;
	byte *decompressedFrame = new byte[decompressedSize];
	byte *outPtr = decompressedFrame;

	if (_header.version == 4) {
		// v4 has just the one fragment, it seems, and ignores the fragment count
		Common::SeekableSubReadStream fragmentStream(_fileStream, _fileStream->pos(), _fileStream->pos() + compressedSize);
		lzs.unpack(&fragmentStream, outPtr, compressedSize, decompressedSize);
	} else {
		for (uint16 i = 0; i < frameFragments; ++i) {
			uint32 compressedFragmentSize = _fileStream->readUint32();
			uint32 decompressedFragmentSize = _fileStream->readUint32();
			uint16 compressionType = _fileStream->readUint16();

			if (compressionType == 0) {
				Common::SeekableSubReadStream fragmentStream(_fileStream, _fileStream->pos(), _fileStream->pos() + compressedFragmentSize);
				lzs.unpack(&fragmentStream, outPtr, compressedFragmentSize, decompressedFragmentSize);
			} else if (compressionType == 2) {	// untested
				_fileStream->read(outPtr, compressedFragmentSize);
			} else {
				error("Unknown frame compression found: %d", compressionType);
			}

			outPtr += decompressedFragmentSize;
		}
	}

	// Copy over the decompressed frame
	byte *inFrame = decompressedFrame;
	byte *outFrame = (byte *)_surface->pixels;

	// Black out the surface
	memset(outFrame, 0, _width * _height);

	// Move to the correct y coordinate
	outFrame += _width * frameY;

	for (uint16 y = 0; y < scaledHeight; y++) {
		memcpy(outFrame + frameX, inFrame, frameWidth);
		inFrame += frameWidth;
		outFrame += _width;
	}

	delete[] decompressedFrame;

	// +1 because we start with frame number -1
	uint32 audioChunkSize = _frameTotalSize[_curFrame + 1] - (24 + compressedSize);

// TODO: The audio chunk size below is usually correct, but there are some
// exceptions (e.g. robot 4902 in Phantasmagoria, towards its end)
#if 0
	// Read frame audio header (14 bytes)
	_fileStream->skip(2); // buffer position
	_fileStream->skip(2); // unknown (usually 1)
	_fileStream->skip(2); /*uint16 audioChunkSize = _fileStream->readUint16() + 8;*/
	_fileStream->skip(2);
#endif

	// Queue the next audio frame
	// FIXME: For some reason, there are audio hiccups/gaps
	if (_header.hasSound) {
		_fileStream->skip(8);	// header
		_audioStream->queueBuffer(g_sci->_audio->getDecodedRobotAudioFrame(_fileStream, audioChunkSize - 8), 
									(audioChunkSize - 8) * 2, DisposeAfterUse::NO, 
									Audio::FLAG_16BITS | Audio::FLAG_LITTLE_ENDIAN);
	} else {
		_fileStream->skip(audioChunkSize);
	}

	if (_curFrame == -1)
		_startTime = g_system->getMillis();

	_curFrame++;

	return _surface;
}

void RobotDecoder::close() {
	if (!_fileStream)
		return;

	delete _fileStream;
	_fileStream = 0;

	_surface->free();
	delete _surface;
	_surface = 0;

	if (_header.hasSound) {
		_mixer->stopHandle(_audioHandle);
		//delete _audioStream; _audioStream = 0;
	}

	reset();
}	

#endif

} // End of namespace Sci