aboutsummaryrefslogtreecommitdiff
path: root/engines/titanic/support/avi_surface.cpp
blob: e371ccb8120afaa69aa24a596c06e909c91cf487 (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
/* 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 "titanic/support/avi_surface.h"
#include "titanic/support/video_surface.h"
#include "video/avi_decoder.h"

namespace Titanic {

AVISurface::AVISurface(const CResourceKey &key) {
	_videoSurface = nullptr;
	_field4 = 0;
	_field8 = 0;
	_currentPos = 0;
	_priorFrame = 0;
	_streamCount = 0;
	_frameInfo = nullptr;

	_decoder = new Video::AVIDecoder();
	if (!_decoder->loadFile(key.getString()))
		error("Could not open video - %s", key.getString().c_str());
}

AVISurface::~AVISurface() {
	if (_videoSurface)
		_videoSurface->_blitStyleFlag = false;
	delete _frameInfo;
	delete _decoder;
}

bool AVISurface::play(uint flags, CGameObject *obj) {
	if (flags & MOVIE_REVERSE)
		return play(_decoder->getFrameCount() - 1, 0, flags, obj);
	else
		return play(0, _decoder->getFrameCount() - 1, flags, obj);
}

bool AVISurface::play(int startFrame, int endFrame, uint flags, CGameObject *obj) {
	if (flags & MOVIE_STOP_PREVIOUS)
		stop();

	return play(startFrame, endFrame, -1, flags, obj);
}

bool AVISurface::play(int startFrame, int endFrame, int initialFrame, uint flags, CGameObject *obj) {
	CMovieRangeInfo *info = new CMovieRangeInfo();
	info->_startFrame = startFrame;
	info->_endFrame = endFrame;
	info->_isReversed = endFrame < startFrame;
	info->_isFlag1 = flags & MOVIE_1;

	if (obj) {
		CMovieEvent *me = new CMovieEvent();
		me->_type = MET_MOVIE_END;
		me->_startFrame = startFrame;
		me->_endFrame = endFrame;
		me->_initialFrame = 0;
		me->_gameObject = obj;
		
		info->addEvent(me);
	}

	_movieRangeInfo.push_back(info);
	
	if (_movieRangeInfo.size() == 1) {
		changeFrame(initialFrame);
	} else {
		return true;
	}	
}

void AVISurface::stop() {
	_isPlaying = false;
	_decoder->stop();
	_movieRangeInfo.destroyContents();
}

bool AVISurface::changeFrame(int frameNumber) {
	if (_isPlaying)
		return false;

	if (frameNumber == -1)
		// Default to starting frame of first movie range
		frameNumber = _movieRangeInfo.front()->_startFrame;

	seekToFrame(frameNumber);
	renderFrame();

	_isPlaying = true;
	return true;
}

void AVISurface::seekToFrame(uint frameNumber) {
	_decoder->seekToFrame(frameNumber);
	_priorFrame = frameNumber;
}

bool AVISurface::handleEvents(CMovieEventList &events) {
	if (!_isPlaying)
		return true;

	CMovieRangeInfo *info = _movieRangeInfo.front();
	_currentPos += info->_isReversed ? -1 : 1;
	if ((info->_isReversed && _currentPos < info->_endFrame) ||
		(!info->_isReversed && _currentPos > info->_endFrame)) {
		if (info->_isFlag1) {
			info->getMovieEnd(events);
			_movieRangeInfo.remove(info);
			delete info;

			if (_movieRangeInfo.empty()) {
				// NO more ranges, so stop playback
				stop();
			} else {
				// Not empty, so move onto new first one
				info = _movieRangeInfo.front();
				_currentPos = info->_startFrame;
			}
		} else {
			_currentPos = info->_startFrame;
		}
	}

	if (_isPlaying) {
		// SInce movie ranges can change the position in the movie,
		// ensure the decoder is kept in sync
		seekToFrame(_currentPos);
				
		info->getMovieFrame(events, _currentPos);
		return renderFrame();
	} else {
		return false;
	}
}

void AVISurface::setVideoSurface(CVideoSurface *surface) {
	_videoSurface = surface;

	warning("TODO: Get video track list from video decoder");
}

uint AVISurface::getWidth() const {
	return _decoder->getWidth();
}

uint AVISurface::getHeight() const {
	return _decoder->getHeight();
}

void AVISurface::setFrame(int frameNumber) {
	// If playback was in process, stop it
	if (_isPlaying)
		stop();

	// Ensure the frame number is valid
	if (frameNumber >= _decoder->getFrameCount())
		frameNumber = _decoder->getFrameCount() - 1;

	seekToFrame(frameNumber);
	renderFrame();
}

int AVISurface::getFrame() const {
	return _decoder->getCurFrame();
}

bool AVISurface::renderFrame() {
	// Check there's a frame ready for 
	assert(_videoSurface);
	if (!_decoder->needsUpdate())
		return false;

	// Get the frame to render, and draw it on the surface
	const Graphics::Surface *frame = _decoder->decodeNextFrame();
	_videoSurface->blitFrom(Point(0, 0), frame);
	return false;
}

bool AVISurface::addEvent(int frameNumber, CGameObject *obj) {
	if (!_movieRangeInfo.empty()) {
		CMovieRangeInfo *tail = _movieRangeInfo.back();
		if (frameNumber == -1)
			frameNumber = tail->_startFrame;

		CMovieEvent *me = new CMovieEvent();
		me->_type = MET_FRAME;
		me->_startFrame = 0;
		me->_endFrame = 0;
		me->_initialFrame = frameNumber;
		me->_gameObject = obj;
		tail->addEvent(me);

		return _movieRangeInfo.size() == 1 && frameNumber == _priorFrame;
	}

	return false;
}

void AVISurface::setFrameRate(double rate) {
	if (rate >= 0.0 && rate <= 100.0) {
		_frameRate = rate;
		warning("TODO: Frame rate set to %d yet to be implemented", (int)rate);
	}
}

void *AVISurface::duplicateFrameInfo() const {
	// TODO
	return nullptr;
}

} // End of namespace Titanic