1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef BLADERUNNER_VQA_DECODER_H
24 #define BLADERUNNER_VQA_DECODER_H
25 
26 #include "bladerunner/adpcm_decoder.h"
27 
28 #include "audio/audiostream.h"
29 
30 #include "common/debug.h"
31 #include "common/str.h"
32 #include "common/stream.h"
33 #include "common/types.h"
34 
35 #include "graphics/surface.h"
36 
37 #include "common/array.h"
38 #include "common/rational.h"
39 
40 namespace BladeRunner {
41 
42 class Lights;
43 class ScreenEffects;
44 class View;
45 class ZBuffer;
46 
47 enum VQADecoderSkipFlags {
48 	kVQAReadCodebook           = 1,
49 	kVQAReadVectorPointerTable = 2,
50 	kVQAReadCustom             = 4,
51 	kVQAReadVideo              = kVQAReadCodebook|kVQAReadVectorPointerTable|kVQAReadCustom,
52 	kVQAReadAudio              = 8,
53 	kVQAReadAll                = kVQAReadVideo|kVQAReadAudio
54 };
55 
56 class VQADecoder {
57 	friend class Debugger;
58 
59 public:
60 	VQADecoder();
61 	~VQADecoder();
62 
63 	bool loadStream(Common::SeekableReadStream *s);
64 
65 	void readFrame(int frame, uint readFlags = kVQAReadAll);
66 
67 	void                        decodeVideoFrame(Graphics::Surface *surface, int frame, bool forceDraw = false);
68 	void                        decodeZBuffer(ZBuffer *zbuffer);
69 	Audio::SeekableAudioStream *decodeAudioFrame();
70 	void                        decodeView(View *view);
71 	void                        decodeScreenEffects(ScreenEffects *aesc);
72 	void                        decodeLights(Lights *lights);
73 
numFrames()74 	uint16 numFrames() const { return _header.numFrames; }
frameRate()75 	uint8  frameRate() const { return _header.frameRate; }
76 
offsetX()77 	uint16 offsetX() const { return _header.offsetX; }
offsetY()78 	uint16 offsetY() const { return _header.offsetY; }
79 
hasAudio()80 	bool   hasAudio() const { return _header.channels != 0; }
frequency()81 	uint16 frequency() const { return _header.freq; }
82 
83 	bool getLoopBeginAndEndFrame(int loop, int *begin, int *end);
84 	int  getLoopIdFromFrame(int frame);
85 
86 	struct Header {
87 		uint16 version;     // 0x00
88 		uint16 flags;       // 0x02
89 		uint16 numFrames;   // 0x04
90 		uint16 width;       // 0x06
91 		uint16 height;      // 0x08
92 		uint8  blockW;      // 0x0A
93 		uint8  blockH;      // 0x0B
94 		uint8  frameRate;   // 0x0C
95 		uint8  cbParts;     // 0x0D
96 		uint16 colors;      // 0x0E
97 		uint16 maxBlocks;   // 0x10
98 		uint16 offsetX;     // 0x12
99 		uint16 offsetY;     // 0x14
100 		uint16 maxVPTRSize; // 0x16
101 		uint16 freq;        // 0x18
102 		uint8  channels;    // 0x1A
103 		uint8  bits;        // 0x1B
104 		uint32 unk3;        // 0x1C
105 		uint16 unk4;        // 0x20
106 		uint32 maxCBFZSize; // 0x22
107 		uint32 unk5;        // 0x26
108 		                    // 0x2A
109 	};
110 
111 	struct Loop {
112 		uint16         begin;
113 		uint16         end;
114 		Common::String name;
115 
LoopLoop116 		Loop() :
117 			begin(0),
118 			end(0)
119 		{}
120 	};
121 
122 	struct LoopInfo {
123 		uint16  loopCount;
124 		uint32  flags;
125 		Loop   *loops;
126 
LoopInfoLoopInfo127 		LoopInfo() : loopCount(0), loops(nullptr), flags(0) {}
~LoopInfoLoopInfo128 		~LoopInfo() {
129 			delete[] loops;
130 		}
131 	};
132 
133 	struct CodebookInfo {
134 		uint16  frame;
135 		uint32  size;
136 		uint8  *data;
137 	};
138 
139 	class VQAVideoTrack;
140 	class VQAAudioTrack;
141 
142 	Common::SeekableReadStream *_s;
143 
144 	Header   _header;
145 	int      _readingFrame;
146 	int      _decodingFrame;
147 	LoopInfo _loopInfo;
148 
149 	Common::Array<CodebookInfo> _codebooks;
150 
151 	uint32  *_frameInfo;
152 
153 	uint32   _maxVIEWChunkSize;
154 	uint32   _maxZBUFChunkSize;
155 	uint32   _maxAESCChunkSize;
156 
157 	VQAVideoTrack *_videoTrack;
158 	VQAAudioTrack *_audioTrack;
159 
160 	void readPacket(uint readFlags);
161 
162 	bool readVQHD(Common::SeekableReadStream *s, uint32 size);
163 	bool readMSCI(Common::SeekableReadStream *s, uint32 size);
164 	bool readMFCI(Common::SeekableReadStream *s, uint32 size);
165 	bool readLINF(Common::SeekableReadStream *s, uint32 size);
166 	bool readCINF(Common::SeekableReadStream *s, uint32 size);
167 	bool readFINF(Common::SeekableReadStream *s, uint32 size);
168 	bool readLNIN(Common::SeekableReadStream *s, uint32 size);
169 	bool readCLIP(Common::SeekableReadStream *s, uint32 size);
170 
171 	CodebookInfo &codebookInfoForFrame(int frame);
172 
173 	class VQAVideoTrack {
174 	public:
175 		VQAVideoTrack(VQADecoder *vqaDecoder);
176 		~VQAVideoTrack();
177 
178 		uint16 getWidth() const;
179 		uint16 getHeight() const;
180 
181 		int getFrameCount() const;
182 
183 		void decodeVideoFrame(Graphics::Surface *surface, bool forceDraw);
184 		void decodeZBuffer(ZBuffer *zbuffer);
185 		void decodeView(View *view);
186 		void decodeScreenEffects(ScreenEffects *aesc);
187 		void decodeLights(Lights *lights);
188 
189 		bool readVQFR(Common::SeekableReadStream *s, uint32 size, uint readFlags);
190 		bool readVPTR(Common::SeekableReadStream *s, uint32 size);
191 		bool readVQFL(Common::SeekableReadStream *s, uint32 size, uint readFlags);
192 		bool readCBFZ(Common::SeekableReadStream *s, uint32 size);
193 		bool readZBUF(Common::SeekableReadStream *s, uint32 size);
194 		bool readVIEW(Common::SeekableReadStream *s, uint32 size);
195 		bool readAESC(Common::SeekableReadStream *s, uint32 size);
196 		bool readLITE(Common::SeekableReadStream *s, uint32 size);
197 
198 	protected:
199 		Common::Rational getFrameRate() const;
200 
useAudioSync()201 		bool useAudioSync() const { return false; }
202 
203 	private:
204 		VQADecoder        *_vqaDecoder;
205 
206 		bool _hasNewFrame;
207 
208 		uint16 _numFrames;
209 		uint16 _width, _height;
210 		uint8  _blockW, _blockH;
211 		uint8  _frameRate;
212 		uint16 _maxBlocks;
213 		uint16 _offsetX, _offsetY;
214 
215 		uint16  _maxVPTRSize;
216 		uint32  _maxCBFZSize;
217 		uint32  _maxZBUFChunkSize;
218 
219 		uint8   *_codebook;
220 		uint8   *_cbfz;
221 		uint32   _zbufChunkSize;
222 		uint8   *_zbufChunk;
223 
224 		uint32   _vpointerSize;
225 		uint8   *_vpointer;
226 
227 		int      _curFrame;
228 
229 		uint8   *_viewData;
230 		uint32   _viewDataSize;
231 		uint8   *_lightsData;
232 		uint32   _lightsDataSize;
233 		uint8   *_screenEffectsData;
234 		uint32   _screenEffectsDataSize;
235 
236 		void VPTRWriteBlock(Graphics::Surface *surface, unsigned int dstBlock, unsigned int srcBlock, int count, bool alpha = false);
237 		bool decodeFrame(Graphics::Surface *surface);
238 	};
239 
240 	class VQAAudioTrack {
241 		static const uint     kSizeInShortsAllocatedToAudioFrame = 2940; // 4 * 735
242 		static const uint     kSizeInBytesOfCompressedAudioFrame = 735;
243 	public:
244 		VQAAudioTrack(VQADecoder *vqaDecoder);
245 		~VQAAudioTrack();
246 
247 		bool readSND2(Common::SeekableReadStream *s, uint32 size);
248 		bool readSN2J(Common::SeekableReadStream *s, uint32 size);
249 
250 		Audio::SeekableAudioStream *decodeAudioFrame();
251 	protected:
252 
253 	private:
254 		uint16               _frequency;
255 		ADPCMWestwoodDecoder _adpcmDecoder;
256 		uint8                _compressedAudioFrame[kSizeInBytesOfCompressedAudioFrame];
257 	};
258 };
259 
260 } // End of namespace BladeRunner
261 
262 #endif
263