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