File: vqa_decoder.h

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (304 lines) | stat: -rw-r--r-- 9,109 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
/* 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/>.
 *
 */

#ifndef BLADERUNNER_VQA_DECODER_H
#define BLADERUNNER_VQA_DECODER_H

#include "bladerunner/adpcm_decoder.h"

#include "audio/audiostream.h"

#include "common/debug.h"
#include "common/str.h"
#include "common/stream.h"
#include "common/types.h"

#include "graphics/surface.h"

#include "common/array.h"
#include "common/rational.h"

namespace BladeRunner {

class Lights;
class ScreenEffects;
class View;
class ZBuffer;

enum VQADecoderSkipFlags {
	kVQAReadCodebook           = 1,
	kVQAReadVectorPointerTable = 2,
	kVQAReadCustom             = 4,
	kVQAReadVideo              = kVQAReadCodebook|kVQAReadVectorPointerTable|kVQAReadCustom,
	kVQAReadAudio              = 8,
	kVQAReadAll                = kVQAReadVideo|kVQAReadAudio
};

class VQADecoder {
	friend class Debugger;

	struct VQPPalette {
		uint8 interpol2D[256][256];
	};

public:
	VQADecoder();
	~VQADecoder();

	bool loadStream(Common::SeekableReadStream *s);

	void readFrame(int frame, uint readFlags = kVQAReadAll);

	void                        decodeVideoFrame(Graphics::Surface *surface, int frame, bool forceDraw = false);
	void                        decodeZBuffer(ZBuffer *zbuffer);
	Audio::SeekableAudioStream *decodeAudioFrame();
	void                        decodeView(View *view);
	void                        decodeScreenEffects(ScreenEffects *aesc);
	void                        decodeLights(Lights *lights);

	uint16 numFrames() const { return _header.numFrames; }
	uint8  frameRate() const { return _header.frameRate; }

	uint16 offsetX() const { return _header.offsetX; }
	uint16 offsetY() const { return _header.offsetY; }

	void overrideOffsetXY(uint16 offX, uint16 offY);

	bool   hasAudio() const { return _header.channels != 0; }
	uint16 frequency() const { return _header.freq; }

	bool getLoopBeginAndEndFrame(int loopId, int *begin, int *end);
	int  getLoopIdFromFrame(int frame);

	void allocatePaletteVQPTable(const uint32 numOfPalettes);
	void updatePaletteVQPTable(uint32 palId, uint16 j, uint16 k, uint8 colorByte);
	void deleteVQPTable();

	struct Header {
		uint16 version;     // 0x00
		uint16 flags;       // 0x02
		uint16 numFrames;   // 0x04
		uint16 width;       // 0x06
		uint16 height;      // 0x08
		uint8  blockW;      // 0x0A
		uint8  blockH;      // 0x0B
		uint8  frameRate;   // 0x0C
		uint8  cbParts;     // 0x0D
		uint16 colors;      // 0x0E
		uint16 maxBlocks;   // 0x10
		uint16 offsetX;     // 0x12
		uint16 offsetY;     // 0x14
		uint16 maxVPTRSize; // 0x16
		uint16 freq;        // 0x18
		uint8  channels;    // 0x1A
		uint8  bits;        // 0x1B
		uint32 unk3;        // 0x1C
		uint16 unk4;        // 0x20
		uint32 maxCBFZSize; // 0x22
		uint32 unk5;        // 0x26
		                    // 0x2A
	};

	struct Loop {
		uint16         begin;
		uint16         end;
		Common::String name;

		Loop() :
			begin(0),
			end(0)
		{}
	};

	struct LoopInfo {
		uint16  loopCount;
		uint32  flags;
		Loop   *loops;

		LoopInfo() : loopCount(0), loops(nullptr), flags(0) {}
		~LoopInfo() {
			delete[] loops;
		}
	};

	struct CodebookInfo {
		uint16  frame;
		uint32  size;
		uint8  *data;
	};

	class VQAVideoTrack;
	class VQAAudioTrack;

	Common::SeekableReadStream *_s;

	Header   _header;
	int      _readingFrame;
	int      _decodingFrame;
	LoopInfo _loopInfo;

	VQPPalette *_vqpPalsArr;
	uint16      _numOfVQPPalettes;

	bool        _oldV2VQA;
	// TODO Maybe add public methods for setting these member vars (essentially flags)?
	bool        _allowHorizontalScanlines;
	bool        _allowVerticalScanlines;
	bool        _scaleVideoTo2xRequested;
	bool        _scale2xPossible;
	bool        _centerVideoRequested;
	Common::Array<CodebookInfo> _codebooks;

	uint32  *_frameInfo;

	uint32   _maxVIEWChunkSize;
	uint32   _maxZBUFChunkSize;
	uint32   _maxAESCChunkSize;

	VQAVideoTrack *_videoTrack;
	VQAAudioTrack *_audioTrack;

	void readPacket(uint readFlags);

	bool readVQHD(Common::SeekableReadStream *s, uint32 size);
	bool readMSCI(Common::SeekableReadStream *s, uint32 size);
	bool readMFCI(Common::SeekableReadStream *s, uint32 size);
	bool readLINF(Common::SeekableReadStream *s, uint32 size);
	bool readCINF(Common::SeekableReadStream *s, uint32 size);
	bool readFINF(Common::SeekableReadStream *s, uint32 size);
	bool readLNIN(Common::SeekableReadStream *s, uint32 size);
	bool readCLIP(Common::SeekableReadStream *s, uint32 size);

	CodebookInfo &codebookInfoForFrame(int frame);

	class VQAVideoTrack {
		static const uint     kSizeInBytesOfCPL0Chunk = 768; // 3 * 256
	public:
		VQAVideoTrack(VQADecoder *vqaDecoder);
		~VQAVideoTrack();

		uint16 getWidth() const;
		uint16 getHeight() const;

		int getFrameCount() const;

		void overrideOffsetXY(uint16 offX, uint16 offY);

		void decodeVideoFrame(Graphics::Surface *surface, bool forceDraw);
		void decodeZBuffer(ZBuffer *zbuffer);
		void decodeView(View *view);
		void decodeScreenEffects(ScreenEffects *aesc);
		void decodeLights(Lights *lights);

		bool readVQFR(Common::SeekableReadStream *s, uint32 size, uint readFlags);
		bool readVPTZ(Common::SeekableReadStream* s, uint32 size); // (for old type v2 VQA, eg. SIZZLE.VQAs)
		bool readVPTR(Common::SeekableReadStream *s, uint32 size);
		bool readVQFL(Common::SeekableReadStream *s, uint32 size, uint readFlags);
		bool readCBFZ(Common::SeekableReadStream *s, uint32 size);
		bool readCBPZ(Common::SeekableReadStream* s, uint32 size); // (for old type v2 VQA, eg. SIZZLE.VQAs)
		bool readZBUF(Common::SeekableReadStream *s, uint32 size);
		bool readVIEW(Common::SeekableReadStream *s, uint32 size);
		bool readAESC(Common::SeekableReadStream *s, uint32 size);
		bool readLITE(Common::SeekableReadStream *s, uint32 size);
		bool readCPL0(Common::SeekableReadStream *s, uint32 size); // (for old type v2 VQA, eg. SIZZLE.VQAs)

	protected:
		Common::Rational getFrameRate() const;

		bool useAudioSync() const { return false; }

	private:
		VQADecoder        *_vqaDecoder;

		bool _hasNewFrame;

		uint16 _numFrames;
		uint16 _width, _height;
		uint8  _blockW, _blockH;
		uint8  _frameRate;
		uint8  _cbParts;
		uint16 _maxBlocks;
		uint16 _offsetX, _offsetY;

		uint16  _maxVPTRSize;
		uint32  _maxCBFZSize;
		uint32  _maxZBUFChunkSize;

		uint8   *_codebook;
		uint8   *_cbfz;
		uint32   _zbufChunkSize;
		uint8   *_zbufChunk;

		uint32   _vpointerSize;
		uint8   *_vpointer;

		int      _curFrame;

		uint8   *_viewData;
		uint32   _viewDataSize;
		uint8   *_lightsData;
		uint32   _lightsDataSize;
		uint8   *_screenEffectsData;
		uint32   _screenEffectsDataSize;

		int32          _currentPaletteId;
		uint8         *_cpalPointer;
		uint8         *_cpalPointerNext;
		uint32	       _cpalPointerSize;
		uint32	       _cpalPointerSizeNext;
		uint8         *_vptz;
		uint8         *_cbfzNext;         // Used to store of compressed parts of a codebook data
		uint8          _countOfCBPsToCBF;
		uint32         _accumulatedCBPZsizeToCBF;

		CodebookInfo  *_codebookInfoNext; // Used to store the decompressed codebook data and swap with the active codebook

		void VPTRWriteBlock(Graphics::Surface *surface, unsigned int dstBlock, unsigned int srcBlock, int count, bool alpha = false);
		bool decodeFrame(Graphics::Surface *surface);
	};

	class VQAAudioTrack {
		static const uint     kSizeInShortsAllocatedToAudioFrame = 2940; // 4 * 735
		static const uint     kSizeInBytesOfCompressedAudioFrame = 735;
		static const uint     kSizeInShortsAllocatedToAudioFrameMax = 22048; // 4 * 5512 (for old type v2 VQA, eg. SIZZLE.VQAs)
		static const uint     kSizeInBytesOfCompressedAudioFrameMax = 5512; // (for old type v2 VQA, eg. SIZZLE.VQAs)
	public:
		VQAAudioTrack(VQADecoder *vqaDecoder);
		~VQAAudioTrack();

		bool readSND2(Common::SeekableReadStream *s, uint32 size);
		bool readSN2J(Common::SeekableReadStream *s, uint32 size);

		Audio::SeekableAudioStream *decodeAudioFrame();
	protected:

	private:
		uint16               _frequency;
		ADPCMWestwoodDecoder _adpcmDecoder;
		bool                 _bigCompressedAudioFrame;
		uint8                _compressedAudioFrame[kSizeInBytesOfCompressedAudioFrameMax];
//		uint8                _compressedAudioFrame[kSizeInBytesOfCompressedAudioFrame + 1]; // +1 because we use roundup for read-in and 735 is odd
	};
};

} // End of namespace BladeRunner

#endif