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 "video/video_decoder.h"
38 
39 namespace BladeRunner {
40 
41 class Lights;
42 class ScreenEffects;
43 class View;
44 class ZBuffer;
45 
46 enum VQADecoderSkipFlags {
47 	kVQAReadCodebook           = 1,
48 	kVQAReadVectorPointerTable = 2,
49 	kVQAReadCustom             = 4,
50 	kVQAReadVideo              = kVQAReadCodebook|kVQAReadVectorPointerTable|kVQAReadCustom,
51 	kVQAReadAudio              = 8,
52 	kVQAReadAll                = kVQAReadVideo|kVQAReadAudio
53 };
54 
55 class VQADecoder {
56 	friend class Debugger;
57 
58 public:
59 	VQADecoder();
60 	~VQADecoder();
61 
62 	bool loadStream(Common::SeekableReadStream *s);
63 
64 	void readFrame(int frame, uint readFlags = kVQAReadAll);
65 
66 	void                        decodeVideoFrame(Graphics::Surface *surface, int frame, bool forceDraw = false);
67 	void                        decodeZBuffer(ZBuffer *zbuffer);
68 	Audio::SeekableAudioStream *decodeAudioFrame();
69 	void                        decodeView(View *view);
70 	void                        decodeScreenEffects(ScreenEffects *aesc);
71 	void                        decodeLights(Lights *lights);
72 
numFrames()73 	uint16 numFrames() const { return _header.numFrames; }
frameRate()74 	uint8  frameRate() const { return _header.frameRate; }
75 
offsetX()76 	uint16 offsetX() const { return _header.offsetX; }
offsetY()77 	uint16 offsetY() const { return _header.offsetY; }
78 
hasAudio()79 	bool   hasAudio() const { return _header.channels != 0; }
frequency()80 	uint16 frequency() const { return _header.freq; }
81 
82 	bool getLoopBeginAndEndFrame(int loop, int *begin, int *end);
83 
84 	struct Header {
85 		uint16 version;     // 0x00
86 		uint16 flags;       // 0x02
87 		uint16 numFrames;   // 0x04
88 		uint16 width;       // 0x06
89 		uint16 height;      // 0x08
90 		uint8  blockW;      // 0x0A
91 		uint8  blockH;      // 0x0B
92 		uint8  frameRate;   // 0x0C
93 		uint8  cbParts;     // 0x0D
94 		uint16 colors;      // 0x0E
95 		uint16 maxBlocks;   // 0x10
96 		uint16 offsetX;     // 0x12
97 		uint16 offsetY;     // 0x14
98 		uint16 maxVPTRSize; // 0x16
99 		uint16 freq;        // 0x18
100 		uint8  channels;    // 0x1A
101 		uint8  bits;        // 0x1B
102 		uint32 unk3;        // 0x1C
103 		uint16 unk4;        // 0x20
104 		uint32 maxCBFZSize; // 0x22
105 		uint32 unk5;        // 0x26
106 		                    // 0x2A
107 	};
108 
109 	struct Loop {
110 		uint16         begin;
111 		uint16         end;
112 		Common::String name;
113 
LoopLoop114 		Loop() :
115 			begin(0),
116 			end(0)
117 		{}
118 	};
119 
120 	struct LoopInfo {
121 		uint16  loopCount;
122 		uint32  flags;
123 		Loop   *loops;
124 
LoopInfoLoopInfo125 		LoopInfo() : loopCount(0), loops(nullptr), flags(0) {}
~LoopInfoLoopInfo126 		~LoopInfo() {
127 			delete[] loops;
128 		}
129 	};
130 
131 	struct CodebookInfo {
132 		uint16  frame;
133 		uint32  size;
134 		uint8  *data;
135 	};
136 
137 	class VQAVideoTrack;
138 	class VQAAudioTrack;
139 
140 	Common::SeekableReadStream *_s;
141 
142 	Header   _header;
143 	int      _readingFrame;
144 	int      _decodingFrame;
145 	LoopInfo _loopInfo;
146 
147 	Common::Array<CodebookInfo> _codebooks;
148 
149 	uint32  *_frameInfo;
150 
151 	uint32   _maxVIEWChunkSize;
152 	uint32   _maxZBUFChunkSize;
153 	uint32   _maxAESCChunkSize;
154 
155 	VQAVideoTrack *_videoTrack;
156 	VQAAudioTrack *_audioTrack;
157 
158 	void readPacket(uint readFlags);
159 
160 	bool readVQHD(Common::SeekableReadStream *s, uint32 size);
161 	bool readMSCI(Common::SeekableReadStream *s, uint32 size);
162 	bool readMFCI(Common::SeekableReadStream *s, uint32 size);
163 	bool readLINF(Common::SeekableReadStream *s, uint32 size);
164 	bool readCINF(Common::SeekableReadStream *s, uint32 size);
165 	bool readFINF(Common::SeekableReadStream *s, uint32 size);
166 	bool readLNIN(Common::SeekableReadStream *s, uint32 size);
167 	bool readCLIP(Common::SeekableReadStream *s, uint32 size);
168 
169 	CodebookInfo &codebookInfoForFrame(int frame);
170 
171 	class VQAVideoTrack {
172 	public:
173 		VQAVideoTrack(VQADecoder *vqaDecoder);
174 		~VQAVideoTrack();
175 
176 		uint16 getWidth() const;
177 		uint16 getHeight() const;
178 
179 		int getFrameCount() const;
180 
181 		void decodeVideoFrame(Graphics::Surface *surface, bool forceDraw);
182 		void decodeZBuffer(ZBuffer *zbuffer);
183 		void decodeView(View *view);
184 		void decodeScreenEffects(ScreenEffects *aesc);
185 		void decodeLights(Lights *lights);
186 
187 		bool readVQFR(Common::SeekableReadStream *s, uint32 size, uint readFlags);
188 		bool readVPTR(Common::SeekableReadStream *s, uint32 size);
189 		bool readVQFL(Common::SeekableReadStream *s, uint32 size, uint readFlags);
190 		bool readCBFZ(Common::SeekableReadStream *s, uint32 size);
191 		bool readZBUF(Common::SeekableReadStream *s, uint32 size);
192 		bool readVIEW(Common::SeekableReadStream *s, uint32 size);
193 		bool readAESC(Common::SeekableReadStream *s, uint32 size);
194 		bool readLITE(Common::SeekableReadStream *s, uint32 size);
195 
196 	protected:
197 		Common::Rational getFrameRate() const;
198 
useAudioSync()199 		bool useAudioSync() const { return false; }
200 
201 	private:
202 		VQADecoder        *_vqaDecoder;
203 
204 		bool _hasNewFrame;
205 
206 		uint16 _numFrames;
207 		uint16 _width, _height;
208 		uint8  _blockW, _blockH;
209 		uint8  _frameRate;
210 		uint16 _maxBlocks;
211 		uint16 _offsetX, _offsetY;
212 
213 		uint16  _maxVPTRSize;
214 		uint32  _maxCBFZSize;
215 		uint32  _maxZBUFChunkSize;
216 
217 		uint8   *_codebook;
218 		uint8   *_cbfz;
219 		uint32   _zbufChunkSize;
220 		uint8   *_zbufChunk;
221 
222 		uint32   _vpointerSize;
223 		uint8   *_vpointer;
224 
225 		int      _curFrame;
226 
227 		uint8   *_viewData;
228 		uint32   _viewDataSize;
229 		uint8   *_lightsData;
230 		uint32   _lightsDataSize;
231 		uint8   *_screenEffectsData;
232 		uint32   _screenEffectsDataSize;
233 
234 		void VPTRWriteBlock(Graphics::Surface *surface, unsigned int dstBlock, unsigned int srcBlock, int count, bool alpha = false);
235 		bool decodeFrame(Graphics::Surface *surface);
236 	};
237 
238 	class VQAAudioTrack {
239 	public:
240 		VQAAudioTrack(VQADecoder *vqaDecoder);
241 		~VQAAudioTrack();
242 
243 		bool readSND2(Common::SeekableReadStream *s, uint32 size);
244 		bool readSN2J(Common::SeekableReadStream *s, uint32 size);
245 
246 		Audio::SeekableAudioStream *decodeAudioFrame();
247 	protected:
248 
249 	private:
250 		uint16               _frequency;
251 		ADPCMWestwoodDecoder _adpcmDecoder;
252 		uint8                _compressedAudioFrame[735];
253 	};
254 };
255 
256 } // End of namespace BladeRunner
257 
258 #endif
259