aboutsummaryrefslogtreecommitdiff
path: root/engines/mohawk/video.cpp
blob: ae20a59370fdd04402569195c91b979f676b3887 (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* 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 "mohawk/mohawk.h"
#include "mohawk/resource.h"
#include "mohawk/video.h"

#include "common/algorithm.h"
#include "common/debug.h"
#include "common/events.h"
#include "common/textconsole.h"
#include "common/system.h"

#include "graphics/palette.h"
#include "graphics/surface.h"

#include "video/qt_decoder.h"


namespace Mohawk {

VideoEntry::VideoEntry() : _video(nullptr), _id(-1), _x(0), _y(0), _loop(false), _enabled(true) {
}

VideoEntry::VideoEntry(Video::VideoDecoder *video, const Common::String &fileName) : _video(video), _fileName(fileName), _id(-1), _x(0), _y(0), _loop(false), _enabled(true) {
}

VideoEntry::VideoEntry(Video::VideoDecoder *video, int id) : _video(video), _id(id), _x(0), _y(0), _loop(false), _enabled(true) {
}

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

void VideoEntry::close() {
	delete _video;
	_video = nullptr;
}

bool VideoEntry::endOfVideo() const {
	return !isOpen() || _video->endOfVideo();
}

int VideoEntry::getCurFrame() const {
	assert(_video);
	return _video->getCurFrame();
}

uint32 VideoEntry::getFrameCount() const {
	assert(_video);
	return _video->getFrameCount();
}

uint32 VideoEntry::getTime() const {
	assert(_video);
	return _video->getTime();
}

Audio::Timestamp VideoEntry::getDuration() const {
	assert(_video);
	return _video->getDuration();
}

Common::Rational VideoEntry::getRate() const {
	assert(_video);
	return _video->getRate();
}

void VideoEntry::center() {
	assert(_video);
	_x = (g_system->getWidth() - _video->getWidth()) / 2;
	_y = (g_system->getHeight() - _video->getHeight()) / 2;
}

void VideoEntry::setBounds(const Audio::Timestamp &startTime, const Audio::Timestamp &endTime) {
	assert(_video);
	_start = startTime;
	_video->setEndTime(endTime);
	_video->seek(startTime);
}

void VideoEntry::seek(const Audio::Timestamp &time) {
	assert(_video);
	_video->seek(time);
}

void VideoEntry::setRate(const Common::Rational &rate) {
	assert(_video);
	_video->setRate(rate);
}

void VideoEntry::pause(bool isPaused) {
	assert(_video);
	_video->pauseVideo(isPaused);
}

void VideoEntry::start() {
	assert(_video);
	_video->start();
}

void VideoEntry::stop() {
	assert(_video);
	_video->stop();
}

bool VideoEntry::isPlaying() const {
	assert(_video);
	return _video->isPlaying();
}

int VideoEntry::getVolume() const {
	assert(_video);
	return _video->getVolume();
}

void VideoEntry::setVolume(int volume) {
	assert(_video);
	_video->setVolume(CLIP(volume, 0, 255));
}

VideoManager::VideoManager(MohawkEngine *vm) : _vm(vm) {
	// Set dithering enabled, if required
	_enableDither = (_vm->getGameType() == GType_MYST || _vm->getGameType() == GType_MAKINGOF) && !(_vm->getFeatures() & GF_ME);
}

VideoManager::~VideoManager() {
	stopVideos();
}

void VideoManager::pauseVideos() {
	for (VideoList::iterator it = _videos.begin(); it != _videos.end(); it++)
		(*it)->pause(true);
}

void VideoManager::resumeVideos() {
	for (VideoList::iterator it = _videos.begin(); it != _videos.end(); it++)
		(*it)->pause(false);
}

void VideoManager::stopVideos() {
	for (VideoList::iterator it = _videos.begin(); it != _videos.end(); it++)
		(*it)->close();

	_videos.clear();
}

VideoEntryPtr VideoManager::playMovie(const Common::String &fileName, Audio::Mixer::SoundType soundType) {
	VideoEntryPtr ptr = open(fileName, soundType);
	if (!ptr)
		return VideoEntryPtr();

	ptr->start();
	return ptr;
}

VideoEntryPtr VideoManager::playMovie(uint16 id) {
	VideoEntryPtr ptr = open(id);
	if (!ptr)
		return VideoEntryPtr();

	ptr->start();
	return ptr;
}

bool VideoManager::updateMovies() {
	bool updateScreen = false;

	for (VideoList::iterator it = _videos.begin(); it != _videos.end(); ) {
		// Check of the video has reached the end
		if ((*it)->endOfVideo()) {
			if ((*it)->isLooping()) {
				// Seek back if looping
				(*it)->seek((*it)->getStart());
			} else {
				// Done; close and continue on
				(*it)->close();
				it = _videos.erase(it);
				continue;
			}
		}

		Video::VideoDecoder *video = (*it)->_video;

		// Ignore paused videos
		if (video->isPaused()) {
			it++;
			continue;
		}

		// Check if we need to draw a frame
		if (video->needsUpdate()) {
			if (drawNextFrame(*it)) {
				updateScreen = true;
			}
		}

		// Remember to increase the iterator
		it++;
	}

	// Return true if we need to update the screen
	return updateScreen;
}

bool VideoManager::drawNextFrame(VideoEntryPtr videoEntry) {
	Video::VideoDecoder *video = videoEntry->_video;
	const Graphics::Surface *frame = video->decodeNextFrame();

	if (!frame || !videoEntry->isEnabled()) {
		return false;
	}

	Graphics::Surface *convertedFrame = nullptr;
	Graphics::PixelFormat pixelFormat = _vm->_system->getScreenFormat();

	if (frame->format != pixelFormat) {
		// We don't support downconverting to 8bpp without having
		// support in the codec. Set _enableDither if shows up.
		if (pixelFormat.bytesPerPixel == 1) {
			warning("Cannot convert high color video frame to 8bpp");
			return false;
		}

		// Convert to the current screen format
		convertedFrame = frame->convertTo(pixelFormat, video->getPalette());
		frame = convertedFrame;
	} else if (pixelFormat.bytesPerPixel == 1 && video->hasDirtyPalette()) {
		// Set the palette when running in 8bpp mode only
		// Don't do this for Myst, which has its own per-stack handling
		if (_vm->getGameType() != GType_MYST)
			_vm->_system->getPaletteManager()->setPalette(video->getPalette(), 0, 256);
	}

	// Clip the video to make sure it stays on the screen (Myst does this a few times)
	Common::Rect targetRect = Common::Rect(video->getWidth(), video->getHeight());
	targetRect.translate(videoEntry->getX(), videoEntry->getY());

	Common::Rect frameRect = Common::Rect(video->getWidth(), video->getHeight());

	if (targetRect.left < 0) {
		frameRect.left -= targetRect.left;
		targetRect.left = 0;
	}

	if (targetRect.top < 0) {
		frameRect.top -= targetRect.top;
		targetRect.top = 0;
	}

	if (targetRect.right > _vm->_system->getWidth()) {
		frameRect.right -= targetRect.right - _vm->_system->getWidth();
		targetRect.right = _vm->_system->getWidth();
	}

	if (targetRect.bottom > _vm->_system->getHeight()) {
		frameRect.bottom -= targetRect.bottom - _vm->_system->getHeight();
		targetRect.bottom = _vm->_system->getHeight();
	}

	_vm->_system->copyRectToScreen(frame->getBasePtr(frameRect.left, frameRect.top), frame->pitch,
	                               targetRect.left, targetRect.top, targetRect.width(), targetRect.height());

	// Delete 8bpp conversion surface
	if (convertedFrame) {
		convertedFrame->free();
		delete convertedFrame;
	}

	// We've drawn something to the screen, make sure we update it
	return true;
}

VideoEntryPtr VideoManager::open(uint16 id) {
	// If this video is already playing, return that handle
	VideoEntryPtr oldVideo = findVideo(id);
	if (oldVideo)
		return oldVideo;

	// Otherwise, create a new entry
	Video::QuickTimeDecoder *video = new Video::QuickTimeDecoder();
	video->setChunkBeginOffset(_vm->getResourceOffset(ID_TMOV, id));
	video->loadStream(_vm->getResource(ID_TMOV, id));

	// Create the entry
	VideoEntryPtr entry(new VideoEntry(video, id));

	// Enable dither if necessary
	checkEnableDither(entry);

	// Add it to the video list
	_videos.push_back(entry);

	return entry;
}

VideoEntryPtr VideoManager::open(const Common::String &fileName, Audio::Mixer::SoundType soundType) {
	// If this video is already playing, return that entry
	VideoEntryPtr oldVideo = findVideo(fileName);
	if (oldVideo)
		return oldVideo;

	// Otherwise, create a new entry
	Common::SeekableReadStream *stream = SearchMan.createReadStreamForMember(fileName);
	if (!stream)
		return VideoEntryPtr();

	Video::VideoDecoder *video = new Video::QuickTimeDecoder();
	video->setSoundType(soundType);
	if (!video->loadStream(stream)) {
		// FIXME: Better error handling
		delete video;
		return VideoEntryPtr();
	}

	// Create the entry
	VideoEntryPtr entry(new VideoEntry(video, fileName));

	// Enable dither if necessary
	checkEnableDither(entry);

	// Add it to the video list
	_videos.push_back(entry);

	return entry;
}

VideoEntryPtr VideoManager::findVideo(uint16 id) {
	if (id == 0)
		return VideoEntryPtr();

	for (VideoList::iterator it = _videos.begin(); it != _videos.end(); it++)
		if ((*it)->getID() == id)
			return *it;

	return VideoEntryPtr();
}

VideoEntryPtr VideoManager::findVideo(const Common::String &fileName) {
	if (fileName.empty())
		return VideoEntryPtr();

	for (VideoList::iterator it = _videos.begin(); it != _videos.end(); it++)
		if ((*it)->getFileName().equalsIgnoreCase(fileName))
			return *it;

	return VideoEntryPtr();
}

bool VideoManager::isVideoPlaying() {
	for (VideoList::iterator it = _videos.begin(); it != _videos.end(); it++)
		if (!(*it)->endOfVideo())
			return true;

	return false;
}

void VideoManager::drawVideoFrame(const VideoEntryPtr &video, const Audio::Timestamp &time) {
	assert(video);
	video->seek(time);
	drawNextFrame(video);
	video->stop();
}

VideoManager::VideoList::iterator VideoManager::findEntry(VideoEntryPtr ptr) {
	return Common::find(_videos.begin(), _videos.end(), ptr);
}

void VideoManager::removeEntry(const VideoEntryPtr &video) {
	VideoManager::VideoList::iterator it = findEntry(video);
	if (it != _videos.end())
		_videos.erase(it);
}

void VideoManager::checkEnableDither(VideoEntryPtr &entry) {
	// If we're not dithering, bail out
	if (!_enableDither)
		return;

	// Set the palette
	byte palette[256 * 3];
	g_system->getPaletteManager()->grabPalette(palette, 0, 256);
	entry->_video->setDitheringPalette(palette);

	if (entry->_video->getPixelFormat().bytesPerPixel != 1) {
		if (entry->getFileName().empty())
			error("Failed to set dither for video tMOV %d", entry->getID());
		else
			error("Failed to set dither for video %s", entry->getFileName().c_str());
	}
}

} // End of namespace Mohawk