ScummVM API documentation
coktel_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  * This file is dual-licensed.
22  * In addition to the GPLv3 license mentioned above, this code is also
23  * licensed under LGPL 2.1. See LICENSES/COPYING.LGPL file for the
24  * full text of the license.
25  *
26  */
27 
28 // Currently, only GOB and SCI32 games play IMDs and VMDs, so skip compiling if GOB and SCI32 is disabled.
29 #if !(defined(ENABLE_GOB) || defined(ENABLE_SCI32) || defined(DYNAMIC_MODULES))
30 
31 // Do not compile the CoktelDecoder code
32 
33 #else
34 
35 #ifndef VIDEO_COKTELDECODER_H
36 #define VIDEO_COKTELDECODER_H
37 
38 #include "common/list.h"
39 #include "common/array.h"
40 #include "common/rational.h"
41 #include "common/str.h"
42 
43 #include "graphics/palette.h"
44 #include "graphics/surface.h"
45 
46 #include "video/video_decoder.h"
47 
48 #include "audio/mixer.h"
49 
50 namespace Common {
51 struct Rect;
52 class MemoryReadWriteStream;
53 class SeekableReadStream;
54 }
55 namespace Audio {
56 class QueuingAudioStream;
57 }
58 
59 namespace Graphics {
60 struct PixelFormat;
61 }
62 
63 namespace Image {
64 class Codec;
65 }
66 
67 namespace Video {
68 
76 class CoktelDecoder {
77 public:
78  struct State {
80  uint32 flags;
82  uint16 speechId;
83 
84  State();
85  };
86 
87  CoktelDecoder(Audio::Mixer *mixer,
89  virtual ~CoktelDecoder();
90 
92  virtual bool reloadStream(Common::SeekableReadStream *stream) = 0;
93 
94  virtual bool seek(int32 frame, int whence = SEEK_SET, bool restart = false) = 0;
95 
97  void setSurfaceMemory(void *mem, uint16 width, uint16 height, uint8 bpp);
99  void setSurfaceMemory();
100 
101  const Graphics::Surface *getSurface() const;
102 
104  virtual void setXY(uint16 x, uint16 y);
106  void setXY();
107 
108  void setDouble(bool isDouble); // double the size of the video, to accommodate higher resolutions
109 
111  void setFrameRate(Common::Rational frameRate);
113  Common::Rational getFrameRate() const;
114 
116  uint16 getDefaultX() const;
118  uint16 getDefaultY() const;
119 
121  const Common::List<Common::Rect> &getDirtyRects() const;
122 
123  bool hasPalette() const;
124  virtual bool hasVideo() const;
125 
126  bool hasSound() const;
127  bool isSoundEnabled() const;
128  bool isSoundPlaying() const;
129 
130  void enableSound();
131  void disableSound();
132  void finishSound();
133 
134  virtual void colorModeChanged();
135 
137  virtual bool getFrameCoords(int16 frame, int16 &x, int16 &y, int16 &width, int16 &height);
138 
140  virtual bool hasEmbeddedFiles() const;
141 
143  virtual bool hasEmbeddedFile(const Common::String &fileName) const;
144 
146  virtual Common::SeekableReadStream *getEmbeddedFile(const Common::String &fileName) const;
147 
149  virtual int32 getSubtitleIndex() const;
150 
152  virtual bool isPaletted() const;
153 
154  virtual uint32 getVideoBufferSize() const = 0;
155 
160  int getCurFrame() const;
161  int getNbFramesPastEnd() const;
162 
167  virtual const Graphics::Surface *decodeNextFrame() = 0;
168 
173  virtual bool loadStream(Common::SeekableReadStream *stream) = 0;
174 
176  virtual bool isVideoLoaded() const = 0;
177 
179  bool endOfVideo() const;
180 
182  void close();
183 
185  Audio::Mixer::SoundType getSoundType() const;
187  Audio::AudioStream *getAudioStream() const;
188 
189  uint16 getWidth() const;
190  uint16 getHeight() const;
191  virtual uint32 getFlags() const = 0;
192  virtual uint16 getSoundFlags() const = 0;
193  virtual Graphics::PixelFormat getPixelFormat() const = 0;
194 
195  uint32 getFrameCount() const;
196 
197  const byte *getPalette();
198  bool hasDirtyPalette() const;
199 
200  uint32 getTimeToNextFrame() const;
201  uint32 getStaticTimeToNextFrame() const;
202  int32 getExpectedFrameFromCurrentTime() const;
203 
204  void pauseVideo(bool pause);
205 
206 protected:
207  enum SoundStage {
208  kSoundNone = 0,
209  kSoundLoaded = 1,
210  kSoundPlaying = 2,
211  kSoundFinished = 3
212  };
213 
214  enum Features {
215  kFeaturesNone = 0x0000,
216  kFeaturesPalette = 0x0008,
217  kFeaturesDataSize = 0x0020,
218  kFeaturesSound = 0x0040,
219  kFeaturesFrameCoords = 0x0080,
220  kFeaturesStdCoords = 0x0100,
221  kFeaturesFramePos = 0x0200,
222  kFeaturesVideo = 0x0400
223  };
224 
225  Audio::Mixer *_mixer;
226  Audio::Mixer::SoundType _soundType;
227 
228  uint16 _width;
229  uint16 _height;
230 
231  uint16 _x;
232  uint16 _y;
233 
234  uint16 _defaultX;
235  uint16 _defaultY;
236 
237  uint32 _features;
238 
239  int32 _curFrame;
240  int32 _nbFramesPastEnd;
241  uint32 _frameCount;
242 
243  uint32 _startTime;
244 
245  Graphics::Palette _palette;
246  bool _paletteDirty;
247 
248  bool _isDouble;
249 
250  bool _ownSurface;
251  Graphics::Surface _surface;
252 
253  Common::List<Common::Rect> _dirtyRects;
254 
255  Common::Rational _frameRate;
256 
257  // Current sound state
258  bool _hasSound;
259  bool _soundEnabled;
260  SoundStage _soundStage;
261 
262  Audio::QueuingAudioStream *_audioStream;
263  Audio::SoundHandle _audioHandle;
264 
265  bool evaluateSeekFrame(int32 &frame, int whence) const;
266 
267  // Surface management
268  bool hasSurface();
269  void createSurface();
270  void freeSurface();
271 
272  // Decompression
273  uint32 deLZ77(byte *dest, const byte *src, uint32 srcSize, uint32 destSize);
274  void deRLE(byte *&destPtr, const byte *&srcPtr, int16 destLen, int16 srcLen);
275 
276  // Block rendering
277  void renderBlockWhole (Graphics::Surface &dstSurf, const byte *src, Common::Rect &rect);
278  void renderBlockWholeDouble (Graphics::Surface &dstSurf, const byte *src, Common::Rect &rect);
279  void renderBlockWhole4X (Graphics::Surface &dstSurf, const byte *src, Common::Rect &rect);
280  void renderBlockWhole2Y (Graphics::Surface &dstSurf, const byte *src, Common::Rect &rect);
281  void renderBlockSparse (Graphics::Surface &dstSurf, const byte *src, Common::Rect &rect);
282  void renderBlockSparseDouble(Graphics::Surface &dstSurf, const byte *src, Common::Rect &rect);
283  void renderBlockSparse2Y (Graphics::Surface &dstSurf, const byte *src, Common::Rect &rect);
284  void renderBlockRLE (Graphics::Surface &dstSurf, const byte *src, Common::Rect &rect);
285 
286  // Sound helper functions
287  inline void unsignedToSigned(byte *buffer, int length);
288 
289 private:
290  uint32 _pauseStartTime;
291  bool _isPaused;
292 };
293 
294 class PreIMDDecoder : public CoktelDecoder {
295 public:
296  PreIMDDecoder(uint16 width, uint16 height, Audio::Mixer *mixer,
298  ~PreIMDDecoder();
299 
300  bool reloadStream(Common::SeekableReadStream *stream);
301 
302  bool seek(int32 frame, int whence = SEEK_SET, bool restart = false);
303 
304  bool loadStream(Common::SeekableReadStream *stream);
305  void close();
306 
307  bool isVideoLoaded() const;
308 
309  const Graphics::Surface *decodeNextFrame();
310 
311  uint32 getFlags() const;
312  uint16 getSoundFlags() const;
313  uint32 getVideoBufferSize() const;
314 
315  Graphics::PixelFormat getPixelFormat() const;
316 
317 private:
319 
320  // Buffer for processed frame data
321  byte *_videoBuffer;
322  uint32 _videoBufferSize;
323 
324  // Frame decoding
325  void processFrame();
326  void renderFrame();
327 };
328 
329 class IMDDecoder : public CoktelDecoder {
330 public:
332  ~IMDDecoder();
333 
334  bool reloadStream(Common::SeekableReadStream *stream);
335 
336  bool seek(int32 frame, int whence = SEEK_SET, bool restart = false);
337 
338  void setXY(uint16 x, uint16 y);
339 
340  bool loadStream(Common::SeekableReadStream *stream);
341  void close();
342 
343  bool isVideoLoaded() const;
344 
345  const Graphics::Surface *decodeNextFrame();
346 
347  uint32 getFlags() const;
348  uint16 getSoundFlags() const;
349  uint32 getVideoBufferSize() const;
350 
351  Graphics::PixelFormat getPixelFormat() const;
352 
353 private:
354  enum Command {
355  kCommandNextSound = 0xFF00,
356  kCommandStartSound = 0xFF01,
357 
358  kCommandBreak = 0xFFF0,
359  kCommandBreakSkip0 = 0xFFF1,
360  kCommandBreakSkip16 = 0xFFF2,
361  kCommandBreakSkip32 = 0xFFF3,
362  kCommandBreakMask = 0xFFF8,
363 
364  kCommandPalette = 0xFFF4,
365  kCommandVideoData = 0xFFFC,
366 
367  kCommandJump = 0xFFFD
368  };
369 
370  struct Coord {
371  int16 left;
372  int16 top;
373  int16 right;
374  int16 bottom;
375  };
376 
378 
379  byte _version;
380 
381  // Standard coordinates gives by the header
382  int16 _stdX;
383  int16 _stdY;
384  int16 _stdWidth;
385  int16 _stdHeight;
386 
387  uint32 _flags;
388 
389  uint32 _firstFramePos;
390  uint32 *_framePos;
391  Coord *_frameCoords;
392 
393  uint32 _videoBufferSize;
394  byte *_videoBuffer[2];
395  uint32 _videoBufferLen[2];
396 
397  // Sound properties
398  uint16 _soundFlags;
399  int16 _soundFreq;
400  int16 _soundSliceSize;
401  int16 _soundSlicesCount;
402 
403  // Loading helper functions
404  bool loadCoordinates();
405  bool loadFrameTableOffsets(uint32 &framePosPos, uint32 &frameCoordsPos);
406  bool assessVideoProperties();
407  bool assessAudioProperties();
408  bool loadFrameTables(uint32 framePosPos, uint32 frameCoordsPos);
409 
410  // Frame decoding
411  void processFrame();
412  Common::Rect calcFrameCoords(uint32 frame);
413 
414  // Video
415  bool renderFrame(Common::Rect &rect);
416 
417  // Sound
418  void nextSoundSlice(bool hasNextCmd);
419  bool initialSoundSlice(bool hasNextCmd);
420  void emptySoundSlice(bool hasNextCmd);
421 };
422 
423 class VMDDecoder : public CoktelDecoder {
424 friend class AdvancedVMDDecoder;
425 
426 public:
428  ~VMDDecoder();
429 
430  bool reloadStream(Common::SeekableReadStream *stream);
431 
432  bool seek(int32 frame, int whence = SEEK_SET, bool restart = false);
433 
434  void setXY(uint16 x, uint16 y);
435 
436  void colorModeChanged();
437 
438  bool getFrameCoords(int16 frame, int16 &x, int16 &y, int16 &width, int16 &height);
439 
440  bool hasEmbeddedFiles() const;
441  bool hasEmbeddedFile(const Common::String &fileName) const;
442  Common::SeekableReadStream *getEmbeddedFile(const Common::String &fileName) const;
443 
444  int32 getSubtitleIndex() const;
445 
446  bool hasVideo() const;
447  bool isPaletted() const;
448 
449  bool loadStream(Common::SeekableReadStream *stream);
450  void close();
451 
452  bool isVideoLoaded() const;
453 
454  const Graphics::Surface *decodeNextFrame();
455 
456  uint32 getFlags() const;
457  uint16 getSoundFlags() const;
458  uint32 getVideoBufferSize() const;
459 
460  Graphics::PixelFormat getPixelFormat() const;
461 
462 protected:
463  void setAutoStartSound(bool autoStartSound);
464 
465 private:
466  enum PartType {
467  kPartTypeSeparator = 0,
468  kPartTypeAudio = 1,
469  kPartTypeVideo = 2,
470  kPartTypeFile = 3,
471  kPartType4 = 4,
472  kPartTypeSubtitle = 5
473  };
474 
475  enum AudioFormat {
476  kAudioFormat8bitRaw = 0,
477  kAudioFormat16bitDPCM = 1,
478  kAudioFormat16bitADPCM = 2
479  };
480 
481  struct File {
482  Common::String name;
483 
484  uint32 offset;
485  uint32 size;
486  uint32 realSize;
487 
488  File();
489  };
490 
491  struct Part {
492  PartType type;
493  byte field_1;
494  byte field_E;
495  uint32 size;
496  int16 left;
497  int16 top;
498  int16 right;
499  int16 bottom;
500  uint16 id;
501  byte flags;
502 
503  Part();
504  };
505 
506  struct Frame {
507  uint32 offset;
508  Part *parts;
509 
510  Frame();
511  ~Frame();
512  };
513 
515 
516  byte _version;
517  uint32 _flags;
518 
519  uint32 _frameInfoOffset;
520  uint16 _partsPerFrame;
521  Frame *_frames;
522 
523  Common::Array<File> _files;
524 
525  // Sound properties
526  uint16 _soundFlags;
527  uint16 _soundFreq;
528  int16 _soundSliceSize;
529  int16 _soundSlicesCount;
530  byte _soundBytesPerSample;
531  byte _soundStereo; // (0: mono, 1: old-style stereo, 2: new-style stereo)
532  uint32 _soundHeaderSize;
533  uint32 _soundDataSize;
534  uint32 _soundLastFilledFrame;
535  AudioFormat _audioFormat;
536  bool _autoStartSound;
537 
547  Common::MemoryReadWriteStream *_oldStereoBuffer;
548 
549  // Video properties
550  bool _hasVideo;
551  uint32 _videoCodec;
552  byte _blitMode;
553  byte _bytesPerPixel;
554 
555  uint32 _firstFramePos;
556 
557  uint32 _videoBufferSize;
558  byte *_videoBuffer[3];
559  uint32 _videoBufferLen[3];
560 
561  Graphics::Surface _8bppSurface[3];
562 
563  bool _externalCodec;
564  Image::Codec *_codec;
565 
566  int32 _subtitle;
567 
568  bool _isPaletted;
569 
570  // Loading helper functions
571  bool assessVideoProperties();
572  bool assessAudioProperties();
573  bool openExternalCodec();
574  bool readFrameTable(int &numFiles);
575  bool readFiles();
576 
577  // Frame decoding
578  void processFrame();
579 
580  // Video
581  bool renderFrame(Common::Rect &rect);
582  bool getRenderRects(const Common::Rect &rect,
583  Common::Rect &realRect, Common::Rect &fakeRect);
584  void blit16(const Graphics::Surface &srcSurf, Common::Rect &rect);
585  void blit24(const Graphics::Surface &srcSurf, Common::Rect &rect);
586 
587  // Sound
588  void emptySoundSlice (uint32 size);
589  void filledSoundSlice (uint32 size);
590  void filledSoundSlices(uint32 size, uint32 mask);
591  void createAudioStream();
592 
593  uint8 evaluateMask(uint32 mask, bool *fillInfo, uint8 &max);
594 
595  // Generating audio streams
596  Audio::AudioStream *create8bitRaw (Common::SeekableReadStream *stream);
597  Audio::AudioStream *create16bitDPCM (Common::SeekableReadStream *stream);
598  Audio::AudioStream *create16bitADPCM(Common::SeekableReadStream *stream);
599 
600  bool getPartCoords(int16 frame, PartType type, int16 &x, int16 &y, int16 &width, int16 &height);
601 };
602 
607 class AdvancedVMDDecoder : public VideoDecoder {
608 public:
609  AdvancedVMDDecoder(Audio::Mixer::SoundType soundType = Audio::Mixer::kPlainSoundType);
610  ~AdvancedVMDDecoder();
611 
612  bool loadStream(Common::SeekableReadStream *stream);
613  void close();
614 
615  void setSurfaceMemory(void *mem, uint16 width, uint16 height, uint8 bpp);
616 
617 private:
618  class VMDVideoTrack : public FixedRateVideoTrack {
619  public:
620  VMDVideoTrack(VMDDecoder *decoder);
621 
622  uint16 getWidth() const;
623  uint16 getHeight() const;
624  Graphics::PixelFormat getPixelFormat() const;
625  int getCurFrame() const;
626  int getFrameCount() const;
627  const Graphics::Surface *decodeNextFrame();
628  const byte *getPalette() const;
629  bool hasDirtyPalette() const;
630 
631  protected:
632  Common::Rational getFrameRate() const;
633 
634  private:
635  VMDDecoder *_decoder;
636  };
637 
638  class VMDAudioTrack : public AudioTrack {
639  public:
640  VMDAudioTrack(VMDDecoder *decoder);
641 
642  protected:
643  virtual Audio::AudioStream *getAudioStream() const;
644 
645  private:
646  VMDDecoder *_decoder;
647  };
648 
649  VMDDecoder *_decoder;
650  VMDVideoTrack *_videoTrack;
651  VMDAudioTrack *_audioTrack;
652 };
653 
654 } // End of namespace Video
655 
656 #endif // VIDEO_COKTELDECODER_H
657 
658 #endif // Engine and dynamic plugins guard
Definition: str.h:59
Definition: surface.h:67
Definition: array.h:52
Definition: pixelformat.h:138
Definition: rect.h:524
Definition: stream.h:745
Definition: rational.h:40
Definition: codec.h:57
Definition: mixer.h:49
SoundType
Definition: mixer.h:73
Definition: mixer.h:70
Definition: algorithm.h:29
Definition: formatinfo.h:28
Definition: audiostream.h:50
Definition: memstream.h:283
Definition: audiostream.h:370
Simple class for handling a palette data.
Definition: palette.h:55
Definition: avi_frames.h:36
Definition: movie_decoder.h:32
Definition: system.h:38
Definition: mixer.h:74