File: paco_decoder.cpp

package info (click to toggle)
scummvm 2.7.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 363,784 kB
  • sloc: cpp: 3,622,060; asm: 27,410; python: 10,528; sh: 10,241; xml: 6,752; java: 5,579; perl: 2,570; yacc: 1,635; javascript: 1,016; lex: 539; makefile: 398; ansic: 378; awk: 275; objc: 82; sed: 11; php: 1
file content (599 lines) | stat: -rw-r--r-- 15,906 bytes parent folder | download
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "common/endian.h"
#include "common/rect.h"
#include "common/stream.h"
#include "common/system.h"
#include "common/textconsole.h"
#include "graphics/surface.h"
#include "video/qt_data.h"
#include "video/paco_decoder.h"


#include "audio/audiostream.h"
#include "audio/decoders/raw.h"

namespace Video {

enum frameTypes {
	NOP = 0, // nop
	// 1 - old initialisation data?
	PALLETE = 2, // - new initialisation data (usually 0x30 0x00 0x00 ... meaning 8-bit with default QuickTime palette)
	DELAY = 3, //  - delay information
	AUDIO = 4, // - audio data (8-bit unsigned PCM)
	// 5 - should not be present
	// 6 - should not be present
	// 7 - unknown
	VIDEO = 8, // - video frame
	// 9 - unknown
	// 10 - dummy?
	EOC = 11 // - end of chunk marker
};

PacoDecoder::PacoDecoder()
	: _fileStream(nullptr), _videoTrack(nullptr), _audioTrack(nullptr) {
}

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

void PacoDecoder::PacoDecoder::close() {
	Video::VideoDecoder::close();

	delete _fileStream;
	_fileStream = nullptr;
	_videoTrack = nullptr;
	_audioTrack = nullptr;
}

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

	_curFrame = 0;
	stream->readUint16BE(); // 1
	stream->readUint16BE(); // 0x26

	uint16 width = stream->readUint16BE();
	uint16 height = stream->readUint16BE();
	int16 frameRate = stream->readUint16BE();
	frameRate = ABS(frameRate); // Negative framerate is indicative of audio, but not always
	uint16 flags = stream->readUint16BE();
	bool hasAudio = (flags & 0x100) == 0x100;

	stream->readUint32BE(); // maxChunksize
	stream->readUint32BE(); // always 0
	stream->readUint32BE(); // audio related flags
	uint16 frameCount = stream->readUint16BE();
	stream->readUint16BE(); // copy of frameCount
	stream->readUint16BE(); // always 8?
	stream->readUint16BE(); // always 0x600?
	stream->readUint32BE(); // flags
	stream->readUint16BE(); // 0

	for (uint i = 0; i < frameCount; i++) {
		_frameSizes[i] = stream->readUint32BE();
	}

	_fileStream = stream;

	_videoTrack = new PacoVideoTrack(frameRate, frameCount, width, height);
	addTrack(_videoTrack);
	if (hasAudio) {
		_audioTrack = new PacoAudioTrack(getAudioSamplingRate());
		addTrack(_audioTrack);
	}
	return true;
}

int PacoDecoder::getAudioSamplingRate() {
	/**
	 * SamplingRate is found inside the audio packets
	 * Search for the first audio packet and use it.
	 */
	const Common::Array<int> samplingRates = {5563, 7418, 11127, 22254};
	int index;

	int64 startPos = _fileStream->pos();

	while (true){
		int64 currentPos = _fileStream->pos();
		int frameType = _fileStream->readByte();
		int v = _fileStream->readByte();
		uint32 chunkSize =  (v << 16 ) | _fileStream->readUint16BE();
		if (frameType != AUDIO) {
			_fileStream->seek(currentPos + chunkSize);
			continue;
		}
		uint16 header = _fileStream->readUint16BE();
		_fileStream->readUint16BE();
		index = (header >> 10) & 7;
		break;
	}
	_fileStream->seek(startPos);

	return samplingRates[index];
}

const Common::List<Common::Rect> *PacoDecoder::getDirtyRects() const {
	const Track *track = getTrack(0);

	if (track)
		return ((const PacoVideoTrack *)track)->getDirtyRects();

	return 0;
}

void PacoDecoder::clearDirtyRects() {
	Track *track = getTrack(0);

	if (track)
		((PacoVideoTrack *)track)->clearDirtyRects();
}

void PacoDecoder::copyDirtyRectsToBuffer(uint8 *dst, uint pitch) {
	Track *track = getTrack(0);

	if (track)
		((PacoVideoTrack *)track)->copyDirtyRectsToBuffer(dst, pitch);
}

const byte* PacoDecoder::getPalette(){
	Track *track = getTrack(0);

	if (track)
		return ((PacoVideoTrack *)track)->getPalette();
	return nullptr;
}

const byte* PacoDecoder::PacoVideoTrack::getPalette() const {
	_dirtyPalette = false;
	return _palette;
}

PacoDecoder::PacoVideoTrack::PacoVideoTrack(
	uint16 frameRate, uint16 frameCount, uint16 width, uint16 height) {
	_curFrame = 0;
	_frameRate = frameRate;
	_frameCount = frameCount;

	_surface = new Graphics::Surface();
	_surface->create(width, height, Graphics::PixelFormat::createFormatCLUT8());
	_palette = const_cast<byte *>(quickTimeDefaultPalette256);
	_dirtyPalette = true;
}

PacoDecoder::PacoVideoTrack::~PacoVideoTrack() {
	_surface->free();
	delete _surface;
}

bool PacoDecoder::PacoVideoTrack::endOfTrack() const {
	return getCurFrame() >= getFrameCount();
}

uint16 PacoDecoder::PacoVideoTrack::getWidth() const {
	return _surface->w;
}

uint16 PacoDecoder::PacoVideoTrack::getHeight() const {
	return _surface->h;
}

Graphics::PixelFormat PacoDecoder::PacoVideoTrack::getPixelFormat() const {
	return _surface->format;
}

void PacoDecoder::readNextPacket() {
	uint32 nextFrame = _fileStream->pos() + _frameSizes[_curFrame];

	debug(2, " frame %3d size %d @ %lX", _curFrame, _frameSizes[_curFrame], long(_fileStream->pos()));

	_curFrame++;

	while (_fileStream->pos() < nextFrame) {
		int64 currentPos = _fileStream->pos();
		int frameType = _fileStream->readByte();
		int v = _fileStream->readByte();
		uint32 chunkSize =  (v << 16 ) | _fileStream->readUint16BE();
		debug(2, "  slot type %d size %d @ %lX", frameType, chunkSize, long(_fileStream->pos() - 4));

		switch (frameType) {
		case AUDIO:
			_audioTrack->queueSound(_fileStream, chunkSize - 4);
			break;
		case VIDEO:
			_videoTrack->handleFrame(_fileStream, chunkSize - 4, _curFrame);
			break;
		case PALLETE:
			_videoTrack->handlePalette(_fileStream);
			break;
		case EOC:
			break;
		default:
			error("PacoDecoder::decodeFrame(): unknown main chunk type (type = 0x%02X)", frameType);
			break;
		}
		_fileStream->seek(currentPos + chunkSize);
	 }


}

const Graphics::Surface *PacoDecoder::PacoVideoTrack::decodeNextFrame() {
	return _surface;
}

void PacoDecoder::PacoVideoTrack::handlePalette(Common::SeekableReadStream *fileStream) {
	uint32 header = fileStream->readUint32BE();
	if (header == 0x30000000) { // default quicktime palette
		_palette = const_cast<byte *>(quickTimeDefaultPalette256);
	} else {
		fileStream->readUint32BE(); // 4 bytes of 00
		_palette = new byte[256 * 3]();
		for (int i = 0; i < 256 * 3; i++){
			_palette[i] = fileStream->readByte();
		}
	}
	_dirtyPalette = true;
}

enum {
	COPY = 0,		// raw copy pixels
	RLE, 	 		// RLE
	PRLE,			// pair RLE (read a pair of pixels and repeat it the specified number of times)
	QRLE, 			// quad RLE (read four pixels and repeat it the specified number of times)
	SKIP, 			// skip
	ENDCURRENTLINE,	// end current line and skip additional len lines
	EOFRAME = 15 	// not real opcode but 00 F0 00 00 is often seen at the end of frame and can serve as an end marker
};

#define ADJUST_LINE \
	if (compr == 1) \
		ypos2 = ypos; \
	else { \
		ypos2 = ypos * 2 - y; \
		if (ypos2 >= y + bh) { \
			ypos2 -= bh; \
			if (!(bh & 1)) \
				ypos2++; \
		} \
	}

#define PUTPIX(pix) \
	do { \
		*dst++ = pix; \
		xpos++; \
	} while(0);

#define SKIP() \
	do { \
		dst++; \
		xpos++; \
	} while(0);


void PacoDecoder::PacoVideoTrack::handleFrame(Common::SeekableReadStream *fileStream, uint32 chunkSize, int curFrame) {
	_curFrame = curFrame;
	uint16 w = getWidth();

	uint16 x = fileStream->readUint16BE();			// x offset of the updated area
	uint16 y = fileStream->readUint16BE();			// y offset of the updated area
	uint16 bw = fileStream->readUint16BE();			// updated area width
	uint16 bh = fileStream->readUint16BE();			// updated area height
	uint compr = fileStream->readByte();	    	// compression method and flags
	fileStream->readByte();							// padding

	debug(5, "    +%d,%d - %dx%d compr %X", x, y, bw, bh, compr);

	compr = compr & 0xF;

	uint8 *fdata = new uint8[1048576];              // 0x100000 copied from original pacodec
	fileStream->read(fdata, chunkSize - 10);       // remove header length
	debug(5, "pos: %ld", long(fileStream->pos()));
	int16 xpos = x, ypos = y, ypos2 = y;
	byte *dst = (byte *)_surface->getPixels() + x + y * w;

	const uint8 *src = fdata;
	int16 i, c, c1, c2, c3, c4;
	uint8 clrs[16];


	while (ypos < y + bh) {
		c = *src++;
		debug(5, "debug info: ypos %d y %d bh %d src: %d", ypos, y, bh, c);

		if (c == 0 ){ // long operation
			int16 op = src[0] >> 4;
			int16 len = ((src[0] & 0xF) << 8) | src[1];
			src += 2;
			debug(5, "    long operation: opcode: %d", op);
			switch (op) {
			case COPY:
				while (len--)
					PUTPIX(*src++);
				break;
			case RLE:
				c1 = *src++;
				while (len--)
					PUTPIX(c1);
				break;
			case PRLE:
				c1 = *src++;
				c2 = *src++;
				while (len--){
					PUTPIX(c1);
					PUTPIX(c2);
				}
				break;
			case QRLE:
				c1 = *src++;
				c2 = *src++;
				c3 = *src++;
				c4 = *src++;
				while (len--) {
					PUTPIX(c1);
					PUTPIX(c2);
					PUTPIX(c3);
					PUTPIX(c4);
				}
				break;
			case SKIP:
				while (len--)
					SKIP();
				break;
			case ENDCURRENTLINE:
				xpos = x;
				ypos += len + 1;
				ADJUST_LINE;
				dst = (byte *)_surface->getPixels() + xpos + ypos2 * w;
				break;
			case EOFRAME:
				xpos = x;
				ypos = y + bh;
				break;
			default:
				PUTPIX(0xFF);
				debug(5, "PacoDecoder::PacoVideoTrack::handleFrame: Long op: 0x0 op %d", op);
			}

		} else if (c < 128) { // copy the same amount of pixels
			debug(5, "    copy pixels: %d", c);
			while (c--)
				PUTPIX(*src++);
		} else if (c < 254) { // repeat the following value 256 - op times
			debug(5, "    copy pixels -op: %d", 256 - c);
			c1 = *src++;
			c = 256 - c;
			while (c--)
				PUTPIX(c1);
		} else if (c < 255) {
			// next byte is either the number of pixels to skip (if non-zero) or
			// a signal of compact RLE mode
			c = *src++;

			if (!c) {                                   // compact RLE mode
				unsigned mask = (src[0] << 8) | src[1];
				src += 2;
				debug(5, "debug info compact RLE: c: %d mask: %d", c, mask);

				for (i = 0; i < 16; i++, mask >>= 1) {
					if (mask & 1)
						clrs[i] = *src++;
				}
				while (xpos < x + bw) {
					int16 op = *src++;
					int16 len = op & 0xF;
					op >>= 4;
					if (op == 0) {                      // low nibble....
						op = len;
						len = *src++;
						debug(5, "debug info compact: op: %d", op);
						switch (op) {
						case COPY:
							debug(5, "debug info COPY: %d", len);
							while (len--) {
								c = *src++;
								PUTPIX(clrs[c >> 4]);
								if (!len)
									break;
								len--;
								PUTPIX(clrs[c & 0xF]);
							}
							break;
						case RLE:
							debug(5, "debug info RLE: %d", len);
							c = *src++;
							while (len--)
								PUTPIX(clrs[c & 0xF]);
							break;
						case PRLE:
							debug(5, "debug info PRLE: %d", len);
							c = *src++;
							c1 = clrs[c >> 4];
							c2 = clrs[c & 0xF];
							while (len--) {
								PUTPIX(c1);
								PUTPIX(c2);
							}
							break;
						case QRLE:
							debug(5, "debug info QRLE: %d", len);
							c = *src++;
							c1 = clrs[c >> 4];
							c2 = clrs[c & 0xF];
							c = *src++;
							c3 = clrs[c >> 4];
							c4 = clrs[c & 0xF];
							while (len--) {
								PUTPIX(c1);
								PUTPIX(c2);
								PUTPIX(c3);
								PUTPIX(c4);
							}
							break;
						case SKIP:
							debug(5, "debug info SKIP: %d", len);
							while (len--)
								SKIP();
							break;
						case ENDCURRENTLINE:
							debug(5, "debug info ENDCURRENTLINE: %d", len);
							xpos = x + bw;
							ypos += len;
							break;
						default:
							warning("PacoDecoder::PacoVideoTrack::handleFrame: Compact RLE mode: 0x0 op %d", op);
						}
					} else if (op < 8) {                // copy 1-7 colors
						debug(5, "debug info copy 1-7 colors: %d", len);
						PUTPIX(clrs[len]);
						op--;
						while (op--) {
							c = *src++;
							PUTPIX(clrs[c >> 4]);
							if (!op)
								break;
							op--;
							PUTPIX(clrs[c & 0xF]);
						}
					} else if (op < 14) {              // repeat color
						debug(5, "debug info Repeat color: %d", len);
						op = 16 - op;
						while (op--)
							PUTPIX(clrs[len]);
					} else if (op < 15) {               // skip number of pixels in low nibbel
						debug(5, "debug info Skip number of pixels: %d", len);
						while (len--)
							SKIP();
					} else {
						if (len < 8) {                  // Pair run
							debug(5, "debug info pair run: %d", len);
							c = *src++;
							c1 = clrs[c >> 4];
							c2 = clrs[c & 0xF];
							while (len--) {
								PUTPIX(c1);
								PUTPIX(c2);
							}
						} else {                        // Quad run
							debug(5, "debug info quad run: %d", len);
							len = 16 - len;
							c = *src++;
							c1 = clrs[c >> 4];
							c2 = clrs[c & 0xF];
							c = *src++;
							c3 = clrs[c >> 4];
							c4 = clrs[c & 0xF];
							while (len--) {
								PUTPIX(c1);
								PUTPIX(c2);
								PUTPIX(c3);
								PUTPIX(c4);
							}
						}
					}
				}
			} else {
				debug(5, "debug info SKIP: %d", c);
				while (c--)
					SKIP();
			}
		} else {
			// pair or quad run. Read the next byte and if it is below 128 then read and
			// repeat a pair of pixels len times, otherwise read and repeat four pixels
			// (but 256 - len times)
			c = *src++;
			if (c < 128) {                             // pair run
				debug(5, "debug info PAIR RUN: %d", c);

				c1 = *src++;
				c2 = *src++;
				while (c--) {
					PUTPIX(c1);
					PUTPIX(c2);
				}
			} else {                                    // quad run
				debug(5, "debug info QUAD RUN: %d", c);
				c = 256 - c;
				c1 = *src++;
				c2 = *src++;
				c3 = *src++;
				c4 = *src++;
				while (c--) {
					PUTPIX(c1);
					PUTPIX(c2);
					PUTPIX(c3);
					PUTPIX(c4);
				}
			}
		}
		if (xpos > x + bw) debug(5, "!!!");
		if (xpos >= x + bw) {
			debug(5, "debug info ADJUST LINE");
			xpos = x;
			ypos++;
			ADJUST_LINE;
			dst = (byte *)_surface->getPixels() + x + ypos2 * w;
		}
	}

	_dirtyRects.clear();
	_dirtyRects.push_back(Common::Rect(x, y, x + bw, y + bh));
	delete[] fdata;
}

void PacoDecoder::PacoVideoTrack::copyDirtyRectsToBuffer(uint8 *dst, uint pitch) {
	for (Common::List<Common::Rect>::const_iterator it = _dirtyRects.begin(); it != _dirtyRects.end(); ++it) {
		for (int y = (*it).top; y < (*it).bottom; ++y) {
			const int x = (*it).left;
			memcpy(dst + y * pitch + x, (byte *)_surface->getBasePtr(x, y), (*it).right - x);
		}
	}
	clearDirtyRects();
}

PacoDecoder::PacoAudioTrack::PacoAudioTrack(int samplingRate)
	: AudioTrack(Audio::Mixer::kPlainSoundType) {
	_samplingRate = samplingRate;
	byte audioFlags = Audio::FLAG_UNSIGNED;
	_packetStream = Audio::makePacketizedRawStream(samplingRate, audioFlags);
}

PacoDecoder::PacoAudioTrack::~PacoAudioTrack() {
	delete _packetStream;
}

void PacoDecoder::PacoAudioTrack::queueSound(Common::SeekableReadStream *fileStream, uint32 chunkSize) {
	const Common::Array<int> samplingRates = {5563, 7418, 11127, 22254};
	uint16 header = fileStream->readUint16BE();
	fileStream->readUint16BE();
	int index = (header >> 10) & 7;
	int currentRate = samplingRates[index];
	if ( currentRate != _samplingRate)
		warning("PacoDecoder::PacoAudioTrack: Sampling rate differs from first frame: %i != %i", currentRate, _samplingRate);

	_packetStream->queuePacket(fileStream->readStream(chunkSize - 4));
}


} // End of namespace Video