aboutsummaryrefslogtreecommitdiff
path: root/engines/groovie/roq.cpp
blob: 589251da315de81e58cfe45e819695cf851df112 (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
/* 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 "groovie/groovie.h"
#include "groovie/roq.h"

#include "sound/mixer.h"

namespace Groovie {

ROQPlayer::ROQPlayer(GroovieEngine *vm) :
	VideoPlayer(vm) {
}

ROQPlayer::~ROQPlayer() {
}

uint16 ROQPlayer::loadInternal() {
	// Begin reading the file
	debugC(1, kGroovieDebugVideo | kGroovieDebugAll, "Groovie::ROQ: Loading video");

	// Read the file header
	ROQBlockHeader blockHeader;
	if (!readBlockHeader(blockHeader)) {
		return 0;
	}
	if (blockHeader.type != 0x1084 || blockHeader.size != 0 || blockHeader.param != 0) {
		return 0;
	}

	// Hardcoded FPS
	return 25;
}

bool ROQPlayer::playFrameInternal() {
	debugC(5, kGroovieDebugVideo | kGroovieDebugAll, "Groovie::ROQ: Playing frame");

	// Process the needed blocks until the next video frame
	bool endframe = false;
	while (!endframe && !_file->eos()) {
		endframe = processBlock();
	}

	// Wait until the current frame can be shown
	waitFrame();

	// Update the screen
	_syst->updateScreen();

	// Return whether the video has ended
	return _file->eos();
}

bool ROQPlayer::readBlockHeader(ROQBlockHeader &blockHeader) {
	if (_file->eos()) {
		return false;
	} else {
		blockHeader.type = _file->readUint16LE();
		blockHeader.size = _file->readUint32LE();
		blockHeader.param = _file->readUint16LE();

		debugC(10, kGroovieDebugVideo | kGroovieDebugAll, "Groovie::ROQ: Block type = 0x%02X", blockHeader.type);
		debugC(10, kGroovieDebugVideo | kGroovieDebugAll, "Groovie::ROQ: Block size = 0x%08X", blockHeader.size);
		debugC(10, kGroovieDebugVideo | kGroovieDebugAll, "Groovie::ROQ: Block param = 0x%04X", blockHeader.param);

		return true;
	}
}

bool ROQPlayer::processBlock() {
	// Read the header of the block
	ROQBlockHeader blockHeader;
	if (!readBlockHeader(blockHeader)) {
		return true;
	}

	bool ok = true;
	bool endframe = false;
	switch (blockHeader.type) {
	case 0x1001: // Video info
		ok = processBlockInfo(blockHeader);
		break;
	case 0x1002: // Quad codebook definition
		ok = processBlockQuadCodebook(blockHeader);
		break;
	case 0x1011: // Quad vector quantised video frame
		ok = processBlockQuadVector(blockHeader);
		endframe = true;
		break;
	case 0x1012: // Still image (JPEG)
		ok = processBlockStill(blockHeader);
		endframe = true;
		break;
	case 0x1013: // Hang
		//warning("Groovie::ROQ: Hang block (skipped)");
		break;
	case 0x1020: // Mono sound samples
		ok = processBlockSoundMono(blockHeader);
		break;
	case 0x1021: // Stereo sound samples
		ok = processBlockSoundStereo(blockHeader);
		break;
	case 0x1030: // Audio container
		ok = processBlockAudioContainer(blockHeader);
		break;
	default:
		error("Groovie::ROQ: Unknown block type: 0x%04X", blockHeader.type);
		ok = false;
	}

	// End the frame when the graphics have been modified or when there's an error
	return endframe || !ok;
}

bool ROQPlayer::processBlockInfo(ROQBlockHeader &blockHeader) {
	debugC(5, kGroovieDebugVideo | kGroovieDebugAll, "Groovie::ROQ: Processing info block");

	// Verify the block header
	if (blockHeader.type != 0x1001 || blockHeader.size != 8 || blockHeader.param != 0) {
		return false;
	}

	uint16 tmp;
	tmp = _file->readUint16LE();
	debugC(5, kGroovieDebugVideo | kGroovieDebugAll, "w = %d\n", tmp);
	if (tmp != 640) {
		return false;
	}
	tmp = _file->readUint16LE();
	debugC(5, kGroovieDebugVideo | kGroovieDebugAll, "h = %d\n", tmp);
	if (tmp != 320) {
		return false;
	}
	tmp = _file->readUint16LE();
	debugC(5, kGroovieDebugVideo | kGroovieDebugAll, "unk1 = %d\n", tmp);
	if (tmp != 8) {
		return false;
	}
	tmp = _file->readUint16LE();
	debugC(5, kGroovieDebugVideo | kGroovieDebugAll, "unk2 = %d\n", tmp);
	if (tmp != 4) {
		return false;
	}
	return true;
}

bool ROQPlayer::processBlockQuadCodebook(ROQBlockHeader &blockHeader) {
	debugC(5, kGroovieDebugVideo | kGroovieDebugAll, "Groovie::ROQ: Processing quad codebook block");

	// Get the number of 2x2 pixel blocks
	_num2blocks = blockHeader.param >> 8;
	if (_num2blocks == 0) {
		_num2blocks = 256;
	}

	// Get the number of 4x4 pixel blocks
	_num4blocks = blockHeader.param & 0xFF;
	if (_num4blocks == 0 && (blockHeader.size > (uint32)_num2blocks * 6)) {
		_num4blocks = 256;
	}

	_file->skip(_num2blocks * 6);
	_file->skip(_num4blocks * 4);

	return true;
}

bool ROQPlayer::processBlockQuadVector(ROQBlockHeader &blockHeader) {
	debugC(5, kGroovieDebugVideo | kGroovieDebugAll, "Groovie::ROQ: Processing quad vector block");
	_file->skip(blockHeader.size);
	return true;

	// Get the mean motion vectors
	//byte Mx = blockHeader.param >> 8;
	//byte My = blockHeader.param & 0xFF;

	int32 ends =_file->pos() + blockHeader.size;
	int numblocks = (640 / 8) * (320 / 8);
	for (int j = 0; j < numblocks && ends > _file->pos(); j++) {
		debugC(5, kGroovieDebugVideo | kGroovieDebugAll, "doing block %d/%d\n", j, numblocks);
		uint16 codingType = _file->readUint16LE();
		for (int i = 0; i < 8; i++) {
			switch (codingType >> 14) {
			case 0: // MOT: Skip block
				//printf("coding type 0\n");
				break;
			case 1: { // FCC: Copy an existing block
				//printf("coding type 1\n");
				byte argument;
				argument = _file->readByte();
				//byte Dx = Mx + (argument >> 4);
				//byte Dy = My + (argument & 0x0F);
				// Dx = X + 8 - (argument >> 4) - Mx
				// Dy = Y + 8 - (argument & 0x0F) - My
				break;
			}
			case 2: { // SLD: Quad vector quantisation
				//printf("coding type 2\n");
				byte argument = _file->readByte();
				if (argument > _num4blocks) {
					//error("invalid 4x4 block %d of %d", argument, _num4blocks);
				}
				// Upsample the 4x4 pixel block
				break;
			}
			case 3: // CCC: 
				//printf("coding type 3:\n");
				processBlockQuadVectorSub(blockHeader);
				break;
			}
			codingType <<= 2;
		}
	}
	debugC(5, kGroovieDebugVideo | kGroovieDebugAll, "Should have ended at %d, and has ended at %d\n", ends, _file->pos());
	return true;
}

bool ROQPlayer::processBlockQuadVectorSub(ROQBlockHeader &blockHeader) {
	debugC(5, kGroovieDebugVideo | kGroovieDebugAll, "Groovie::ROQ: Processing quad vector sub block");

	// Get the mean motion vectors
	//byte Mx = blockHeader.param >> 8;
	//byte My = blockHeader.param & 0xFF;

	uint16 codingType = _file->readUint16LE();
	for (int i = 0; i < 4; i++) {
		switch (codingType >> 14) {
		case 0: // MOT: Skip block
			//printf("coding type 0\n");
			break;
		case 1: { // FCC: Copy an existing block
			//printf("coding type 1\n");
			byte argument;
			argument = _file->readByte();
			//byte Dx = Mx + (argument >> 4);
			//byte Dy = My + (argument & 0x0F);
			// Dx = X + 8 - (argument >> 4) - Mx
			// Dy = Y + 8 - (argument & 0x0F) - My
			break;
		}
		case 2: { // SLD: Quad vector quantisation
			//printf("coding type 2\n");
			byte argument = _file->readByte();
			if (argument > _num2blocks) {
				//error("invalid 2x2 block: %d of %d", argument, _num2blocks);
			}
			break;
		}
		case 3:
			//printf("coding type 3\n");
			_file->readByte();
			_file->readByte();
			_file->readByte();
			_file->readByte();
			break;
		}
		codingType <<= 2;
	}
	return true;
}

bool ROQPlayer::processBlockStill(ROQBlockHeader &blockHeader) {
	debugC(5, kGroovieDebugVideo | kGroovieDebugAll, "Groovie::ROQ: Processing still (JPEG) block");
	//Common::ReadStream *jpegData = new Common::SubReadStream(_file, blockHeader.size);
	//Graphics::JPEG jpegFrame;
	//jpegFrame.read(jpegData);
	/*
	Common::File save;
	save.open("dump.jpg", Common::File::kFileWriteMode);
	save.write(data, blockHeader.size);
	save.close();
	*/
	error("JPEG!");
	return true;
}

bool ROQPlayer::processBlockSoundMono(ROQBlockHeader &blockHeader) {
	debugC(5, kGroovieDebugVideo | kGroovieDebugAll, "Groovie::ROQ: Processing mono sound block");

	// Verify the block header
	if (blockHeader.type != 0x1020) {
		return false;
	}

	// Initialize the audio stream if needed
	if (!_audioStream) {
		byte flags = Audio::Mixer::FLAG_16BITS | Audio::Mixer::FLAG_AUTOFREE;
#ifdef SCUMM_LITTLE_ENDIAN
		flags |= Audio::Mixer::FLAG_LITTLE_ENDIAN;
#endif
		_audioStream = Audio::makeAppendableAudioStream(22050, flags);
		Audio::SoundHandle sound_handle;
		::g_engine->_mixer->playInputStream(Audio::Mixer::kPlainSoundType, &sound_handle, _audioStream);
	}

	// Create the audio buffer
	int16 *buffer = new int16[blockHeader.size];

	// Initialize the prediction with the block parameter
	int16 prediction = blockHeader.param ^ 0x8000;

	// Process the data
	for (uint16 i = 0; i < blockHeader.size; i++) {
		int16 data = _file->readByte();
		if (data < 0x80) {
			prediction += data * data;
		} else {
			data -= 0x80;
			prediction -= data * data;
		}
		buffer[i] = prediction;
	}

	// Queue the read buffer
	_audioStream->queueBuffer((byte *)buffer, blockHeader.size * 2);

	return true;
}

bool ROQPlayer::processBlockSoundStereo(ROQBlockHeader &blockHeader) {
	debugC(5, kGroovieDebugVideo | kGroovieDebugAll, "Groovie::ROQ: Processing stereo sound block");

	// Verify the block header
	if (blockHeader.type != 0x1021) {
		return false;
	}

	// Initialize the audio stream if needed
	if (!_audioStream) {
		byte flags = Audio::Mixer::FLAG_16BITS | Audio::Mixer::FLAG_AUTOFREE | Audio::Mixer::FLAG_STEREO;
#ifdef SCUMM_LITTLE_ENDIAN
		flags |= Audio::Mixer::FLAG_LITTLE_ENDIAN;
#endif
		_audioStream = Audio::makeAppendableAudioStream(22050, flags);
		Audio::SoundHandle sound_handle;
		::g_engine->_mixer->playInputStream(Audio::Mixer::kPlainSoundType, &sound_handle, _audioStream);
	}

	// Create the audio buffer
	int16 *buffer = new int16[blockHeader.size];

	// Initialize the prediction with the block parameter
	int16 predictionLeft = (blockHeader.param & 0xFF00) ^ 0x8000;
	int16 predictionRight = (blockHeader.param << 8) ^ 0x8000;
	bool left = true;

	// Process the data
	for (uint16 i = 0; i < blockHeader.size; i++) {
		int16 data = _file->readByte();
		if (left) {
			if (data < 0x80) {
				predictionLeft += data * data;
			} else {
				data -= 0x80;
				predictionLeft -= data * data;
			}
			buffer[i] = predictionLeft;
		} else {
			if (data < 0x80) {
				predictionRight += data * data;
			} else {
				data -= 0x80;
				predictionRight -= data * data;
			}
			buffer[i] = predictionRight;
		}
		left = !left;
	}

	// Queue the read buffer
	_audioStream->queueBuffer((byte *)buffer, blockHeader.size * 2);

	return true;
}

bool ROQPlayer::processBlockAudioContainer(ROQBlockHeader &blockHeader) {
	debugC(5, kGroovieDebugVideo | kGroovieDebugAll, "Groovie::ROQ: Processing audio container block: 0x%04X", blockHeader.param);
	return true;
}

} // End of Groovie namespace