ScummVM API documentation
video_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 VIDEO_DECODER_H
23 #define VIDEO_DECODER_H
24 
25 #include "audio/mixer.h"
26 #include "audio/timestamp.h" // TODO: Move this to common/ ?
27 #include "common/array.h"
28 #include "common/path.h"
29 #include "common/rational.h"
30 #include "common/str.h"
31 #include "graphics/pixelformat.h"
32 #include "image/codec-options.h"
33 
34 namespace Audio {
35 class AudioStream;
36 class RewindableAudioStream;
37 class SeekableAudioStream;
38 }
39 
40 namespace Common {
41 class SeekableReadStream;
42 }
43 
44 namespace Graphics {
45 struct Surface;
46 }
47 
48 namespace Video {
49 
53 class VideoDecoder {
54 public:
55  VideoDecoder();
56  virtual ~VideoDecoder() {}
57 
59  // Opening/Closing a Video
61 
70  virtual bool loadFile(const Common::Path &filename);
71 
83  virtual bool loadStream(Common::SeekableReadStream *stream) = 0;
84 
91  virtual void close();
92 
96  bool isVideoLoaded() const;
97 
98 
100  // Playback Control
102 
108  void start();
109 
115  void stop();
116 
127  void setRate(const Common::Rational &rate);
128 
133  Common::Rational getRate() const { return _playbackRate; }
134 
143  bool isPlaying() const;
144 
149  virtual bool isRewindable() const;
150 
159  virtual bool rewind();
160 
165  virtual bool isSeekable() const;
166 
178  bool seek(const Audio::Timestamp &time);
179 
186  virtual bool seekToFrame(uint frame);
187 
199  void pauseVideo(bool pause);
200 
204  bool isPaused() const { return _pauseLevel != 0; }
205 
213  void setEndTime(const Audio::Timestamp &endTime);
214 
224  void setEndFrame(uint frame);
225 
229  Audio::Timestamp getEndTime() const { return _endTime; }
230 
234  void resetStartTime();
235 
236 
238  // Playback Status
240 
245  bool endOfVideo() const;
246 
251  int getCurFrame() const;
252 
257  int getCurFrameDelay() const;
258 
263  uint32 getFrameCount() const;
264 
279  uint32 getTime() const;
280 
281 
283  // Video Info
285 
295  virtual uint16 getWidth() const;
296 
306  virtual uint16 getHeight() const;
307 
311  Graphics::PixelFormat getPixelFormat() const;
312 
319  virtual Audio::Timestamp getDuration() const;
320 
321 
323  // Frame Decoding
325 
332  const byte *getPalette();
333 
337  bool hasDirtyPalette() const { return _dirtyPalette; }
338 
343  void delayMillis(uint msecs);
344 
348  uint32 getTimeToNextFrame() const;
349 
355  bool needsUpdate() const;
356 
372  virtual const Graphics::Surface *decodeNextFrame();
373 
384  bool setReverse(bool reverse);
385 
403  bool setDitheringPalette(const byte *palette);
404 
414  bool setOutputPixelFormat(const Graphics::PixelFormat &format);
415 
426  bool setOutputPixelFormats(const Common::List<Graphics::PixelFormat> &formatList);
427 
431  virtual void setVideoCodecAccuracy(Image::CodecAccuracy accuracy);
432 
434  // Audio Control
436 
441  byte getVolume() const { return _audioVolume; }
442 
450  void setVolume(byte volume);
451 
456  int8 getBalance() const { return _audioBalance; }
457 
465  void setBalance(int8 balance);
466 
470  Audio::Mixer::SoundType getSoundType() const;
471 
477  void setSoundType(Audio::Mixer::SoundType soundType);
478 
482  bool addStreamTrack(Audio::SeekableAudioStream *stream);
483 
489  bool addStreamFileTrack(const Common::Path &baseName);
490 
499  bool setAudioTrack(int index);
500 
504  uint getAudioTrackCount() const;
505 
506 protected:
511  class Track {
512  public:
513  Track();
514  virtual ~Track() {}
515 
519  enum TrackType {
520  kTrackTypeNone,
521  kTrackTypeVideo,
522  kTrackTypeAudio
523  };
524 
528  virtual TrackType getTrackType() const = 0;
529 
533  virtual bool endOfTrack() const = 0;
534 
541  virtual bool isRewindable() const;
542 
551  virtual bool rewind();
552 
556  virtual bool isSeekable() const { return false; }
557 
563  virtual bool seek(const Audio::Timestamp &time) { return false; }
564 
568  void pause(bool shouldPause);
569 
573  bool isPaused() const { return _paused; }
574 
580  virtual Audio::Timestamp getDuration() const;
581 
582  protected:
586  virtual void pauseIntern(bool shouldPause) {}
587 
588  private:
589  bool _paused;
590  };
591 
595  class VideoTrack : public Track {
596  public:
597  VideoTrack() {}
598  virtual ~VideoTrack() {}
599 
600  TrackType getTrackType() const { return kTrackTypeVideo; }
601  virtual bool endOfTrack() const;
602 
606  virtual uint16 getWidth() const = 0;
607 
611  virtual uint16 getHeight() const = 0;
612 
616  virtual Graphics::PixelFormat getPixelFormat() const = 0;
617 
621  virtual bool setOutputPixelFormat(const Graphics::PixelFormat &format) { return format == getPixelFormat(); }
622 
626  virtual void setCodecAccuracy(Image::CodecAccuracy accuracy) {}
627 
633  virtual int getCurFrame() const = 0;
634 
640  virtual int getCurFrameDelay() const {
641  return 0;
642  };
643 
651  virtual int getFrameCount() const { return 0; }
652 
657  virtual uint32 getNextFrameStartTime() const = 0;
658 
662  virtual const Graphics::Surface *decodeNextFrame() = 0;
663 
667  virtual const byte *getPalette() const { return 0; }
668 
672  virtual bool hasDirtyPalette() const { return false; }
673 
680  virtual Audio::Timestamp getFrameTime(uint frame) const;
681 
690  virtual bool setReverse(bool reverse) { return !reverse; }
691 
695  virtual bool isReversed() const { return false; }
696 
700  virtual bool canDither() const { return false; }
701 
705  virtual void setDither(const byte *palette) {}
706  };
707 
714  public:
716  virtual ~FixedRateVideoTrack() {}
717 
718  uint32 getNextFrameStartTime() const;
719  virtual Audio::Timestamp getDuration() const;
720  Audio::Timestamp getFrameTime(uint frame) const;
721 
726  uint getFrameAtTime(const Audio::Timestamp &time) const;
727 
728  protected:
732  virtual Common::Rational getFrameRate() const = 0;
733  };
734 
738  class AudioTrack : public Track {
739  public:
741  virtual ~AudioTrack() {}
742 
743  TrackType getTrackType() const { return kTrackTypeAudio; }
744 
745  virtual bool endOfTrack() const;
746 
750  void start();
751 
755  void stop();
756 
757  void start(const Audio::Timestamp &limit);
758 
762  byte getVolume() const { return _volume; }
763 
767  void setVolume(byte volume);
768 
772  uint32 getRate() const { return _rate; }
773 
777  void setRate(uint32 rate);
778 
783  void setRate(Common::Rational rate);
784 
788  int8 getBalance() const { return _balance; }
789 
793  void setBalance(int8 balance);
794 
798  Audio::Mixer::SoundType getSoundType() const { return _soundType; }
799 
803  void setSoundType(Audio::Mixer::SoundType soundType) { _soundType = soundType; }
804 
809  uint32 getRunningTime() const;
810 
814  void setMute(bool mute);
815 
816  protected:
817  void pauseIntern(bool shouldPause);
818 
822  virtual Audio::AudioStream *getAudioStream() const = 0;
823 
824  private:
825  Audio::SoundHandle _handle;
826  Audio::Mixer::SoundType _soundType;
827  byte _volume;
828  uint32 _rate;
829  int8 _balance;
830  bool _muted;
831  };
832 
838  public:
839  RewindableAudioTrack(Audio::Mixer::SoundType soundType) : AudioTrack(soundType) {}
840  virtual ~RewindableAudioTrack() {}
841 
842  bool isRewindable() const { return true; }
843  bool rewind();
844 
845  protected:
846  Audio::AudioStream *getAudioStream() const;
847 
852  virtual Audio::RewindableAudioStream *getRewindableAudioStream() const = 0;
853  };
854 
860  public:
861  SeekableAudioTrack(Audio::Mixer::SoundType soundType) : AudioTrack(soundType) {}
862  virtual ~SeekableAudioTrack() {}
863 
864  bool isSeekable() const { return true; }
865  bool seek(const Audio::Timestamp &time);
866 
867  Audio::Timestamp getDuration() const;
868 
869  protected:
870  Audio::AudioStream *getAudioStream() const;
871 
876  virtual Audio::SeekableAudioStream *getSeekableAudioStream() const = 0;
877  };
878 
884  public:
888 
894  bool loadFromFile(const Common::Path &baseName);
895 
896  protected:
899  };
900 
904  void resetPauseStartTime();
905 
913  virtual void readNextPacket() {}
914 
923  void addTrack(Track *track, bool isExternal = false);
924 
930  virtual bool useAudioSync() const { return true; }
931 
937  Track *getTrack(uint track);
938 
944  const Track *getTrack(uint track) const;
945 
952  bool endOfVideoTracks() const;
953 
959  VideoTrack *findNextVideoTrack();
960 
965  typedef TrackList::iterator TrackListIterator;
966 
970  TrackListIterator getTrackListBegin() { return _internalTracks.begin(); }
971 
975  TrackListIterator getTrackListEnd() { return _internalTracks.end(); }
976 
980  void eraseTrack(Track *track);
981 
989  virtual bool seekIntern(const Audio::Timestamp &time);
990 
998  virtual bool supportsAudioTrackSwitching() const { return false; }
999 
1008  virtual AudioTrack *getAudioTrack(int index) { return 0; }
1009 
1010  uint getNumTracks() { return _tracks.size(); }
1011 
1012 private:
1013  // Tracks owned by this VideoDecoder
1014  TrackList _tracks;
1015  TrackList _internalTracks;
1016  TrackList _externalTracks;
1017 
1018  // Current playback status
1019  bool _needsUpdate;
1020  Audio::Timestamp _endTime;
1021  bool _endTimeSet;
1022  Common::Rational _playbackRate;
1023 
1024  // Palette settings from individual tracks
1025  mutable bool _dirtyPalette;
1026  const byte *_palette;
1027 
1028  // Enforcement of not being able to set dither or set the default format
1029  bool _canSetDither;
1030  bool _canSetDefaultFormat;
1031 
1032 protected:
1033  // Internal helper functions
1034  void stopAudio();
1035  void setAudioRate(Common::Rational rate);
1036  void startAudio();
1037  void startAudioLimit(const Audio::Timestamp &limit);
1038  bool hasFramesLeft() const;
1039  bool hasAudio() const;
1040 
1041  Audio::Timestamp _lastTimeChange;
1042  int32 _startTime;
1043 
1044  VideoTrack *_nextVideoTrack;
1045 
1046  Image::CodecAccuracy _videoCodecAccuracy;
1047 
1048 private:
1049  uint32 _pauseLevel;
1050  uint32 _pauseStartTime;
1051  byte _audioVolume;
1052  int8 _audioBalance;
1053  Audio::Mixer::SoundType _soundType;
1054 
1055  AudioTrack *_mainAudioTrack;
1056 };
1057 
1058 } // End of namespace Video
1059 
1060 #endif
virtual AudioTrack * getAudioTrack(int index)
Definition: video_decoder.h:1008
byte getVolume() const
Definition: video_decoder.h:441
TrackListIterator getTrackListBegin()
Definition: video_decoder.h:970
Definition: video_decoder.h:511
byte getVolume() const
Definition: video_decoder.h:762
Definition: surface.h:67
virtual bool hasDirtyPalette() const
Definition: video_decoder.h:672
int8 getBalance() const
Definition: video_decoder.h:456
Definition: video_decoder.h:713
Common::Array< Track * > TrackList
Definition: video_decoder.h:964
Definition: pixelformat.h:138
virtual bool useAudioSync() const
Definition: video_decoder.h:930
virtual int getFrameCount() const
Definition: video_decoder.h:651
Definition: list.h:44
Definition: video_decoder.h:738
T * iterator
Definition: array.h:54
Definition: path.h:52
Definition: timestamp.h:83
virtual bool supportsAudioTrackSwitching() const
Definition: video_decoder.h:998
virtual bool isSeekable() const
Definition: video_decoder.h:556
Definition: stream.h:745
Definition: rational.h:40
Definition: video_decoder.h:883
virtual int getCurFrameDelay() const
Definition: video_decoder.h:640
Audio::Timestamp getEndTime() const
Definition: video_decoder.h:229
Definition: audiostream.h:212
virtual bool setReverse(bool reverse)
Definition: video_decoder.h:690
uint32 getRate() const
Definition: video_decoder.h:772
void setSoundType(Audio::Mixer::SoundType soundType)
Definition: video_decoder.h:803
Definition: mixer.h:49
SoundType
Definition: mixer.h:73
TrackType getTrackType() const
Definition: video_decoder.h:600
Definition: video_decoder.h:53
virtual void pauseIntern(bool shouldPause)
Definition: video_decoder.h:586
bool isPaused() const
Definition: video_decoder.h:573
Definition: algorithm.h:29
Definition: formatinfo.h:28
TrackType
Definition: video_decoder.h:519
Definition: audiostream.h:50
bool hasDirtyPalette() const
Definition: video_decoder.h:337
virtual void setDither(const byte *palette)
Definition: video_decoder.h:705
virtual void setCodecAccuracy(Image::CodecAccuracy accuracy)
Definition: video_decoder.h:626
Definition: video_decoder.h:837
virtual bool isReversed() const
Definition: video_decoder.h:695
bool isRewindable() const
Definition: video_decoder.h:842
TrackListIterator getTrackListEnd()
Definition: video_decoder.h:975
bool isSeekable() const
Definition: video_decoder.h:864
Definition: audiostream.h:109
virtual bool seek(const Audio::Timestamp &time)
Definition: video_decoder.h:563
virtual bool canDither() const
Definition: video_decoder.h:700
int8 getBalance() const
Definition: video_decoder.h:788
Audio::Mixer::SoundType getSoundType() const
Definition: video_decoder.h:798
virtual bool setOutputPixelFormat(const Graphics::PixelFormat &format)
Definition: video_decoder.h:621
virtual void readNextPacket()
Definition: video_decoder.h:913
bool isPaused() const
Definition: video_decoder.h:204
TrackType getTrackType() const
Definition: video_decoder.h:743
Audio::SeekableAudioStream * getSeekableAudioStream() const
Definition: video_decoder.h:898
Definition: animation.h:37
Definition: video_decoder.h:859
Common::Rational getRate() const
Definition: video_decoder.h:133
Definition: system.h:38
virtual const byte * getPalette() const
Definition: video_decoder.h:667
Definition: video_decoder.h:595