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 
33 namespace Audio {
34 class AudioStream;
35 class RewindableAudioStream;
36 class SeekableAudioStream;
37 }
38 
39 namespace Common {
40 class SeekableReadStream;
41 }
42 
43 namespace Graphics {
44 struct Surface;
45 }
46 
47 namespace Video {
48 
52 class VideoDecoder {
53 public:
54  VideoDecoder();
55  virtual ~VideoDecoder() {}
56 
58  // Opening/Closing a Video
60 
69  virtual bool loadFile(const Common::Path &filename);
70 
82  virtual bool loadStream(Common::SeekableReadStream *stream) = 0;
83 
90  virtual void close();
91 
95  bool isVideoLoaded() const;
96 
97 
99  // Playback Control
101 
107  void start();
108 
114  void stop();
115 
126  void setRate(const Common::Rational &rate);
127 
132  Common::Rational getRate() const { return _playbackRate; }
133 
142  bool isPlaying() const;
143 
148  virtual bool isRewindable() const;
149 
158  virtual bool rewind();
159 
164  virtual bool isSeekable() const;
165 
177  bool seek(const Audio::Timestamp &time);
178 
185  virtual bool seekToFrame(uint frame);
186 
198  void pauseVideo(bool pause);
199 
203  bool isPaused() const { return _pauseLevel != 0; }
204 
212  void setEndTime(const Audio::Timestamp &endTime);
213 
223  void setEndFrame(uint frame);
224 
228  Audio::Timestamp getEndTime() const { return _endTime; }
229 
230 
232  // Playback Status
234 
239  bool endOfVideo() const;
240 
245  int getCurFrame() const;
246 
251  uint32 getFrameCount() const;
252 
267  uint32 getTime() const;
268 
269 
271  // Video Info
273 
283  virtual uint16 getWidth() const;
284 
294  virtual uint16 getHeight() const;
295 
299  Graphics::PixelFormat getPixelFormat() const;
300 
307  virtual Audio::Timestamp getDuration() const;
308 
309 
311  // Frame Decoding
313 
320  const byte *getPalette();
321 
325  bool hasDirtyPalette() const { return _dirtyPalette; }
326 
330  uint32 getTimeToNextFrame() const;
331 
337  bool needsUpdate() const;
338 
354  virtual const Graphics::Surface *decodeNextFrame();
355 
366  bool setReverse(bool reverse);
367 
385  bool setDitheringPalette(const byte *palette);
386 
396  bool setOutputPixelFormat(const Graphics::PixelFormat &format);
397 
399  // Audio Control
401 
406  byte getVolume() const { return _audioVolume; }
407 
415  void setVolume(byte volume);
416 
421  int8 getBalance() const { return _audioBalance; }
422 
430  void setBalance(int8 balance);
431 
435  Audio::Mixer::SoundType getSoundType() const;
436 
442  void setSoundType(Audio::Mixer::SoundType soundType);
443 
447  bool addStreamTrack(Audio::SeekableAudioStream *stream);
448 
454  bool addStreamFileTrack(const Common::Path &baseName);
455 
464  bool setAudioTrack(int index);
465 
469  uint getAudioTrackCount() const;
470 
471 protected:
476  class Track {
477  public:
478  Track();
479  virtual ~Track() {}
480 
484  enum TrackType {
485  kTrackTypeNone,
486  kTrackTypeVideo,
487  kTrackTypeAudio
488  };
489 
493  virtual TrackType getTrackType() const = 0;
494 
498  virtual bool endOfTrack() const = 0;
499 
506  virtual bool isRewindable() const;
507 
516  virtual bool rewind();
517 
521  virtual bool isSeekable() const { return false; }
522 
528  virtual bool seek(const Audio::Timestamp &time) { return false; }
529 
533  void pause(bool shouldPause);
534 
538  bool isPaused() const { return _paused; }
539 
545  virtual Audio::Timestamp getDuration() const;
546 
547  protected:
551  virtual void pauseIntern(bool shouldPause) {}
552 
553  private:
554  bool _paused;
555  };
556 
560  class VideoTrack : public Track {
561  public:
562  VideoTrack() {}
563  virtual ~VideoTrack() {}
564 
565  TrackType getTrackType() const { return kTrackTypeVideo; }
566  virtual bool endOfTrack() const;
567 
571  virtual uint16 getWidth() const = 0;
572 
576  virtual uint16 getHeight() const = 0;
577 
581  virtual Graphics::PixelFormat getPixelFormat() const = 0;
582 
586  virtual bool setOutputPixelFormat(const Graphics::PixelFormat &format) { return false; }
587 
593  virtual int getCurFrame() const = 0;
594 
602  virtual int getFrameCount() const { return 0; }
603 
608  virtual uint32 getNextFrameStartTime() const = 0;
609 
613  virtual const Graphics::Surface *decodeNextFrame() = 0;
614 
618  virtual const byte *getPalette() const { return 0; }
619 
623  virtual bool hasDirtyPalette() const { return false; }
624 
631  virtual Audio::Timestamp getFrameTime(uint frame) const;
632 
641  virtual bool setReverse(bool reverse) { return !reverse; }
642 
646  virtual bool isReversed() const { return false; }
647 
651  virtual bool canDither() const { return false; }
652 
656  virtual void setDither(const byte *palette) {}
657  };
658 
665  public:
667  virtual ~FixedRateVideoTrack() {}
668 
669  uint32 getNextFrameStartTime() const;
670  virtual Audio::Timestamp getDuration() const;
671  Audio::Timestamp getFrameTime(uint frame) const;
672 
677  uint getFrameAtTime(const Audio::Timestamp &time) const;
678 
679  protected:
683  virtual Common::Rational getFrameRate() const = 0;
684  };
685 
689  class AudioTrack : public Track {
690  public:
692  virtual ~AudioTrack() {}
693 
694  TrackType getTrackType() const { return kTrackTypeAudio; }
695 
696  virtual bool endOfTrack() const;
697 
701  void start();
702 
706  void stop();
707 
708  void start(const Audio::Timestamp &limit);
709 
713  byte getVolume() const { return _volume; }
714 
718  void setVolume(byte volume);
719 
723  uint32 getRate() const { return _rate; }
724 
728  void setRate(uint32 rate);
729 
734  void setRate(Common::Rational rate);
735 
739  int8 getBalance() const { return _balance; }
740 
744  void setBalance(int8 balance);
745 
749  Audio::Mixer::SoundType getSoundType() const { return _soundType; }
750 
754  void setSoundType(Audio::Mixer::SoundType soundType) { _soundType = soundType; }
755 
760  uint32 getRunningTime() const;
761 
765  void setMute(bool mute);
766 
767  protected:
768  void pauseIntern(bool shouldPause);
769 
773  virtual Audio::AudioStream *getAudioStream() const = 0;
774 
775  private:
776  Audio::SoundHandle _handle;
777  Audio::Mixer::SoundType _soundType;
778  byte _volume;
779  uint32 _rate;
780  int8 _balance;
781  bool _muted;
782  };
783 
789  public:
790  RewindableAudioTrack(Audio::Mixer::SoundType soundType) : AudioTrack(soundType) {}
791  virtual ~RewindableAudioTrack() {}
792 
793  bool isRewindable() const { return true; }
794  bool rewind();
795 
796  protected:
797  Audio::AudioStream *getAudioStream() const;
798 
803  virtual Audio::RewindableAudioStream *getRewindableAudioStream() const = 0;
804  };
805 
811  public:
812  SeekableAudioTrack(Audio::Mixer::SoundType soundType) : AudioTrack(soundType) {}
813  virtual ~SeekableAudioTrack() {}
814 
815  bool isSeekable() const { return true; }
816  bool seek(const Audio::Timestamp &time);
817 
818  Audio::Timestamp getDuration() const;
819 
820  protected:
821  Audio::AudioStream *getAudioStream() const;
822 
827  virtual Audio::SeekableAudioStream *getSeekableAudioStream() const = 0;
828  };
829 
835  public:
839 
845  bool loadFromFile(const Common::Path &baseName);
846 
847  protected:
850  };
851 
855  void resetPauseStartTime();
856 
864  virtual void readNextPacket() {}
865 
874  void addTrack(Track *track, bool isExternal = false);
875 
881  virtual bool useAudioSync() const { return true; }
882 
888  Track *getTrack(uint track);
889 
895  const Track *getTrack(uint track) const;
896 
903  bool endOfVideoTracks() const;
904 
910  VideoTrack *findNextVideoTrack();
911 
916  typedef TrackList::iterator TrackListIterator;
917 
921  TrackListIterator getTrackListBegin() { return _internalTracks.begin(); }
922 
926  TrackListIterator getTrackListEnd() { return _internalTracks.end(); }
927 
931  void eraseTrack(Track *track);
932 
940  virtual bool seekIntern(const Audio::Timestamp &time);
941 
949  virtual bool supportsAudioTrackSwitching() const { return false; }
950 
959  virtual AudioTrack *getAudioTrack(int index) { return 0; }
960 
961 private:
962  // Tracks owned by this VideoDecoder
963  TrackList _tracks;
964  TrackList _internalTracks;
965  TrackList _externalTracks;
966 
967  // Current playback status
968  bool _needsUpdate;
969  Audio::Timestamp _endTime;
970  bool _endTimeSet;
971  Common::Rational _playbackRate;
972  VideoTrack *_nextVideoTrack;
973 
974  // Palette settings from individual tracks
975  mutable bool _dirtyPalette;
976  const byte *_palette;
977 
978  // Enforcement of not being able to set dither or set the default format
979  bool _canSetDither;
980  bool _canSetDefaultFormat;
981 
982 protected:
983  // Internal helper functions
984  void stopAudio();
985  void setAudioRate(Common::Rational rate);
986  void startAudio();
987  void startAudioLimit(const Audio::Timestamp &limit);
988  bool hasFramesLeft() const;
989  bool hasAudio() const;
990 
991  Audio::Timestamp _lastTimeChange;
992  int32 _startTime;
993 
994 private:
995  uint32 _pauseLevel;
996  uint32 _pauseStartTime;
997  byte _audioVolume;
998  int8 _audioBalance;
999  Audio::Mixer::SoundType _soundType;
1000 
1001  AudioTrack *_mainAudioTrack;
1002 };
1003 
1004 } // End of namespace Video
1005 
1006 #endif
virtual AudioTrack * getAudioTrack(int index)
Definition: video_decoder.h:959
byte getVolume() const
Definition: video_decoder.h:406
TrackListIterator getTrackListBegin()
Definition: video_decoder.h:921
Definition: video_decoder.h:476
byte getVolume() const
Definition: video_decoder.h:713
Definition: surface.h:66
virtual bool hasDirtyPalette() const
Definition: video_decoder.h:623
int8 getBalance() const
Definition: video_decoder.h:421
Definition: video_decoder.h:664
Common::Array< Track * > TrackList
Definition: video_decoder.h:915
Definition: pixelformat.h:138
virtual bool useAudioSync() const
Definition: video_decoder.h:881
virtual int getFrameCount() const
Definition: video_decoder.h:602
Definition: video_decoder.h:689
T * iterator
Definition: array.h:54
Definition: path.h:52
Definition: timestamp.h:83
virtual bool supportsAudioTrackSwitching() const
Definition: video_decoder.h:949
virtual bool isSeekable() const
Definition: video_decoder.h:521
Definition: stream.h:745
Definition: rational.h:40
Definition: video_decoder.h:834
Audio::Timestamp getEndTime() const
Definition: video_decoder.h:228
Definition: audiostream.h:212
virtual bool setReverse(bool reverse)
Definition: video_decoder.h:641
uint32 getRate() const
Definition: video_decoder.h:723
void setSoundType(Audio::Mixer::SoundType soundType)
Definition: video_decoder.h:754
Definition: mixer.h:49
SoundType
Definition: mixer.h:62
TrackType getTrackType() const
Definition: video_decoder.h:565
Definition: video_decoder.h:52
virtual void pauseIntern(bool shouldPause)
Definition: video_decoder.h:551
bool isPaused() const
Definition: video_decoder.h:538
Definition: algorithm.h:29
Definition: formatinfo.h:28
TrackType
Definition: video_decoder.h:484
Definition: audiostream.h:50
bool hasDirtyPalette() const
Definition: video_decoder.h:325
virtual void setDither(const byte *palette)
Definition: video_decoder.h:656
Definition: video_decoder.h:788
virtual bool isReversed() const
Definition: video_decoder.h:646
bool isRewindable() const
Definition: video_decoder.h:793
TrackListIterator getTrackListEnd()
Definition: video_decoder.h:926
bool isSeekable() const
Definition: video_decoder.h:815
Definition: audiostream.h:109
virtual bool seek(const Audio::Timestamp &time)
Definition: video_decoder.h:528
virtual bool canDither() const
Definition: video_decoder.h:651
int8 getBalance() const
Definition: video_decoder.h:739
Audio::Mixer::SoundType getSoundType() const
Definition: video_decoder.h:749
virtual bool setOutputPixelFormat(const Graphics::PixelFormat &format)
Definition: video_decoder.h:586
virtual void readNextPacket()
Definition: video_decoder.h:864
bool isPaused() const
Definition: video_decoder.h:203
TrackType getTrackType() const
Definition: video_decoder.h:694
Audio::SeekableAudioStream * getSeekableAudioStream() const
Definition: video_decoder.h:849
Definition: avi_frames.h:36
Definition: video_decoder.h:810
Common::Rational getRate() const
Definition: video_decoder.h:132
Definition: system.h:37
virtual const byte * getPalette() const
Definition: video_decoder.h:618
Definition: video_decoder.h:560