ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 
158  int getCurFrame() const;
159 
164  virtual const Graphics::Surface *decodeNextFrame() = 0;
165 
170  virtual bool loadStream(Common::SeekableReadStream *stream) = 0;
171 
173  virtual bool isVideoLoaded() const = 0;
174 
176  bool endOfVideo() const;
177 
179  void close();
180 
182  Audio::Mixer::SoundType getSoundType() const;
184  Audio::AudioStream *getAudioStream() const;
185 
186  uint16 getWidth() const;
187  uint16 getHeight() const;
188  virtual uint32 getFlags() const = 0;
189  virtual Graphics::PixelFormat getPixelFormat() const = 0;
190 
191  uint32 getFrameCount() const;
192 
193  const byte *getPalette();
194  bool hasDirtyPalette() const;
195 
196  uint32 getTimeToNextFrame() const;
197  uint32 getStaticTimeToNextFrame() const;
198  int32 getExpectedFrameFromCurrentTime() const;
199 
200  void pauseVideo(bool pause);
201 
202 protected:
203  enum SoundStage {
204  kSoundNone = 0,
205  kSoundLoaded = 1,
206  kSoundPlaying = 2,
207  kSoundFinished = 3
208  };
209 
210  enum Features {
211  kFeaturesNone = 0x0000,
212  kFeaturesPalette = 0x0008,
213  kFeaturesDataSize = 0x0020,
214  kFeaturesSound = 0x0040,
215  kFeaturesFrameCoords = 0x0080,
216  kFeaturesStdCoords = 0x0100,
217  kFeaturesFramePos = 0x0200,
218  kFeaturesVideo = 0x0400
219  };
220 
221  Audio::Mixer *_mixer;
222  Audio::Mixer::SoundType _soundType;
223 
224  uint16 _width;
225  uint16 _height;
226 
227  uint16 _x;
228  uint16 _y;
229 
230  uint16 _defaultX;
231  uint16 _defaultY;
232 
233  uint32 _features;
234 
235  int32 _curFrame;
236  uint32 _frameCount;
237 
238  uint32 _startTime;
239 
240  Graphics::Palette _palette;
241  bool _paletteDirty;
242 
243  bool _isDouble;
244 
245  bool _ownSurface;
246  Graphics::Surface _surface;
247 
248  Common::List<Common::Rect> _dirtyRects;
249 
250  Common::Rational _frameRate;
251 
252  // Current sound state
253  bool _hasSound;
254  bool _soundEnabled;
255  SoundStage _soundStage;
256 
257  Audio::QueuingAudioStream *_audioStream;
258  Audio::SoundHandle _audioHandle;
259 
260  bool evaluateSeekFrame(int32 &frame, int whence) const;
261 
262  // Surface management
263  bool hasSurface();
264  void createSurface();
265  void freeSurface();
266 
267  // Decompression
268  uint32 deLZ77(byte *dest, const byte *src, uint32 srcSize, uint32 destSize);
269  void deRLE(byte *&destPtr, const byte *&srcPtr, int16 destLen, int16 srcLen);
270 
271  // Block rendering
272  void renderBlockWhole (Graphics::Surface &dstSurf, const byte *src, Common::Rect &rect);
273  void renderBlockWholeDouble (Graphics::Surface &dstSurf, const byte *src, Common::Rect &rect);
274  void renderBlockWhole4X (Graphics::Surface &dstSurf, const byte *src, Common::Rect &rect);
275  void renderBlockWhole2Y (Graphics::Surface &dstSurf, const byte *src, Common::Rect &rect);
276  void renderBlockSparse (Graphics::Surface &dstSurf, const byte *src, Common::Rect &rect);
277  void renderBlockSparseDouble(Graphics::Surface &dstSurf, const byte *src, Common::Rect &rect);
278  void renderBlockSparse2Y (Graphics::Surface &dstSurf, const byte *src, Common::Rect &rect);
279  void renderBlockRLE (Graphics::Surface &dstSurf, const byte *src, Common::Rect &rect);
280 
281  // Sound helper functions
282  inline void unsignedToSigned(byte *buffer, int length);
283 
284 private:
285  uint32 _pauseStartTime;
286  bool _isPaused;
287 };
288 
289 class PreIMDDecoder : public CoktelDecoder {
290 public:
291  PreIMDDecoder(uint16 width, uint16 height, Audio::Mixer *mixer,
293  ~PreIMDDecoder();
294 
295  bool reloadStream(Common::SeekableReadStream *stream);
296 
297  bool seek(int32 frame, int whence = SEEK_SET, bool restart = false);
298 
299  bool loadStream(Common::SeekableReadStream *stream);
300  void close();
301 
302  bool isVideoLoaded() const;
303 
304  const Graphics::Surface *decodeNextFrame();
305 
306  uint32 getFlags() const;
307 
308  Graphics::PixelFormat getPixelFormat() const;
309 
310 private:
312 
313  // Buffer for processed frame data
314  byte *_videoBuffer;
315  uint32 _videoBufferSize;
316 
317  // Frame decoding
318  void processFrame();
319  void renderFrame();
320 };
321 
322 class IMDDecoder : public CoktelDecoder {
323 public:
325  ~IMDDecoder();
326 
327  bool reloadStream(Common::SeekableReadStream *stream);
328 
329  bool seek(int32 frame, int whence = SEEK_SET, bool restart = false);
330 
331  void setXY(uint16 x, uint16 y);
332 
333  bool loadStream(Common::SeekableReadStream *stream);
334  void close();
335 
336  bool isVideoLoaded() const;
337 
338  const Graphics::Surface *decodeNextFrame();
339 
340  uint32 getFlags() const;
341 
342  Graphics::PixelFormat getPixelFormat() const;
343 
344 private:
345  enum Command {
346  kCommandNextSound = 0xFF00,
347  kCommandStartSound = 0xFF01,
348 
349  kCommandBreak = 0xFFF0,
350  kCommandBreakSkip0 = 0xFFF1,
351  kCommandBreakSkip16 = 0xFFF2,
352  kCommandBreakSkip32 = 0xFFF3,
353  kCommandBreakMask = 0xFFF8,
354 
355  kCommandPalette = 0xFFF4,
356  kCommandVideoData = 0xFFFC,
357 
358  kCommandJump = 0xFFFD
359  };
360 
361  struct Coord {
362  int16 left;
363  int16 top;
364  int16 right;
365  int16 bottom;
366  };
367 
369 
370  byte _version;
371 
372  // Standard coordinates gives by the header
373  int16 _stdX;
374  int16 _stdY;
375  int16 _stdWidth;
376  int16 _stdHeight;
377 
378  uint32 _flags;
379 
380  uint32 _firstFramePos;
381  uint32 *_framePos;
382  Coord *_frameCoords;
383 
384  uint32 _videoBufferSize;
385  byte *_videoBuffer[2];
386  uint32 _videoBufferLen[2];
387 
388  // Sound properties
389  uint16 _soundFlags;
390  int16 _soundFreq;
391  int16 _soundSliceSize;
392  int16 _soundSlicesCount;
393 
394  // Loading helper functions
395  bool loadCoordinates();
396  bool loadFrameTableOffsets(uint32 &framePosPos, uint32 &frameCoordsPos);
397  bool assessVideoProperties();
398  bool assessAudioProperties();
399  bool loadFrameTables(uint32 framePosPos, uint32 frameCoordsPos);
400 
401  // Frame decoding
402  void processFrame();
403  Common::Rect calcFrameCoords(uint32 frame);
404 
405  // Video
406  bool renderFrame(Common::Rect &rect);
407 
408  // Sound
409  void nextSoundSlice(bool hasNextCmd);
410  bool initialSoundSlice(bool hasNextCmd);
411  void emptySoundSlice(bool hasNextCmd);
412 };
413 
414 class VMDDecoder : public CoktelDecoder {
415 friend class AdvancedVMDDecoder;
416 
417 public:
419  ~VMDDecoder();
420 
421  bool reloadStream(Common::SeekableReadStream *stream);
422 
423  bool seek(int32 frame, int whence = SEEK_SET, bool restart = false);
424 
425  void setXY(uint16 x, uint16 y);
426 
427  void colorModeChanged();
428 
429  bool getFrameCoords(int16 frame, int16 &x, int16 &y, int16 &width, int16 &height);
430 
431  bool hasEmbeddedFiles() const;
432  bool hasEmbeddedFile(const Common::String &fileName) const;
433  Common::SeekableReadStream *getEmbeddedFile(const Common::String &fileName) const;
434 
435  int32 getSubtitleIndex() const;
436 
437  bool hasVideo() const;
438  bool isPaletted() const;
439 
440  bool loadStream(Common::SeekableReadStream *stream);
441  void close();
442 
443  bool isVideoLoaded() const;
444 
445  const Graphics::Surface *decodeNextFrame();
446 
447  uint32 getFlags() const;
448 
449  Graphics::PixelFormat getPixelFormat() const;
450 
451 protected:
452  void setAutoStartSound(bool autoStartSound);
453 
454 private:
455  enum PartType {
456  kPartTypeSeparator = 0,
457  kPartTypeAudio = 1,
458  kPartTypeVideo = 2,
459  kPartTypeFile = 3,
460  kPartType4 = 4,
461  kPartTypeSubtitle = 5
462  };
463 
464  enum AudioFormat {
465  kAudioFormat8bitRaw = 0,
466  kAudioFormat16bitDPCM = 1,
467  kAudioFormat16bitADPCM = 2
468  };
469 
470  struct File {
471  Common::String name;
472 
473  uint32 offset;
474  uint32 size;
475  uint32 realSize;
476 
477  File();
478  };
479 
480  struct Part {
481  PartType type;
482  byte field_1;
483  byte field_E;
484  uint32 size;
485  int16 left;
486  int16 top;
487  int16 right;
488  int16 bottom;
489  uint16 id;
490  byte flags;
491 
492  Part();
493  };
494 
495  struct Frame {
496  uint32 offset;
497  Part *parts;
498 
499  Frame();
500  ~Frame();
501  };
502 
504 
505  byte _version;
506  uint32 _flags;
507 
508  uint32 _frameInfoOffset;
509  uint16 _partsPerFrame;
510  Frame *_frames;
511 
512  Common::Array<File> _files;
513 
514  // Sound properties
515  uint16 _soundFlags;
516  int16 _soundFreq;
517  int16 _soundSliceSize;
518  int16 _soundSlicesCount;
519  byte _soundBytesPerSample;
520  byte _soundStereo; // (0: mono, 1: old-style stereo, 2: new-style stereo)
521  uint32 _soundHeaderSize;
522  uint32 _soundDataSize;
523  uint32 _soundLastFilledFrame;
524  AudioFormat _audioFormat;
525  bool _autoStartSound;
526 
536  Common::MemoryReadWriteStream *_oldStereoBuffer;
537 
538  // Video properties
539  bool _hasVideo;
540  uint32 _videoCodec;
541  byte _blitMode;
542  byte _bytesPerPixel;
543 
544  uint32 _firstFramePos;
545 
546  uint32 _videoBufferSize;
547  byte *_videoBuffer[3];
548  uint32 _videoBufferLen[3];
549 
550  Graphics::Surface _8bppSurface[3];
551 
552  bool _externalCodec;
553  Image::Codec *_codec;
554 
555  int32 _subtitle;
556 
557  bool _isPaletted;
558 
559  // Loading helper functions
560  bool assessVideoProperties();
561  bool assessAudioProperties();
562  bool openExternalCodec();
563  bool readFrameTable(int &numFiles);
564  bool readFiles();
565 
566  // Frame decoding
567  void processFrame();
568 
569  // Video
570  bool renderFrame(Common::Rect &rect);
571  bool getRenderRects(const Common::Rect &rect,
572  Common::Rect &realRect, Common::Rect &fakeRect);
573  void blit16(const Graphics::Surface &srcSurf, Common::Rect &rect);
574  void blit24(const Graphics::Surface &srcSurf, Common::Rect &rect);
575 
576  // Sound
577  void emptySoundSlice (uint32 size);
578  void filledSoundSlice (uint32 size);
579  void filledSoundSlices(uint32 size, uint32 mask);
580  void createAudioStream();
581 
582  uint8 evaluateMask(uint32 mask, bool *fillInfo, uint8 &max);
583 
584  // Generating audio streams
585  Audio::AudioStream *create8bitRaw (Common::SeekableReadStream *stream);
586  Audio::AudioStream *create16bitDPCM (Common::SeekableReadStream *stream);
587  Audio::AudioStream *create16bitADPCM(Common::SeekableReadStream *stream);
588 
589  bool getPartCoords(int16 frame, PartType type, int16 &x, int16 &y, int16 &width, int16 &height);
590 };
591 
596 class AdvancedVMDDecoder : public VideoDecoder {
597 public:
598  AdvancedVMDDecoder(Audio::Mixer::SoundType soundType = Audio::Mixer::kPlainSoundType);
599  ~AdvancedVMDDecoder();
600 
601  bool loadStream(Common::SeekableReadStream *stream);
602  void close();
603 
604  void setSurfaceMemory(void *mem, uint16 width, uint16 height, uint8 bpp);
605 
606 private:
607  class VMDVideoTrack : public FixedRateVideoTrack {
608  public:
609  VMDVideoTrack(VMDDecoder *decoder);
610 
611  uint16 getWidth() const;
612  uint16 getHeight() const;
613  Graphics::PixelFormat getPixelFormat() const;
614  int getCurFrame() const;
615  int getFrameCount() const;
616  const Graphics::Surface *decodeNextFrame();
617  const byte *getPalette() const;
618  bool hasDirtyPalette() const;
619 
620  protected:
621  Common::Rational getFrameRate() const;
622 
623  private:
624  VMDDecoder *_decoder;
625  };
626 
627  class VMDAudioTrack : public AudioTrack {
628  public:
629  VMDAudioTrack(VMDDecoder *decoder);
630 
631  protected:
632  virtual Audio::AudioStream *getAudioStream() const;
633 
634  private:
635  VMDDecoder *_decoder;
636  };
637 
638  VMDDecoder *_decoder;
639  VMDVideoTrack *_videoTrack;
640  VMDAudioTrack *_audioTrack;
641 };
642 
643 } // End of namespace Video
644 
645 #endif // VIDEO_COKTELDECODER_H
646 
647 #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:144
Definition: stream.h:745
Definition: rational.h:40
Definition: codec.h:57
Definition: mixer.h:49
SoundType
Definition: mixer.h:62
Definition: mixer.h:59
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:51
Definition: avi_frames.h:36
Definition: movie_decoder.h:32
Definition: system.h:38
Definition: mixer.h:63