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