ScummVM API documentation
vqa_decoder.h
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 modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (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, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef BLADERUNNER_VQA_DECODER_H
23 #define BLADERUNNER_VQA_DECODER_H
24 
25 #include "bladerunner/adpcm_decoder.h"
26 
27 #include "audio/audiostream.h"
28 
29 #include "common/debug.h"
30 #include "common/str.h"
31 #include "common/stream.h"
32 #include "common/types.h"
33 
34 #include "graphics/surface.h"
35 
36 #include "common/array.h"
37 #include "common/rational.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  struct VQPPalette {
59  uint8 interpol2D[256][256];
60  };
61 
62 public:
63  VQADecoder();
64  ~VQADecoder();
65 
66  bool loadStream(Common::SeekableReadStream *s);
67  void close();
68 
69  void readFrame(int frame, uint readFlags = kVQAReadAll);
70 
71  void decodeVideoFrame(Graphics::Surface *surface, int frame, bool forceDraw = false);
72  void decodeZBuffer(ZBuffer *zbuffer);
73  Audio::SeekableAudioStream *decodeAudioFrame();
74  void decodeView(View *view);
75  void decodeScreenEffects(ScreenEffects *aesc);
76  void decodeLights(Lights *lights);
77 
78  uint16 numFrames() const { return _header.numFrames; }
79  uint8 frameRate() const { return _header.frameRate; }
80 
81  uint16 offsetX() const { return _header.offsetX; }
82  uint16 offsetY() const { return _header.offsetY; }
83 
84  void overrideOffsetXY(uint16 offX, uint16 offY);
85 
86  bool hasAudio() const { return _header.channels != 0; }
87  uint16 frequency() const { return _header.freq; }
88 
89  bool getLoopBeginAndEndFrame(int loopId, int *begin, int *end);
90  int getLoopIdFromFrame(int frame);
91 
92  void allocatePaletteVQPTable(const uint32 numOfPalettes);
93  void updatePaletteVQPTable(uint32 palId, uint16 j, uint16 k, uint8 colorByte);
94  void deleteVQPTable();
95 
96  struct Header {
97  uint16 version; // 0x00
98  uint16 flags; // 0x02
99  uint16 numFrames; // 0x04
100  uint16 width; // 0x06
101  uint16 height; // 0x08
102  uint8 blockW; // 0x0A
103  uint8 blockH; // 0x0B
104  uint8 frameRate; // 0x0C
105  uint8 cbParts; // 0x0D
106  uint16 colors; // 0x0E
107  uint16 maxBlocks; // 0x10
108  uint16 offsetX; // 0x12
109  uint16 offsetY; // 0x14
110  uint16 maxVPTRSize; // 0x16
111  uint16 freq; // 0x18
112  uint8 channels; // 0x1A
113  uint8 bits; // 0x1B
114  uint32 unk3; // 0x1C
115  uint16 unk4; // 0x20
116  uint32 maxCBFZSize; // 0x22
117  uint32 unk5; // 0x26
118  // 0x2A
119  };
120 
121  struct Loop {
122  uint16 begin;
123  uint16 end;
124  Common::String name;
125 
126  Loop() :
127  begin(0),
128  end(0)
129  {}
130  };
131 
132  struct LoopInfo {
133  uint16 loopCount;
134  uint32 flags;
135  Loop *loops;
136 
137  LoopInfo() : loopCount(0), loops(nullptr), flags(0) {}
138  ~LoopInfo() {
139  close();
140  }
141 
142  void close() {
143  delete[] loops;
144  loops = nullptr;
145  loopCount = 0;
146  flags = 0;
147  }
148  };
149 
150  struct CodebookInfo {
151  uint16 frame;
152  uint32 size;
153  uint8 *data;
154  };
155 
156  class VQAVideoTrack;
157  class VQAAudioTrack;
158 
160 
161  Header _header;
162  int _readingFrame;
163  int _decodingFrame;
164  LoopInfo _loopInfo;
165 
166  VQPPalette *_vqpPalsArr;
167  uint16 _numOfVQPPalettes;
168 
169  bool _oldV2VQA;
170  // TODO Maybe add public methods for setting these member vars (essentially flags)?
171  bool _allowHorizontalScanlines;
172  bool _allowVerticalScanlines;
173  bool _scaleVideoTo2xRequested;
174  bool _scale2xPossible;
175  bool _centerVideoRequested;
176  Common::Array<CodebookInfo> _codebooks;
177 
178  uint32 *_frameInfo;
179 
180  uint32 _maxVIEWChunkSize;
181  uint32 _maxZBUFChunkSize;
182  uint32 _maxAESCChunkSize;
183 
184  VQAVideoTrack *_videoTrack;
185  VQAAudioTrack *_audioTrack;
186 
187  void readPacket(uint readFlags);
188 
189  bool readVQHD(Common::SeekableReadStream *s, uint32 size);
190  bool readMSCI(Common::SeekableReadStream *s, uint32 size);
191  bool readMFCI(Common::SeekableReadStream *s, uint32 size);
192  bool readLINF(Common::SeekableReadStream *s, uint32 size);
193  bool readCINF(Common::SeekableReadStream *s, uint32 size);
194  bool readFINF(Common::SeekableReadStream *s, uint32 size);
195  bool readLNIN(Common::SeekableReadStream *s, uint32 size);
196  bool readCLIP(Common::SeekableReadStream *s, uint32 size);
197 
198  CodebookInfo &codebookInfoForFrame(int frame);
199 
201  static const uint kSizeInBytesOfCPL0Chunk = 768; // 3 * 256
202  public:
203  VQAVideoTrack(VQADecoder *vqaDecoder);
204  ~VQAVideoTrack();
205 
206  uint16 getWidth() const;
207  uint16 getHeight() const;
208 
209  int getFrameCount() const;
210 
211  void overrideOffsetXY(uint16 offX, uint16 offY);
212 
213  void decodeVideoFrame(Graphics::Surface *surface, bool forceDraw);
214  void decodeZBuffer(ZBuffer *zbuffer);
215  void decodeView(View *view);
216  void decodeScreenEffects(ScreenEffects *aesc);
217  void decodeLights(Lights *lights);
218 
219  bool readVQFR(Common::SeekableReadStream *s, uint32 size, uint readFlags);
220  bool readVPTZ(Common::SeekableReadStream* s, uint32 size); // (for old type v2 VQA, eg. SIZZLE.VQAs)
221  bool readVPTR(Common::SeekableReadStream *s, uint32 size);
222  bool readVQFL(Common::SeekableReadStream *s, uint32 size, uint readFlags);
223  bool readCBFZ(Common::SeekableReadStream *s, uint32 size);
224  bool readCBPZ(Common::SeekableReadStream* s, uint32 size); // (for old type v2 VQA, eg. SIZZLE.VQAs)
225  bool readZBUF(Common::SeekableReadStream *s, uint32 size);
226  bool readVIEW(Common::SeekableReadStream *s, uint32 size);
227  bool readAESC(Common::SeekableReadStream *s, uint32 size);
228  bool readLITE(Common::SeekableReadStream *s, uint32 size);
229  bool readCPL0(Common::SeekableReadStream *s, uint32 size); // (for old type v2 VQA, eg. SIZZLE.VQAs)
230 
231  protected:
232  Common::Rational getFrameRate() const;
233 
234  bool useAudioSync() const { return false; }
235 
236  private:
237  VQADecoder *_vqaDecoder;
238 
239  bool _hasNewFrame;
240 
241  uint16 _numFrames;
242  uint16 _width, _height;
243  uint8 _blockW, _blockH;
244  uint8 _frameRate;
245  uint8 _cbParts;
246  uint16 _maxBlocks;
247  uint16 _offsetX, _offsetY;
248 
249  uint16 _maxVPTRSize;
250  uint32 _maxCBFZSize;
251  uint32 _maxZBUFChunkSize;
252 
253  uint8 *_codebook;
254  uint8 *_cbfz;
255  uint32 _zbufChunkSize;
256  uint8 *_zbufChunk;
257 
258  uint32 _vpointerSize;
259  uint8 *_vpointer;
260 
261  int _curFrame;
262 
263  uint8 *_viewData;
264  uint32 _viewDataSize;
265  uint8 *_lightsData;
266  uint32 _lightsDataSize;
267  uint8 *_screenEffectsData;
268  uint32 _screenEffectsDataSize;
269 
270  int32 _currentPaletteId;
271  uint8 *_cpalPointer;
272  uint8 *_cpalPointerNext;
273  uint32 _cpalPointerSize;
274  uint32 _cpalPointerSizeNext;
275  uint8 *_vptz;
276  uint8 *_cbfzNext; // Used to store of compressed parts of a codebook data
277  uint8 _countOfCBPsToCBF;
278  uint32 _accumulatedCBPZsizeToCBF;
279 
280  CodebookInfo *_codebookInfoNext; // Used to store the decompressed codebook data and swap with the active codebook
281 
282  void VPTRWriteBlock(Graphics::Surface *surface, unsigned int dstBlock, unsigned int srcBlock, int count, bool alpha = false);
283  bool decodeFrame(Graphics::Surface *surface);
284  };
285 
287  static const uint kSizeInShortsAllocatedToAudioFrame = 2940; // 4 * 735
288  static const uint kSizeInBytesOfCompressedAudioFrame = 735;
289  static const uint kSizeInShortsAllocatedToAudioFrameMax = 22048; // 4 * 5512 (for old type v2 VQA, eg. SIZZLE.VQAs)
290  static const uint kSizeInBytesOfCompressedAudioFrameMax = 5512; // (for old type v2 VQA, eg. SIZZLE.VQAs)
291  public:
292  VQAAudioTrack(VQADecoder *vqaDecoder);
293  ~VQAAudioTrack();
294 
295  bool readSND2(Common::SeekableReadStream *s, uint32 size);
296  bool readSN2J(Common::SeekableReadStream *s, uint32 size);
297 
298  Audio::SeekableAudioStream *decodeAudioFrame();
299  protected:
300 
301  private:
302  uint16 _frequency;
303  ADPCMWestwoodDecoder _adpcmDecoder;
304  bool _bigCompressedAudioFrame;
305  uint8 _compressedAudioFrame[kSizeInBytesOfCompressedAudioFrameMax];
306 // uint8 _compressedAudioFrame[kSizeInBytesOfCompressedAudioFrame + 1]; // +1 because we use roundup for read-in and 735 is odd
307  };
308 };
309 
310 } // End of namespace BladeRunner
311 
312 #endif
Definition: view.h:33
Definition: str.h:59
Definition: surface.h:67
Definition: array.h:52
Definition: vqa_decoder.h:150
Definition: actor.h:31
Definition: vqa_decoder.h:96
Definition: zbuffer.h:49
Definition: stream.h:745
Definition: rational.h:40
Definition: audiostream.h:212
Definition: lights.h:33
Definition: debugger.h:56
Definition: adpcm_decoder.h:29
Definition: vqa_decoder.h:55
Definition: vqa_decoder.h:132
Definition: vqa_decoder.h:200
Definition: vqa_decoder.h:286
Definition: screen_effects.h:39
Definition: vqa_decoder.h:121