ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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  uint32 getFrameCount() const;
258 
273  uint32 getTime() const;
274 
275 
277  // Video Info
279 
289  virtual uint16 getWidth() const;
290 
300  virtual uint16 getHeight() const;
301 
305  Graphics::PixelFormat getPixelFormat() const;
306 
313  virtual Audio::Timestamp getDuration() const;
314 
315 
317  // Frame Decoding
319 
326  const byte *getPalette();
327 
331  bool hasDirtyPalette() const { return _dirtyPalette; }
332 
337  void delayMillis(uint msecs);
338 
342  uint32 getTimeToNextFrame() const;
343 
349  bool needsUpdate() const;
350 
366  virtual const Graphics::Surface *decodeNextFrame();
367 
378  bool setReverse(bool reverse);
379 
397  bool setDitheringPalette(const byte *palette);
398 
408  bool setOutputPixelFormat(const Graphics::PixelFormat &format);
409 
420  bool setOutputPixelFormats(const Common::List<Graphics::PixelFormat> &formatList);
421 
425  virtual void setVideoCodecAccuracy(Image::CodecAccuracy accuracy);
426 
428  // Audio Control
430 
435  byte getVolume() const { return _audioVolume; }
436 
444  void setVolume(byte volume);
445 
450  int8 getBalance() const { return _audioBalance; }
451 
459  void setBalance(int8 balance);
460 
464  Audio::Mixer::SoundType getSoundType() const;
465 
471  void setSoundType(Audio::Mixer::SoundType soundType);
472 
476  bool addStreamTrack(Audio::SeekableAudioStream *stream);
477 
483  bool addStreamFileTrack(const Common::Path &baseName);
484 
493  bool setAudioTrack(int index);
494 
498  uint getAudioTrackCount() const;
499 
500 protected:
505  class Track {
506  public:
507  Track();
508  virtual ~Track() {}
509 
513  enum TrackType {
514  kTrackTypeNone,
515  kTrackTypeVideo,
516  kTrackTypeAudio
517  };
518 
522  virtual TrackType getTrackType() const = 0;
523 
527  virtual bool endOfTrack() const = 0;
528 
535  virtual bool isRewindable() const;
536 
545  virtual bool rewind();
546 
550  virtual bool isSeekable() const { return false; }
551 
557  virtual bool seek(const Audio::Timestamp &time) { return false; }
558 
562  void pause(bool shouldPause);
563 
567  bool isPaused() const { return _paused; }
568 
574  virtual Audio::Timestamp getDuration() const;
575 
576  protected:
580  virtual void pauseIntern(bool shouldPause) {}
581 
582  private:
583  bool _paused;
584  };
585 
589  class VideoTrack : public Track {
590  public:
591  VideoTrack() {}
592  virtual ~VideoTrack() {}
593 
594  TrackType getTrackType() const { return kTrackTypeVideo; }
595  virtual bool endOfTrack() const;
596 
600  virtual uint16 getWidth() const = 0;
601 
605  virtual uint16 getHeight() const = 0;
606 
610  virtual Graphics::PixelFormat getPixelFormat() const = 0;
611 
615  virtual bool setOutputPixelFormat(const Graphics::PixelFormat &format) { return format == getPixelFormat(); }
616 
620  virtual void setCodecAccuracy(Image::CodecAccuracy accuracy) {}
621 
627  virtual int getCurFrame() const = 0;
628 
636  virtual int getFrameCount() const { return 0; }
637 
642  virtual uint32 getNextFrameStartTime() const = 0;
643 
647  virtual const Graphics::Surface *decodeNextFrame() = 0;
648 
652  virtual const byte *getPalette() const { return 0; }
653 
657  virtual bool hasDirtyPalette() const { return false; }
658 
665  virtual Audio::Timestamp getFrameTime(uint frame) const;
666 
675  virtual bool setReverse(bool reverse) { return !reverse; }
676 
680  virtual bool isReversed() const { return false; }
681 
685  virtual bool canDither() const { return false; }
686 
690  virtual void setDither(const byte *palette) {}
691  };
692 
699  public:
701  virtual ~FixedRateVideoTrack() {}
702 
703  uint32 getNextFrameStartTime() const;
704  virtual Audio::Timestamp getDuration() const;
705  Audio::Timestamp getFrameTime(uint frame) const;
706 
711  uint getFrameAtTime(const Audio::Timestamp &time) const;
712 
713  protected:
717  virtual Common::Rational getFrameRate() const = 0;
718  };
719 
723  class AudioTrack : public Track {
724  public:
726  virtual ~AudioTrack() {}
727 
728  TrackType getTrackType() const { return kTrackTypeAudio; }
729 
730  virtual bool endOfTrack() const;
731 
735  void start();
736 
740  void stop();
741 
742  void start(const Audio::Timestamp &limit);
743 
747  byte getVolume() const { return _volume; }
748 
752  void setVolume(byte volume);
753 
757  uint32 getRate() const { return _rate; }
758 
762  void setRate(uint32 rate);
763 
768  void setRate(Common::Rational rate);
769 
773  int8 getBalance() const { return _balance; }
774 
778  void setBalance(int8 balance);
779 
783  Audio::Mixer::SoundType getSoundType() const { return _soundType; }
784 
788  void setSoundType(Audio::Mixer::SoundType soundType) { _soundType = soundType; }
789 
794  uint32 getRunningTime() const;
795 
799  void setMute(bool mute);
800 
801  protected:
802  void pauseIntern(bool shouldPause);
803 
807  virtual Audio::AudioStream *getAudioStream() const = 0;
808 
809  private:
810  Audio::SoundHandle _handle;
811  Audio::Mixer::SoundType _soundType;
812  byte _volume;
813  uint32 _rate;
814  int8 _balance;
815  bool _muted;
816  };
817 
823  public:
824  RewindableAudioTrack(Audio::Mixer::SoundType soundType) : AudioTrack(soundType) {}
825  virtual ~RewindableAudioTrack() {}
826 
827  bool isRewindable() const { return true; }
828  bool rewind();
829 
830  protected:
831  Audio::AudioStream *getAudioStream() const;
832 
837  virtual Audio::RewindableAudioStream *getRewindableAudioStream() const = 0;
838  };
839 
845  public:
846  SeekableAudioTrack(Audio::Mixer::SoundType soundType) : AudioTrack(soundType) {}
847  virtual ~SeekableAudioTrack() {}
848 
849  bool isSeekable() const { return true; }
850  bool seek(const Audio::Timestamp &time);
851 
852  Audio::Timestamp getDuration() const;
853 
854  protected:
855  Audio::AudioStream *getAudioStream() const;
856 
861  virtual Audio::SeekableAudioStream *getSeekableAudioStream() const = 0;
862  };
863 
869  public:
873 
879  bool loadFromFile(const Common::Path &baseName);
880 
881  protected:
884  };
885 
889  void resetPauseStartTime();
890 
898  virtual void readNextPacket() {}
899 
908  void addTrack(Track *track, bool isExternal = false);
909 
915  virtual bool useAudioSync() const { return true; }
916 
922  Track *getTrack(uint track);
923 
929  const Track *getTrack(uint track) const;
930 
937  bool endOfVideoTracks() const;
938 
944  VideoTrack *findNextVideoTrack();
945 
950  typedef TrackList::iterator TrackListIterator;
951 
955  TrackListIterator getTrackListBegin() { return _internalTracks.begin(); }
956 
960  TrackListIterator getTrackListEnd() { return _internalTracks.end(); }
961 
965  void eraseTrack(Track *track);
966 
974  virtual bool seekIntern(const Audio::Timestamp &time);
975 
983  virtual bool supportsAudioTrackSwitching() const { return false; }
984 
993  virtual AudioTrack *getAudioTrack(int index) { return 0; }
994 
995  uint getNumTracks() { return _tracks.size(); }
996 
997 private:
998  // Tracks owned by this VideoDecoder
999  TrackList _tracks;
1000  TrackList _internalTracks;
1001  TrackList _externalTracks;
1002 
1003  // Current playback status
1004  bool _needsUpdate;
1005  Audio::Timestamp _endTime;
1006  bool _endTimeSet;
1007  Common::Rational _playbackRate;
1008 
1009  // Palette settings from individual tracks
1010  mutable bool _dirtyPalette;
1011  const byte *_palette;
1012 
1013  // Enforcement of not being able to set dither or set the default format
1014  bool _canSetDither;
1015  bool _canSetDefaultFormat;
1016 
1017 protected:
1018  // Internal helper functions
1019  void stopAudio();
1020  void setAudioRate(Common::Rational rate);
1021  void startAudio();
1022  void startAudioLimit(const Audio::Timestamp &limit);
1023  bool hasFramesLeft() const;
1024  bool hasAudio() const;
1025 
1026  Audio::Timestamp _lastTimeChange;
1027  int32 _startTime;
1028 
1029  VideoTrack *_nextVideoTrack;
1030 
1031  Image::CodecAccuracy _videoCodecAccuracy;
1032 
1033 private:
1034  uint32 _pauseLevel;
1035  uint32 _pauseStartTime;
1036  byte _audioVolume;
1037  int8 _audioBalance;
1038  Audio::Mixer::SoundType _soundType;
1039 
1040  AudioTrack *_mainAudioTrack;
1041 };
1042 
1043 } // End of namespace Video
1044 
1045 #endif
virtual AudioTrack * getAudioTrack(int index)
Definition: video_decoder.h:993
byte getVolume() const
Definition: video_decoder.h:435
TrackListIterator getTrackListBegin()
Definition: video_decoder.h:955
Definition: video_decoder.h:505
byte getVolume() const
Definition: video_decoder.h:747
Definition: surface.h:67
virtual bool hasDirtyPalette() const
Definition: video_decoder.h:657
int8 getBalance() const
Definition: video_decoder.h:450
Definition: video_decoder.h:698
Common::Array< Track * > TrackList
Definition: video_decoder.h:949
Definition: pixelformat.h:138
virtual bool useAudioSync() const
Definition: video_decoder.h:915
virtual int getFrameCount() const
Definition: video_decoder.h:636
Definition: list.h:44
Definition: video_decoder.h:723
T * iterator
Definition: array.h:54
Definition: path.h:52
Definition: timestamp.h:83
virtual bool supportsAudioTrackSwitching() const
Definition: video_decoder.h:983
virtual bool isSeekable() const
Definition: video_decoder.h:550
Definition: stream.h:745
Definition: rational.h:40
Definition: video_decoder.h:868
Audio::Timestamp getEndTime() const
Definition: video_decoder.h:229
Definition: audiostream.h:212
virtual bool setReverse(bool reverse)
Definition: video_decoder.h:675
uint32 getRate() const
Definition: video_decoder.h:757
void setSoundType(Audio::Mixer::SoundType soundType)
Definition: video_decoder.h:788
Definition: mixer.h:49
SoundType
Definition: mixer.h:62
TrackType getTrackType() const
Definition: video_decoder.h:594
Definition: video_decoder.h:53
virtual void pauseIntern(bool shouldPause)
Definition: video_decoder.h:580
bool isPaused() const
Definition: video_decoder.h:567
Definition: algorithm.h:29
Definition: formatinfo.h:28
TrackType
Definition: video_decoder.h:513
Definition: audiostream.h:50
bool hasDirtyPalette() const
Definition: video_decoder.h:331
virtual void setDither(const byte *palette)
Definition: video_decoder.h:690
virtual void setCodecAccuracy(Image::CodecAccuracy accuracy)
Definition: video_decoder.h:620
Definition: video_decoder.h:822
virtual bool isReversed() const
Definition: video_decoder.h:680
bool isRewindable() const
Definition: video_decoder.h:827
TrackListIterator getTrackListEnd()
Definition: video_decoder.h:960
bool isSeekable() const
Definition: video_decoder.h:849
Definition: audiostream.h:109
virtual bool seek(const Audio::Timestamp &time)
Definition: video_decoder.h:557
virtual bool canDither() const
Definition: video_decoder.h:685
int8 getBalance() const
Definition: video_decoder.h:773
Audio::Mixer::SoundType getSoundType() const
Definition: video_decoder.h:783
virtual bool setOutputPixelFormat(const Graphics::PixelFormat &format)
Definition: video_decoder.h:615
virtual void readNextPacket()
Definition: video_decoder.h:898
bool isPaused() const
Definition: video_decoder.h:204
TrackType getTrackType() const
Definition: video_decoder.h:728
Audio::SeekableAudioStream * getSeekableAudioStream() const
Definition: video_decoder.h:883
Definition: avi_frames.h:36
Definition: video_decoder.h:844
Common::Rational getRate() const
Definition: video_decoder.h:133
Definition: system.h:38
virtual const byte * getPalette() const
Definition: video_decoder.h:652
Definition: video_decoder.h:589