aboutsummaryrefslogtreecommitdiff
path: root/engines/made/pmvplayer.cpp
blob: db56235a672114e030d5d4050c89409c9934209b (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
/* 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 "made/pmvplayer.h"
#include "made/screen.h"

namespace Made {

PmvPlayer::PmvPlayer(MadeEngine *vm, Audio::Mixer *mixer) : _fd(NULL), _vm(vm), _mixer(mixer) {
}

PmvPlayer::~PmvPlayer() {
}

void PmvPlayer::play(const char *filename) {

	_abort = false;
	_surface = NULL;

	_fd = new Common::File();
	_fd->open(filename);

	uint32 chunkType, chunkSize;

	readChunk(chunkType, chunkSize);	// "MOVE"
	readChunk(chunkType, chunkSize);	// "MHED"

	// TODO: Evaluate header
	//_fd->skip(0x3A);

	uint frameDelay = _fd->readUint16LE();
	_fd->skip(10);
	uint soundFreq = _fd->readUint16LE();
	// FIXME: weird frequencies... (11127 or 22254)
	if (soundFreq == 11127) soundFreq = 11025;
	if (soundFreq == 22254) soundFreq = 22050;

	int unk;

	for (int i = 0; i < 22; i++) {
		unk = _fd->readUint16LE();
		debug(2, "%i ", unk);
	}
	debug(2, "\n");

	_mixer->stopAll();

	// Read palette
	_fd->read(_paletteRGB, 768);
	_vm->_screen->setRGBPalette(_paletteRGB);

	uint32 frameCount = 0;
	uint16 chunkCount = 0;
	uint32 soundSize = 0;
	uint32 palChunkOfs = 0;
	uint32 palSize = 0;
	byte *frameData, *audioData, *soundData, *palData, *imageData;
	bool firstTime = true;
	
	uint32 soundStartTime = 0, skipFrames = 0;

	uint32 frameNum;
	uint16 width, height, cmdOffs, pixelOffs, maskOffs, lineSize;

	// TODO: Sound can still be a little choppy. A bug in the decoder or -
	// perhaps more likely - do we have to implement double buffering to
	// get it to work well?
	_audioStream = Audio::makeAppendableAudioStream(soundFreq, Audio::Mixer::FLAG_UNSIGNED);

	while (!_abort && !_fd->eof()) {

		int32 frameTime = _vm->_system->getMillis();

		readChunk(chunkType, chunkSize);

		if (_fd->eof())
			break;

		frameData = new byte[chunkSize];
		_fd->read(frameData, chunkSize);

		// Handle audio
		audioData = frameData + READ_LE_UINT32(frameData + 8) - 8;
		chunkSize = READ_LE_UINT16(audioData + 4);
		chunkCount = READ_LE_UINT16(audioData + 6);

		if (chunkCount > 50) break;	// FIXME: this is a hack

		debug(1, "chunkCount = %d; chunkSize = %d; total = %d\n", chunkCount, chunkSize, chunkCount * chunkSize);

		soundSize = chunkCount * chunkSize;
		soundData = new byte[soundSize];
		decompressSound(audioData + 8, soundData, chunkSize, chunkCount);
		_audioStream->queueBuffer(soundData, soundSize);

		// Handle palette
		palChunkOfs = READ_LE_UINT32(frameData + 16);
		if (palChunkOfs) {
			palData = frameData + palChunkOfs - 8;
			palSize = READ_LE_UINT32(palData + 4);
			decompressPalette(palData + 8, _paletteRGB, palSize);
			_vm->_screen->setRGBPalette(_paletteRGB);
		}

		// Handle video
		imageData = frameData + READ_LE_UINT32(frameData + 12) - 8;

		frameNum = READ_LE_UINT32(frameData);
		width = READ_LE_UINT16(imageData + 8);
		height = READ_LE_UINT16(imageData + 10);
		cmdOffs = READ_LE_UINT16(imageData + 12);
		pixelOffs = READ_LE_UINT16(imageData + 16);
		maskOffs = READ_LE_UINT16(imageData + 20);
		lineSize = READ_LE_UINT16(imageData + 24);

		debug(2, "width = %d; height = %d; cmdOffs = %04X; pixelOffs = %04X; maskOffs = %04X; lineSize = %d\n",
			width, height, cmdOffs, pixelOffs, maskOffs, lineSize);

		if (!_surface) {
			_surface = new Graphics::Surface();
			_surface->create(width, height, 1);
		}

		decompressImage(imageData, *_surface, cmdOffs, pixelOffs, maskOffs, lineSize, frameNum > 0);
	
		if (firstTime) {
			_mixer->playInputStream(Audio::Mixer::kPlainSoundType, &_audioStreamHandle, _audioStream);
			soundStartTime = g_system->getMillis();
			skipFrames = 0;
			firstTime = false;
		}

		handleEvents();
		updateScreen();

		delete[] frameData;

		if (skipFrames == 0) {
			int32 waitTime = (frameCount * frameDelay) -
				(g_system->getMillis() - soundStartTime) - (_vm->_system->getMillis() - frameTime);

			if (waitTime < 0) {
				skipFrames = -waitTime / frameDelay;
				warning("Video A/V sync broken, skipping %d frame(s)", skipFrames + 1);
			} else if (waitTime > 0)
				g_system->delayMillis(waitTime);

		} else
			skipFrames--;

		frameCount++;

	}

	_audioStream->finish();
	_mixer->stopHandle(_audioStreamHandle);
	
	//delete _audioStream;
	delete _fd;
	delete _surface;

}

void PmvPlayer::readChunk(uint32 &chunkType, uint32 &chunkSize) {
	chunkType = _fd->readUint32BE();
	chunkSize = _fd->readUint32LE();

	debug(2, "ofs = %08X; chunkType = %c%c%c%c; chunkSize = %d\n",
		_fd->pos(),
		(chunkType >> 24) & 0xFF, (chunkType >> 16) & 0xFF, (chunkType >> 8) & 0xFF, chunkType & 0xFF,
		chunkSize);

}

void PmvPlayer::handleEvents() {
	Common::Event event;
	while (_vm->_system->getEventManager()->pollEvent(event)) {
		switch (event.type) {
		case Common::EVENT_KEYDOWN:
			if (event.kbd.keycode == Common::KEYCODE_ESCAPE)
				_abort = true;
			break;
		case Common::EVENT_QUIT:
			_vm->_quit = true;
			_abort = true;
			break;
		default:
			break;
		}
	}
}

void PmvPlayer::updateScreen() {
	_vm->_system->copyRectToScreen((const byte*)_surface->pixels, _surface->pitch, 
									(320 - _surface->w) / 2, (200 - _surface->h) / 2, _surface->w, _surface->h);
	_vm->_system->updateScreen();
}

void PmvPlayer::decompressPalette(byte *palData, byte *outPal, uint32 palDataSize) {
	byte *palDataEnd = palData + palDataSize;
	while (palData < palDataEnd) {
		byte count = *palData++;
		byte entry = *palData++;
		if (count == 255 && entry == 255)
			break;
		memcpy(&outPal[entry * 3], palData, (count + 1) * 3);
		palData += (count + 1) * 3;
	}
}

}