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
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (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, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef VIDEO_DECODER_H
24 #define VIDEO_DECODER_H
25 
26 #include "audio/mixer.h"
27 #include "audio/timestamp.h"	// TODO: Move this to common/ ?
28 #include "common/array.h"
29 #include "common/path.h"
30 #include "common/rational.h"
31 #include "common/str.h"
32 #include "graphics/pixelformat.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 
50 /**
51  * Generic interface for video decoder classes.
52  */
53 class VideoDecoder {
54 public:
55 	VideoDecoder();
~VideoDecoder()56 	virtual ~VideoDecoder() {}
57 
58 	/////////////////////////////////////////
59 	// Opening/Closing a Video
60 	/////////////////////////////////////////
61 
62 	/**
63 	 * Load a video from a file with the given name.
64 	 *
65 	 * A default implementation using Common::File and loadStream is provided.
66 	 *
67 	 * @param filename	the filename to load
68 	 * @return whether loading the file succeeded
69 	 */
70 	virtual bool loadFile(const Common::Path &filename);
71 
72 	/**
73 	 * Load a video from a generic read stream. The ownership of the
74 	 * stream object transfers to this VideoDecoder instance, which is
75 	 * hence also responsible for eventually deleting it.
76 	 *
77 	 * Implementations of this function are required to call addTrack()
78 	 * for each track in the video upon success.
79 	 *
80 	 * @param stream  the stream to load
81 	 * @return whether loading the stream succeeded
82 	 */
83 	virtual bool loadStream(Common::SeekableReadStream *stream) = 0;
84 
85 	/**
86 	 * Close the active video stream and free any associated resources.
87 	 *
88 	 * All subclasses that need to close their own resources should still
89 	 * call the base class' close() function at the start of their function.
90 	 */
91 	virtual void close();
92 
93 	/**
94 	 * Returns if a video stream is currently loaded or not.
95 	 */
96 	bool isVideoLoaded() const;
97 
98 
99 	/////////////////////////////////////////
100 	// Playback Control
101 	/////////////////////////////////////////
102 
103 	/**
104 	 * Begin playback of the video at normal speed.
105 	 *
106 	 * @note This has no effect if the video is already playing.
107 	 */
108 	void start();
109 
110 	/**
111 	 * Stop playback of the video.
112 	 *
113 	 * @note This has no effect if the video is not playing.
114 	 */
115 	void stop();
116 
117 	/**
118 	 * Set the rate of playback.
119 	 *
120 	 * For instance, a rate of 0 would stop the video, while a rate of 1
121 	 * would play the video normally. Passing 2 to this function would
122 	 * play the video at twice the normal speed.
123 	 *
124 	 * @note This function does not work for non-0/1 rates on videos that
125 	 * have audio tracks.
126 	 *
127 	 * @todo This currently does not implement backwards playback, but will
128 	 * be implemented soon.
129 	 */
130 	void setRate(const Common::Rational &rate);
131 
132 	/**
133 	 * Returns the rate at which the video is being played.
134 	 */
getRate()135 	Common::Rational getRate() const { return _playbackRate; }
136 
137 	/**
138 	 * Returns if the video is currently playing or not.
139 	 *
140 	 * This is not equivalent to the inverse of endOfVideo(). A video keeps
141 	 * its playing status even after reaching the end of the video. This will
142 	 * return true after calling start() and will continue to return true
143 	 * until stop() (or close()) is called.
144 	 */
145 	bool isPlaying() const;
146 
147 	/**
148 	 * Returns if a video is rewindable or not. The default implementation
149 	 * polls each track for rewindability.
150 	 */
151 	virtual bool isRewindable() const;
152 
153 	/**
154 	 * Rewind a video to its beginning.
155 	 *
156 	 * If the video is playing, it will continue to play. The default
157 	 * implementation will rewind each track.
158 	 *
159 	 * @return true on success, false otherwise
160 	 */
161 	virtual bool rewind();
162 
163 	/**
164 	 * Returns if a video is seekable or not. The default implementation
165 	 * polls each track for seekability.
166 	 */
167 	virtual bool isSeekable() const;
168 
169 	/**
170 	 * Seek to a given time in the video.
171 	 *
172 	 * If the video is playing, it will continue to play. This calls
173 	 * seekIntern(), which can be overriden. By default, seekIntern()
174 	 * will call Track::seek() on all tracks with the time passed to
175 	 * this function.
176 	 *
177 	 * @param time The time to seek to
178 	 * @return true on success, false otherwise
179 	 */
180 	bool seek(const Audio::Timestamp &time);
181 
182 	/**
183 	 * Seek to a given frame.
184 	 *
185 	 * This only works when one video track is present, and that track
186 	 * supports getFrameTime(). This calls seek() internally.
187 	 */
188 	virtual bool seekToFrame(uint frame);
189 
190 	/**
191 	 * Pause or resume the video. This should stop/resume any audio playback
192 	 * and other stuff. The initial pause time is kept so that any timing
193 	 * variables can be updated appropriately.
194 	 *
195 	 * This is a convenience method which automatically keeps track on how
196 	 * often the video has been paused, ensuring that after pausing an video
197 	 * e.g. twice, it has to be unpaused twice before actuallying resuming.
198 	 *
199 	 * @param pause		true to pause the video, false to resume it
200 	 */
201 	void pauseVideo(bool pause);
202 
203 	/**
204 	 * Return whether the video is currently paused or not.
205 	 */
isPaused()206 	bool isPaused() const { return _pauseLevel != 0; }
207 
208 	/**
209 	 * Set the time for this video to end at. At this time in the video,
210 	 * all audio will stop and endOfVideo() will return true.
211 	 *
212 	 * While the setting is stored even if a video is not playing,
213 	 * endOfVideo() is only affected when the video is playing.
214 	 */
215 	void setEndTime(const Audio::Timestamp &endTime);
216 
217 	/**
218 	 * Set the end frame.
219 	 *
220 	 * The passed frame will be the last frame to show.
221 	 *
222 	 * Like seekToFrame(), this only works when one video track is present,
223 	 * and that track supports getFrameTime(). This calls setEndTime()
224 	 * internally.
225 	 */
226 	void setEndFrame(uint frame);
227 
228 	/**
229 	 * Get the stop time of the video (if not set, zero)
230 	 */
getEndTime()231 	Audio::Timestamp getEndTime() const { return _endTime; }
232 
233 
234 	/////////////////////////////////////////
235 	// Playback Status
236 	/////////////////////////////////////////
237 
238 	/**
239 	 * Returns if the video has reached the end or not.
240 	 * @return true if the video has finished playing or if none is loaded, false otherwise
241 	 */
242 	bool endOfVideo() const;
243 
244 	/**
245 	 * Returns the current frame number of the video.
246 	 * @return the last frame decoded by the video
247 	 */
248 	int getCurFrame() const;
249 
250 	/**
251 	 * Returns the number of frames in the video.
252 	 * @return the number of frames in the video
253 	 */
254 	uint32 getFrameCount() const;
255 
256 	/**
257 	 * Returns the time position (in ms) of the current video.
258 	 * This can be based on the "wall clock" time as determined by
259 	 * OSystem::getMillis() or the current time of any audio track
260 	 * running in the video, and takes pausing the video into account.
261 	 *
262 	 * As such, it will differ from what multiplying getCurFrame() by
263 	 * some constant would yield, e.g. for a video with non-constant
264 	 * frame rate.
265 	 *
266 	 * Due to the nature of the timing, this value may not always be
267 	 * completely accurate (since our mixer does not have precise
268 	 * timing).
269 	 */
270 	uint32 getTime() const;
271 
272 
273 	/////////////////////////////////////////
274 	// Video Info
275 	/////////////////////////////////////////
276 
277 	/**
278 	 * Returns the width of the video's frames.
279 	 *
280 	 * By default, this finds the largest width between all of the loaded
281 	 * tracks. However, a subclass may override this if it does any kind
282 	 * of post-processing on it.
283 	 *
284 	 * @return the width of the video's frames
285 	 */
286 	virtual uint16 getWidth() const;
287 
288 	/**
289 	 * Returns the height of the video's frames.
290 	 *
291 	 * By default, this finds the largest height between all of the loaded
292 	 * tracks. However, a subclass may override this if it does any kind
293 	 * of post-processing on it.
294 	 *
295 	 * @return the height of the video's frames
296 	 */
297 	virtual uint16 getHeight() const;
298 
299 	/**
300 	 * Get the pixel format of the currently loaded video.
301 	 */
302 	Graphics::PixelFormat getPixelFormat() const;
303 
304 	/**
305 	 * Get the duration of the video.
306 	 *
307 	 * If the duration is unknown, this will return 0. If this is not
308 	 * overriden, it will take the length of the longest track.
309 	 */
310 	virtual Audio::Timestamp getDuration() const;
311 
312 
313 	/////////////////////////////////////////
314 	// Frame Decoding
315 	/////////////////////////////////////////
316 
317 	/**
318 	 * Get the palette for the video in RGB format (if 8bpp or less).
319 	 *
320 	 * The palette's format is the same as PaletteManager's palette
321 	 * (interleaved RGB values).
322 	 */
323 	const byte *getPalette();
324 
325 	/**
326 	 * Returns if the palette is dirty or not.
327 	 */
hasDirtyPalette()328 	bool hasDirtyPalette() const { return _dirtyPalette; }
329 
330 	/**
331 	 * Return the time (in ms) until the next frame should be displayed.
332 	 */
333 	uint32 getTimeToNextFrame() const;
334 
335 	/**
336 	 * Check whether a new frame should be decoded, i.e. because enough
337 	 * time has elapsed since the last frame was decoded.
338 	 * @return whether a new frame should be decoded or not
339 	 */
340 	bool needsUpdate() const;
341 
342 	/**
343 	 * Decode the next frame into a surface and return the latter.
344 	 *
345 	 * A subclass may override this, but must still call this function. As an
346 	 * example, a subclass may do this to apply some global video scale to
347 	 * individual track's frame.
348 	 *
349 	 * Note that this will call readNextPacket() internally first before calling
350 	 * the next video track's decodeNextFrame() function.
351 	 *
352 	 * @return a surface containing the decoded frame, or 0
353 	 * @note Ownership of the returned surface stays with the VideoDecoder,
354 	 *       hence the caller must *not* free it.
355 	 * @note this may return 0, in which case the last frame should be kept on screen
356 	 */
357 	virtual const Graphics::Surface *decodeNextFrame();
358 
359 	/**
360 	 * Set the default high color format for videos that convert from YUV.
361 	 *
362 	 * By default, VideoDecoder will attempt to use the screen format
363 	 * if it's >8bpp and use a 32bpp format when not.
364 	 *
365 	 * This must be set before calling loadStream().
366 	 */
setDefaultHighColorFormat(const Graphics::PixelFormat & format)367 	void setDefaultHighColorFormat(const Graphics::PixelFormat &format) { _defaultHighColorFormat = format; }
368 
369 	/**
370 	 * Set the video to decode frames in reverse.
371 	 *
372 	 * By default, VideoDecoder will decode forward.
373 	 *
374 	 * @note This is used by setRate()
375 	 * @note This will not work if an audio track is present
376 	 * @param reverse true for reverse, false for forward
377 	 * @return true on success, false otherwise
378 	 */
379 	bool setReverse(bool reverse);
380 
381 	/**
382 	 * Tell the video to dither to a palette.
383 	 *
384 	 * By default, VideoDecoder will return surfaces in native, or in the case
385 	 * of YUV-based videos, the format set by setDefaultHighColorFormat().
386 	 * For video formats or codecs that support it, this will start outputting
387 	 * its surfaces in 8bpp with this palette.
388 	 *
389 	 * This should be called after loadStream(), but before a decodeNextFrame()
390 	 * call. This is enforced.
391 	 *
392 	 * The palette will be copied, so you do not need to worry about the pointer
393 	 * going out-of-scope.
394 	 *
395 	 * @param palette The palette to use for dithering
396 	 * @return true on success, false otherwise
397 	 */
398 	bool setDitheringPalette(const byte *palette);
399 
400 	/////////////////////////////////////////
401 	// Audio Control
402 	/////////////////////////////////////////
403 
404 	/**
405 	 * Get the current volume at which the audio in the video is being played
406 	 * @return the current volume at which the audio in the video is being played
407 	 */
getVolume()408 	byte getVolume() const { return _audioVolume; }
409 
410 	/**
411 	 * Set the volume at which the audio in the video should be played.
412 	 * This setting remains until close() is called (which may be called
413 	 * from loadStream()). The default volume is the maximum.
414 	 *
415 	 * @param volume The volume at which to play the audio in the video
416 	 */
417 	void setVolume(byte volume);
418 
419 	/**
420 	 * Get the current balance at which the audio in the video is being played
421 	 * @return the current balance at which the audio in the video is being played
422 	 */
getBalance()423 	int8 getBalance() const { return _audioBalance; }
424 
425 	/**
426 	 * Set the balance at which the audio in the video should be played.
427 	 * This setting remains until close() is called (which may be called
428 	 * from loadStream()). The default balance is 0.
429 	 *
430 	 * @param balance The balance at which to play the audio in the video
431 	 */
432 	void setBalance(int8 balance);
433 
434 	/**
435 	 * Get the mixer sound type audio is being played with.
436 	 */
437 	Audio::Mixer::SoundType getSoundType() const;
438 
439 	/**
440 	 * Set the mixer sound type used to play the audio tracks.
441 	 *
442 	 * This must be set before calling loadStream().
443 	 */
444 	void setSoundType(Audio::Mixer::SoundType soundType);
445 
446 	/**
447 	 * Add an audio track from a stream file.
448 	 *
449 	 * This calls SeekableAudioStream::openStreamFile() internally
450 	 */
451 	bool addStreamFileTrack(const Common::String &baseName);
452 
453 	/**
454 	 * Set the internal audio track.
455 	 *
456 	 * Has no effect if the container does not support this.
457 	 * @see supportsAudioTrackSwitching()
458 	 *
459 	 * @param index The index of the track, whose meaning is dependent on the container
460 	 */
461 	bool setAudioTrack(int index);
462 
463 	/**
464 	 * Get the number of internal audio tracks.
465 	 */
466 	uint getAudioTrackCount() const;
467 
468 protected:
469 	/**
470 	 * An abstract representation of a track in a movie. Since tracks here are designed
471 	 * to work independently, they should not reference any other track(s) in the video.
472 	 */
473 	class Track {
474 	public:
475 		Track();
~Track()476 		virtual ~Track() {}
477 
478 		/**
479 		 * The types of tracks this class can be.
480 		 */
481 		enum TrackType {
482 			kTrackTypeNone,
483 			kTrackTypeVideo,
484 			kTrackTypeAudio
485 		};
486 
487 		/**
488 		 * Get the type of track.
489 		 */
490 		virtual TrackType getTrackType() const = 0;
491 
492 		/**
493 		 * Return if the track has finished.
494 		 */
495 		virtual bool endOfTrack() const = 0;
496 
497 		/**
498 		 * Return if the track is rewindable.
499 		 *
500 		 * If a video is seekable, it does not need to implement this
501 		 * for it to also be rewindable.
502 		 */
503 		virtual bool isRewindable() const;
504 
505 		/**
506 		 * Rewind the video to the beginning.
507 		 *
508 		 * If a video is seekable, it does not need to implement this
509 		 * for it to also be rewindable.
510 		 *
511 		 * @return true on success, false otherwise.
512 		 */
513 		virtual bool rewind();
514 
515 		/**
516 		 * Return if the track is seekable.
517 		 */
isSeekable()518 		virtual bool isSeekable() const { return false; }
519 
520 		/**
521 		 * Seek to the given time.
522 		 * @param time The time to seek to, from the beginning of the video.
523 		 * @return true on success, false otherwise.
524 		 */
seek(const Audio::Timestamp & time)525 		virtual bool seek(const Audio::Timestamp &time) { return false; }
526 
527 		/**
528 		 * Set the pause status of the track.
529 		 */
530 		void pause(bool shouldPause);
531 
532 		/**
533 		 * Return if the track is paused.
534 		 */
isPaused()535 		bool isPaused() const { return _paused; }
536 
537 		/**
538 		 * Get the duration of the track (starting from this track's start time).
539 		 *
540 		 * By default, this returns 0 for unknown.
541 		 */
542 		virtual Audio::Timestamp getDuration() const;
543 
544 	protected:
545 		/**
546 		 * Function called by pause() for subclasses to implement.
547 		 */
pauseIntern(bool shouldPause)548 		virtual void pauseIntern(bool shouldPause) {}
549 
550 	private:
551 		bool _paused;
552 	};
553 
554 	/**
555 	 * An abstract representation of a video track.
556 	 */
557 	class VideoTrack : public Track {
558 	public:
VideoTrack()559 		VideoTrack() {}
~VideoTrack()560 		virtual ~VideoTrack() {}
561 
getTrackType()562 		TrackType getTrackType() const  { return kTrackTypeVideo; }
563 		virtual bool endOfTrack() const;
564 
565 		/**
566 		 * Get the width of this track
567 		 */
568 		virtual uint16 getWidth() const = 0;
569 
570 		/**
571 		 * Get the height of this track
572 		 */
573 		virtual uint16 getHeight() const = 0;
574 
575 		/**
576 		 * Get the pixel format of this track
577 		 */
578 		virtual Graphics::PixelFormat getPixelFormat() const = 0;
579 
580 		/**
581 		 * Get the current frame of this track
582 		 *
583 		 * @see VideoDecoder::getCurFrame()
584 		 */
585 		virtual int getCurFrame() const = 0;
586 
587 		/**
588 		 * Get the frame count of this track
589 		 *
590 		 * @note If the frame count is unknown, return 0 (which is also
591 		 * the default implementation of the function). However, one must
592 		 * also implement endOfTrack() in that case.
593 		 */
getFrameCount()594 		virtual int getFrameCount() const { return 0; }
595 
596 		/**
597 		 * Get the start time of the next frame in milliseconds since
598 		 * the start of the video
599 		 */
600 		virtual uint32 getNextFrameStartTime() const = 0;
601 
602 		/**
603 		 * Decode the next frame
604 		 */
605 		virtual const Graphics::Surface *decodeNextFrame() = 0;
606 
607 		/**
608 		 * Get the palette currently in use by this track
609 		 */
getPalette()610 		virtual const byte *getPalette() const { return 0; }
611 
612 		/**
613 		 * Does the palette currently in use by this track need to be updated?
614 		 */
hasDirtyPalette()615 		virtual bool hasDirtyPalette() const { return false; }
616 
617 		/**
618 		 * Get the time the given frame should be shown.
619 		 *
620 		 * By default, this returns a negative (invalid) value. This function
621 		 * should only be used by VideoDecoder::seekToFrame().
622 		 */
623 		virtual Audio::Timestamp getFrameTime(uint frame) const;
624 
625 		/**
626 		 * Set the video track to play in reverse or forward.
627 		 *
628 		 * By default, a VideoTrack must decode forward.
629 		 *
630 		 * @param reverse true for reverse, false for forward
631 		 * @return true for success, false for failure
632 		 */
setReverse(bool reverse)633 		virtual bool setReverse(bool reverse) { return !reverse; }
634 
635 		/**
636 		 * Is the video track set to play in reverse?
637 		 */
isReversed()638 		virtual bool isReversed() const { return false; }
639 
640 		/**
641 		 * Can the video track dither?
642 		 */
canDither()643 		virtual bool canDither() const { return false; }
644 
645 		/**
646 		 * Activate dithering mode with a palette
647 		 */
setDither(const byte * palette)648 		virtual void setDither(const byte *palette) {}
649 	};
650 
651 	/**
652 	 * A VideoTrack that is played at a constant rate.
653 	 *
654 	 * If the frame count is unknown, you must override endOfTrack().
655 	 */
656 	class FixedRateVideoTrack : public VideoTrack {
657 	public:
FixedRateVideoTrack()658 		FixedRateVideoTrack() {}
~FixedRateVideoTrack()659 		virtual ~FixedRateVideoTrack() {}
660 
661 		uint32 getNextFrameStartTime() const;
662 		virtual Audio::Timestamp getDuration() const;
663 		Audio::Timestamp getFrameTime(uint frame) const;
664 
665 		/**
666 		 * Get the frame that should be displaying at the given time. This is
667 		 * helpful for someone implementing seek().
668 		 */
669 		uint getFrameAtTime(const Audio::Timestamp &time) const;
670 
671 	protected:
672 		/**
673 		 * Get the rate at which this track is played.
674 		 */
675 		virtual Common::Rational getFrameRate() const = 0;
676 	};
677 
678 	/**
679 	 * An abstract representation of an audio track.
680 	 */
681 	class AudioTrack : public Track {
682 	public:
683 		AudioTrack(Audio::Mixer::SoundType soundType);
~AudioTrack()684 		virtual ~AudioTrack() {}
685 
getTrackType()686 		TrackType getTrackType() const { return kTrackTypeAudio; }
687 
688 		virtual bool endOfTrack() const;
689 
690 		/**
691 		 * Start playing this track
692 		 */
693 		void start();
694 
695 		/**
696 		 * Stop playing this track
697 		 */
698 		void stop();
699 
700 		void start(const Audio::Timestamp &limit);
701 
702 		/**
703 		 * Get the volume for this track
704 		 */
getVolume()705 		byte getVolume() const { return _volume; }
706 
707 		/**
708 		 * Set the volume for this track
709 		 */
710 		void setVolume(byte volume);
711 
712 		/**
713 		 * Get the balance for this track
714 		 */
getBalance()715 		int8 getBalance() const { return _balance; }
716 
717 		/**
718 		 * Set the balance for this track
719 		 */
720 		void setBalance(int8 balance);
721 
722 		/**
723 		 * Get the sound type for this track
724 		 */
getSoundType()725 		Audio::Mixer::SoundType getSoundType() const { return _soundType; }
726 
727 		/**
728 		 * Set the sound type for this track
729 		 */
setSoundType(Audio::Mixer::SoundType soundType)730 		void setSoundType(Audio::Mixer::SoundType soundType) { _soundType = soundType; }
731 
732 		/**
733 		 * Get the time the AudioStream behind this track has been
734 		 * running
735 		 */
736 		uint32 getRunningTime() const;
737 
738 		/**
739 		 * Mute the track
740 		 */
741 		void setMute(bool mute);
742 
743 	protected:
744 		void pauseIntern(bool shouldPause);
745 
746 		/**
747 		 * Get the AudioStream that is the representation of this AudioTrack
748 		 */
749 		virtual Audio::AudioStream *getAudioStream() const = 0;
750 
751 	private:
752 		Audio::SoundHandle _handle;
753 		Audio::Mixer::SoundType _soundType;
754 		byte _volume;
755 		int8 _balance;
756 		bool _muted;
757 	};
758 
759 	/**
760 	 * An AudioTrack that implements isRewindable() and rewind() using
761 	 * RewindableAudioStream.
762 	 */
763 	class RewindableAudioTrack : public AudioTrack {
764 	public:
RewindableAudioTrack(Audio::Mixer::SoundType soundType)765 		RewindableAudioTrack(Audio::Mixer::SoundType soundType) : AudioTrack(soundType) {}
~RewindableAudioTrack()766 		virtual ~RewindableAudioTrack() {}
767 
isRewindable()768 		bool isRewindable() const { return true; }
769 		bool rewind();
770 
771 	protected:
772 		Audio::AudioStream *getAudioStream() const;
773 
774 		/**
775 		 * Get the RewindableAudioStream pointer to be used by this class
776 		 * for rewind() and getAudioStream()
777 		 */
778 		virtual Audio::RewindableAudioStream *getRewindableAudioStream() const = 0;
779 	};
780 
781 	/**
782 	 * An AudioTrack that implements isSeekable() and seek() using
783 	 * SeekableAudioStream.
784 	 */
785 	class SeekableAudioTrack : public AudioTrack {
786 	public:
SeekableAudioTrack(Audio::Mixer::SoundType soundType)787 		SeekableAudioTrack(Audio::Mixer::SoundType soundType) : AudioTrack(soundType) {}
~SeekableAudioTrack()788 		virtual ~SeekableAudioTrack() {}
789 
isSeekable()790 		bool isSeekable() const { return true; }
791 		bool seek(const Audio::Timestamp &time);
792 
793 		Audio::Timestamp getDuration() const;
794 
795 	protected:
796 		Audio::AudioStream *getAudioStream() const;
797 
798 		/**
799 		 * Get the SeekableAudioStream pointer to be used by this class
800 		 * for seek(), getDuration(), and getAudioStream()
801 		 */
802 		virtual Audio::SeekableAudioStream *getSeekableAudioStream() const = 0;
803 	};
804 
805 	/**
806 	 * A SeekableAudioTrack that constructs its SeekableAudioStream using
807 	 * SeekableAudioStream::openStreamFile()
808 	 */
809 	class StreamFileAudioTrack : public SeekableAudioTrack {
810 	public:
811 		StreamFileAudioTrack(Audio::Mixer::SoundType soundType);
812 		~StreamFileAudioTrack();
813 
814 		/**
815 		 * Load the track from a file with the given base name.
816 		 *
817 		 * @return true on success, false otherwise
818 		 */
819 		bool loadFromFile(const Common::String &baseName);
820 
821 	protected:
822 		Audio::SeekableAudioStream *_stream;
getSeekableAudioStream()823 		Audio::SeekableAudioStream *getSeekableAudioStream() const { return _stream; }
824 	};
825 
826 	/**
827 	 * Reset the pause start time (which should be called when seeking)
828 	 */
829 	void resetPauseStartTime();
830 
831 	/**
832 	 * Decode enough data for the next frame and enough audio to last that long.
833 	 *
834 	 * This function is used by this class' decodeNextFrame() function. A subclass
835 	 * of a Track may decide to just have its decodeNextFrame() function read
836 	 * and decode the frame, but only if it is the only track in the video.
837 	 */
readNextPacket()838 	virtual void readNextPacket() {}
839 
840 	/**
841 	 * Define a track to be used by this class.
842 	 *
843 	 * The pointer is then owned by this base class.
844 	 *
845 	 * @param track The track to add
846 	 * @param isExternal Is this an external track not found by loadStream()?
847 	 */
848 	void addTrack(Track *track, bool isExternal = false);
849 
850 	/**
851 	 * Whether or not getTime() will sync with a playing audio track.
852 	 *
853 	 * A subclass can override this to disable this feature.
854 	 */
useAudioSync()855 	virtual bool useAudioSync() const { return true; }
856 
857 	/**
858 	 * Get the given track based on its index.
859 	 *
860 	 * @return A valid track pointer on success, 0 otherwise
861 	 */
862 	Track *getTrack(uint track);
863 
864 	/**
865 	 * Get the given track based on its index
866 	 *
867 	 * @return A valid track pointer on success, 0 otherwise
868 	 */
869 	const Track *getTrack(uint track) const;
870 
871 	/**
872 	 * Find out if all video tracks have finished
873 	 *
874 	 * This is useful if one wants to figure out if they need to buffer all
875 	 * remaining audio in a file.
876 	 */
877 	bool endOfVideoTracks() const;
878 
879 	/**
880 	 * Get the default high color format
881 	 */
getDefaultHighColorFormat()882 	Graphics::PixelFormat getDefaultHighColorFormat() const { return _defaultHighColorFormat; }
883 
884 	/**
885 	 * Set _nextVideoTrack to the video track with the lowest start time for the next frame.
886 	 *
887 	 * @return _nextVideoTrack
888 	 */
889 	VideoTrack *findNextVideoTrack();
890 
891 	/**
892 	 * Typedef helpers for accessing tracks
893 	 */
894 	typedef Common::Array<Track *> TrackList;
895 	typedef TrackList::iterator TrackListIterator;
896 
897 	/**
898 	 * Get the begin iterator of the tracks
899 	 */
getTrackListBegin()900 	TrackListIterator getTrackListBegin() { return _internalTracks.begin(); }
901 
902 	/**
903 	 * Get the end iterator of the tracks
904 	 */
getTrackListEnd()905 	TrackListIterator getTrackListEnd() { return _internalTracks.end(); }
906 
907 	/**
908 	 * Removes a specified track
909 	 */
910 	void eraseTrack(Track *track);
911 
912 	/**
913 	 * The internal seek function that does the actual seeking.
914 	 *
915 	 * @see seek()
916 	 *
917 	 * @return true on success, false otherwise
918 	 */
919 	virtual bool seekIntern(const Audio::Timestamp &time);
920 
921 	/**
922 	 * Does this video format support switching between audio tracks?
923 	 *
924 	 * Returning true implies this format supports multiple audio tracks,
925 	 * can switch tracks, and defaults to playing the first found audio
926 	 * track.
927 	 */
supportsAudioTrackSwitching()928 	virtual bool supportsAudioTrackSwitching() const { return false; }
929 
930 	/**
931 	 * Get the audio track for the given index.
932 	 *
933 	 * This is used only if supportsAudioTrackSwitching() returns true.
934 	 *
935 	 * @param index The index of the track, whose meaning is dependent on the container
936 	 * @return The audio track for the index, or 0 if not found
937 	 */
getAudioTrack(int index)938 	virtual AudioTrack *getAudioTrack(int index) { return 0; }
939 
940 private:
941 	// Tracks owned by this VideoDecoder
942 	TrackList _tracks;
943 	TrackList _internalTracks;
944 	TrackList _externalTracks;
945 
946 	// Current playback status
947 	bool _needsUpdate;
948 	Audio::Timestamp _endTime;
949 	bool _endTimeSet;
950 	Common::Rational _playbackRate;
951 	VideoTrack *_nextVideoTrack;
952 
953 	// Palette settings from individual tracks
954 	mutable bool _dirtyPalette;
955 	const byte *_palette;
956 
957 	// Enforcement of not being able to set dither
958 	bool _canSetDither;
959 
960 	// Default PixelFormat settings
961 	Graphics::PixelFormat _defaultHighColorFormat;
962 
963 protected:
964 	// Internal helper functions
965 	void stopAudio();
966 	void startAudio();
967 	void startAudioLimit(const Audio::Timestamp &limit);
968 	bool hasFramesLeft() const;
969 	bool hasAudio() const;
970 
971 	Audio::Timestamp _lastTimeChange;
972 	int32 _startTime;
973 
974 private:
975 	uint32 _pauseLevel;
976 	uint32 _pauseStartTime;
977 	byte _audioVolume;
978 	int8 _audioBalance;
979 	Audio::Mixer::SoundType _soundType;
980 
981 	AudioTrack *_mainAudioTrack;
982 };
983 
984 } // End of namespace Video
985 
986 #endif
987