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  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 
413  virtual void setVideoCodecAccuracy(Image::CodecAccuracy accuracy);
414 
416  // Audio Control
418 
423  byte getVolume() const { return _audioVolume; }
424 
432  void setVolume(byte volume);
433 
438  int8 getBalance() const { return _audioBalance; }
439 
447  void setBalance(int8 balance);
448 
452  Audio::Mixer::SoundType getSoundType() const;
453 
459  void setSoundType(Audio::Mixer::SoundType soundType);
460 
464  bool addStreamTrack(Audio::SeekableAudioStream *stream);
465 
471  bool addStreamFileTrack(const Common::Path &baseName);
472 
481  bool setAudioTrack(int index);
482 
486  uint getAudioTrackCount() const;
487 
488 protected:
493  class Track {
494  public:
495  Track();
496  virtual ~Track() {}
497 
501  enum TrackType {
502  kTrackTypeNone,
503  kTrackTypeVideo,
504  kTrackTypeAudio
505  };
506 
510  virtual TrackType getTrackType() const = 0;
511 
515  virtual bool endOfTrack() const = 0;
516 
523  virtual bool isRewindable() const;
524 
533  virtual bool rewind();
534 
538  virtual bool isSeekable() const { return false; }
539 
545  virtual bool seek(const Audio::Timestamp &time) { return false; }
546 
550  void pause(bool shouldPause);
551 
555  bool isPaused() const { return _paused; }
556 
562  virtual Audio::Timestamp getDuration() const;
563 
564  protected:
568  virtual void pauseIntern(bool shouldPause) {}
569 
570  private:
571  bool _paused;
572  };
573 
577  class VideoTrack : public Track {
578  public:
579  VideoTrack() {}
580  virtual ~VideoTrack() {}
581 
582  TrackType getTrackType() const { return kTrackTypeVideo; }
583  virtual bool endOfTrack() const;
584 
588  virtual uint16 getWidth() const = 0;
589 
593  virtual uint16 getHeight() const = 0;
594 
598  virtual Graphics::PixelFormat getPixelFormat() const = 0;
599 
603  virtual bool setOutputPixelFormat(const Graphics::PixelFormat &format) { return false; }
604 
608  virtual void setCodecAccuracy(Image::CodecAccuracy accuracy) {}
609 
615  virtual int getCurFrame() const = 0;
616 
624  virtual int getFrameCount() const { return 0; }
625 
630  virtual uint32 getNextFrameStartTime() const = 0;
631 
635  virtual const Graphics::Surface *decodeNextFrame() = 0;
636 
640  virtual const byte *getPalette() const { return 0; }
641 
645  virtual bool hasDirtyPalette() const { return false; }
646 
653  virtual Audio::Timestamp getFrameTime(uint frame) const;
654 
663  virtual bool setReverse(bool reverse) { return !reverse; }
664 
668  virtual bool isReversed() const { return false; }
669 
673  virtual bool canDither() const { return false; }
674 
678  virtual void setDither(const byte *palette) {}
679  };
680 
687  public:
689  virtual ~FixedRateVideoTrack() {}
690 
691  uint32 getNextFrameStartTime() const;
692  virtual Audio::Timestamp getDuration() const;
693  Audio::Timestamp getFrameTime(uint frame) const;
694 
699  uint getFrameAtTime(const Audio::Timestamp &time) const;
700 
701  protected:
705  virtual Common::Rational getFrameRate() const = 0;
706  };
707 
711  class AudioTrack : public Track {
712  public:
714  virtual ~AudioTrack() {}
715 
716  TrackType getTrackType() const { return kTrackTypeAudio; }
717 
718  virtual bool endOfTrack() const;
719 
723  void start();
724 
728  void stop();
729 
730  void start(const Audio::Timestamp &limit);
731 
735  byte getVolume() const { return _volume; }
736 
740  void setVolume(byte volume);
741 
745  uint32 getRate() const { return _rate; }
746 
750  void setRate(uint32 rate);
751 
756  void setRate(Common::Rational rate);
757 
761  int8 getBalance() const { return _balance; }
762 
766  void setBalance(int8 balance);
767 
771  Audio::Mixer::SoundType getSoundType() const { return _soundType; }
772 
776  void setSoundType(Audio::Mixer::SoundType soundType) { _soundType = soundType; }
777 
782  uint32 getRunningTime() const;
783 
787  void setMute(bool mute);
788 
789  protected:
790  void pauseIntern(bool shouldPause);
791 
795  virtual Audio::AudioStream *getAudioStream() const = 0;
796 
797  private:
798  Audio::SoundHandle _handle;
799  Audio::Mixer::SoundType _soundType;
800  byte _volume;
801  uint32 _rate;
802  int8 _balance;
803  bool _muted;
804  };
805 
811  public:
812  RewindableAudioTrack(Audio::Mixer::SoundType soundType) : AudioTrack(soundType) {}
813  virtual ~RewindableAudioTrack() {}
814 
815  bool isRewindable() const { return true; }
816  bool rewind();
817 
818  protected:
819  Audio::AudioStream *getAudioStream() const;
820 
825  virtual Audio::RewindableAudioStream *getRewindableAudioStream() const = 0;
826  };
827 
833  public:
834  SeekableAudioTrack(Audio::Mixer::SoundType soundType) : AudioTrack(soundType) {}
835  virtual ~SeekableAudioTrack() {}
836 
837  bool isSeekable() const { return true; }
838  bool seek(const Audio::Timestamp &time);
839 
840  Audio::Timestamp getDuration() const;
841 
842  protected:
843  Audio::AudioStream *getAudioStream() const;
844 
849  virtual Audio::SeekableAudioStream *getSeekableAudioStream() const = 0;
850  };
851 
857  public:
861 
867  bool loadFromFile(const Common::Path &baseName);
868 
869  protected:
872  };
873 
877  void resetPauseStartTime();
878 
886  virtual void readNextPacket() {}
887 
896  void addTrack(Track *track, bool isExternal = false);
897 
903  virtual bool useAudioSync() const { return true; }
904 
910  Track *getTrack(uint track);
911 
917  const Track *getTrack(uint track) const;
918 
925  bool endOfVideoTracks() const;
926 
932  VideoTrack *findNextVideoTrack();
933 
938  typedef TrackList::iterator TrackListIterator;
939 
943  TrackListIterator getTrackListBegin() { return _internalTracks.begin(); }
944 
948  TrackListIterator getTrackListEnd() { return _internalTracks.end(); }
949 
953  void eraseTrack(Track *track);
954 
962  virtual bool seekIntern(const Audio::Timestamp &time);
963 
971  virtual bool supportsAudioTrackSwitching() const { return false; }
972 
981  virtual AudioTrack *getAudioTrack(int index) { return 0; }
982 
983 private:
984  // Tracks owned by this VideoDecoder
985  TrackList _tracks;
986  TrackList _internalTracks;
987  TrackList _externalTracks;
988 
989  // Current playback status
990  bool _needsUpdate;
991  Audio::Timestamp _endTime;
992  bool _endTimeSet;
993  Common::Rational _playbackRate;
994 
995  // Palette settings from individual tracks
996  mutable bool _dirtyPalette;
997  const byte *_palette;
998 
999  // Enforcement of not being able to set dither or set the default format
1000  bool _canSetDither;
1001  bool _canSetDefaultFormat;
1002 
1003 protected:
1004  // Internal helper functions
1005  void stopAudio();
1006  void setAudioRate(Common::Rational rate);
1007  void startAudio();
1008  void startAudioLimit(const Audio::Timestamp &limit);
1009  bool hasFramesLeft() const;
1010  bool hasAudio() const;
1011 
1012  Audio::Timestamp _lastTimeChange;
1013  int32 _startTime;
1014 
1015  VideoTrack *_nextVideoTrack;
1016 
1017  Image::CodecAccuracy _videoCodecAccuracy;
1018 
1019 private:
1020  uint32 _pauseLevel;
1021  uint32 _pauseStartTime;
1022  byte _audioVolume;
1023  int8 _audioBalance;
1024  Audio::Mixer::SoundType _soundType;
1025 
1026  AudioTrack *_mainAudioTrack;
1027 };
1028 
1029 } // End of namespace Video
1030 
1031 #endif
virtual AudioTrack * getAudioTrack(int index)
Definition: video_decoder.h:981
byte getVolume() const
Definition: video_decoder.h:423
TrackListIterator getTrackListBegin()
Definition: video_decoder.h:943
Definition: video_decoder.h:493
byte getVolume() const
Definition: video_decoder.h:735
Definition: surface.h:67
virtual bool hasDirtyPalette() const
Definition: video_decoder.h:645
int8 getBalance() const
Definition: video_decoder.h:438
Definition: video_decoder.h:686
Common::Array< Track * > TrackList
Definition: video_decoder.h:937
Definition: pixelformat.h:138
virtual bool useAudioSync() const
Definition: video_decoder.h:903
virtual int getFrameCount() const
Definition: video_decoder.h:624
Definition: video_decoder.h:711
T * iterator
Definition: array.h:54
Definition: path.h:52
Definition: timestamp.h:83
virtual bool supportsAudioTrackSwitching() const
Definition: video_decoder.h:971
virtual bool isSeekable() const
Definition: video_decoder.h:538
Definition: stream.h:745
Definition: rational.h:40
Definition: video_decoder.h:856
Audio::Timestamp getEndTime() const
Definition: video_decoder.h:229
Definition: audiostream.h:212
virtual bool setReverse(bool reverse)
Definition: video_decoder.h:663
uint32 getRate() const
Definition: video_decoder.h:745
void setSoundType(Audio::Mixer::SoundType soundType)
Definition: video_decoder.h:776
Definition: mixer.h:49
SoundType
Definition: mixer.h:62
TrackType getTrackType() const
Definition: video_decoder.h:582
Definition: video_decoder.h:53
virtual void pauseIntern(bool shouldPause)
Definition: video_decoder.h:568
bool isPaused() const
Definition: video_decoder.h:555
Definition: algorithm.h:29
Definition: formatinfo.h:28
TrackType
Definition: video_decoder.h:501
Definition: audiostream.h:50
bool hasDirtyPalette() const
Definition: video_decoder.h:331
virtual void setDither(const byte *palette)
Definition: video_decoder.h:678
virtual void setCodecAccuracy(Image::CodecAccuracy accuracy)
Definition: video_decoder.h:608
Definition: video_decoder.h:810
virtual bool isReversed() const
Definition: video_decoder.h:668
bool isRewindable() const
Definition: video_decoder.h:815
TrackListIterator getTrackListEnd()
Definition: video_decoder.h:948
bool isSeekable() const
Definition: video_decoder.h:837
Definition: audiostream.h:109
virtual bool seek(const Audio::Timestamp &time)
Definition: video_decoder.h:545
virtual bool canDither() const
Definition: video_decoder.h:673
int8 getBalance() const
Definition: video_decoder.h:761
Audio::Mixer::SoundType getSoundType() const
Definition: video_decoder.h:771
virtual bool setOutputPixelFormat(const Graphics::PixelFormat &format)
Definition: video_decoder.h:603
virtual void readNextPacket()
Definition: video_decoder.h:886
bool isPaused() const
Definition: video_decoder.h:204
TrackType getTrackType() const
Definition: video_decoder.h:716
Audio::SeekableAudioStream * getSeekableAudioStream() const
Definition: video_decoder.h:871
Definition: avi_frames.h:36
Definition: video_decoder.h:832
Common::Rational getRate() const
Definition: video_decoder.h:133
Definition: system.h:38
virtual const byte * getPalette() const
Definition: video_decoder.h:640
Definition: video_decoder.h:577