2010-05-18 14:17:24 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-01-23 17:37:17 +00:00
|
|
|
#include "video/video_decoder.h"
|
2010-05-18 14:17:24 +00:00
|
|
|
|
2012-07-21 00:51:42 +00:00
|
|
|
#include "audio/audiostream.h"
|
2012-05-28 01:18:32 +00:00
|
|
|
#include "audio/mixer.h" // for kMaxChannelVolume
|
|
|
|
|
2011-04-24 08:34:27 +00:00
|
|
|
#include "common/rational.h"
|
2010-05-18 14:17:24 +00:00
|
|
|
#include "common/file.h"
|
|
|
|
#include "common/system.h"
|
|
|
|
|
2011-04-24 08:34:27 +00:00
|
|
|
#include "graphics/palette.h"
|
|
|
|
|
2011-01-23 19:08:09 +00:00
|
|
|
namespace Video {
|
2010-05-18 14:17:24 +00:00
|
|
|
|
|
|
|
VideoDecoder::VideoDecoder() {
|
2012-08-16 16:27:05 +00:00
|
|
|
_startTime = 0;
|
2012-08-16 18:00:14 +00:00
|
|
|
_dirtyPalette = false;
|
|
|
|
_palette = 0;
|
|
|
|
_isPlaying = false;
|
|
|
|
_audioVolume = Audio::Mixer::kMaxChannelVolume;
|
|
|
|
_audioBalance = 0;
|
2012-08-16 16:27:05 +00:00
|
|
|
_pauseLevel = 0;
|
2012-08-16 18:00:14 +00:00
|
|
|
_needsUpdate = false;
|
|
|
|
_lastTimeChange = 0;
|
|
|
|
_endTime = 0;
|
|
|
|
_endTimeSet = false;
|
|
|
|
|
|
|
|
// Find the best format for output
|
|
|
|
_defaultHighColorFormat = g_system->getScreenFormat();
|
|
|
|
|
|
|
|
if (_defaultHighColorFormat.bytesPerPixel == 1)
|
|
|
|
_defaultHighColorFormat = Graphics::PixelFormat(4, 8, 8, 8, 8, 8, 16, 24, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoDecoder::close() {
|
|
|
|
if (isPlaying())
|
|
|
|
stop();
|
|
|
|
|
|
|
|
for (TrackList::iterator it = _tracks.begin(); it != _tracks.end(); it++)
|
|
|
|
delete *it;
|
|
|
|
|
|
|
|
_tracks.clear();
|
|
|
|
_dirtyPalette = false;
|
|
|
|
_palette = 0;
|
|
|
|
_startTime = 0;
|
2012-08-16 16:27:05 +00:00
|
|
|
_audioVolume = Audio::Mixer::kMaxChannelVolume;
|
|
|
|
_audioBalance = 0;
|
2012-08-16 18:00:14 +00:00
|
|
|
_pauseLevel = 0;
|
|
|
|
_needsUpdate = false;
|
|
|
|
_lastTimeChange = 0;
|
|
|
|
_endTime = 0;
|
|
|
|
_endTimeSet = false;
|
2010-05-18 14:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool VideoDecoder::loadFile(const Common::String &filename) {
|
|
|
|
Common::File *file = new Common::File();
|
|
|
|
|
|
|
|
if (!file->open(filename)) {
|
|
|
|
delete file;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-02-07 17:54:16 +00:00
|
|
|
return loadStream(file);
|
2010-05-18 14:17:24 +00:00
|
|
|
}
|
|
|
|
|
2010-07-21 18:17:51 +00:00
|
|
|
bool VideoDecoder::needsUpdate() const {
|
2012-09-01 01:47:52 +00:00
|
|
|
return hasFramesLeft() && getTimeToNextFrame() == 0;
|
2010-05-18 14:17:24 +00:00
|
|
|
}
|
|
|
|
|
2010-05-20 18:38:06 +00:00
|
|
|
void VideoDecoder::pauseVideo(bool pause) {
|
|
|
|
if (pause) {
|
|
|
|
_pauseLevel++;
|
2010-07-30 21:07:27 +00:00
|
|
|
|
|
|
|
// We can't go negative
|
|
|
|
} else if (_pauseLevel) {
|
2010-05-20 18:38:06 +00:00
|
|
|
_pauseLevel--;
|
2010-07-30 21:07:27 +00:00
|
|
|
|
|
|
|
// Do nothing
|
|
|
|
} else {
|
|
|
|
return;
|
2010-05-20 18:38:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_pauseLevel == 1 && pause) {
|
|
|
|
_pauseStartTime = g_system->getMillis(); // Store the starting time from pausing to keep it for later
|
2012-08-16 18:00:14 +00:00
|
|
|
|
|
|
|
for (TrackList::iterator it = _tracks.begin(); it != _tracks.end(); it++)
|
|
|
|
(*it)->pause(true);
|
2010-05-20 18:38:06 +00:00
|
|
|
} else if (_pauseLevel == 0) {
|
2012-08-16 18:00:14 +00:00
|
|
|
for (TrackList::iterator it = _tracks.begin(); it != _tracks.end(); it++)
|
|
|
|
(*it)->pause(false);
|
|
|
|
|
2012-08-16 16:27:05 +00:00
|
|
|
_startTime += (g_system->getMillis() - _pauseStartTime);
|
2010-05-20 18:38:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-14 13:01:57 +00:00
|
|
|
void VideoDecoder::resetPauseStartTime() {
|
|
|
|
if (isPaused())
|
|
|
|
_pauseStartTime = g_system->getMillis();
|
|
|
|
}
|
|
|
|
|
2012-05-28 01:18:32 +00:00
|
|
|
void VideoDecoder::setVolume(byte volume) {
|
|
|
|
_audioVolume = volume;
|
2012-08-16 18:00:14 +00:00
|
|
|
|
|
|
|
for (TrackList::iterator it = _tracks.begin(); it != _tracks.end(); it++)
|
|
|
|
if ((*it)->getTrackType() == Track::kTrackTypeAudio)
|
|
|
|
((AudioTrack *)*it)->setVolume(_audioVolume);
|
2012-05-28 01:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VideoDecoder::setBalance(int8 balance) {
|
|
|
|
_audioBalance = balance;
|
2012-07-21 00:51:42 +00:00
|
|
|
|
|
|
|
for (TrackList::iterator it = _tracks.begin(); it != _tracks.end(); it++)
|
2012-08-16 18:00:14 +00:00
|
|
|
if ((*it)->getTrackType() == Track::kTrackTypeAudio)
|
|
|
|
((AudioTrack *)*it)->setBalance(_audioBalance);
|
2012-07-21 00:51:42 +00:00
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
bool VideoDecoder::isVideoLoaded() const {
|
2012-07-21 00:51:42 +00:00
|
|
|
return !_tracks.empty();
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
uint16 VideoDecoder::getWidth() const {
|
2012-07-21 16:40:16 +00:00
|
|
|
for (TrackList::const_iterator it = _tracks.begin(); it != _tracks.end(); it++)
|
|
|
|
if ((*it)->getTrackType() == Track::kTrackTypeVideo)
|
|
|
|
return ((VideoTrack *)*it)->getWidth();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
uint16 VideoDecoder::getHeight() const {
|
2012-07-21 16:40:16 +00:00
|
|
|
for (TrackList::const_iterator it = _tracks.begin(); it != _tracks.end(); it++)
|
|
|
|
if ((*it)->getTrackType() == Track::kTrackTypeVideo)
|
|
|
|
return ((VideoTrack *)*it)->getHeight();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
Graphics::PixelFormat VideoDecoder::getPixelFormat() const {
|
2012-07-21 16:40:16 +00:00
|
|
|
for (TrackList::const_iterator it = _tracks.begin(); it != _tracks.end(); it++)
|
|
|
|
if ((*it)->getTrackType() == Track::kTrackTypeVideo)
|
|
|
|
return ((VideoTrack *)*it)->getPixelFormat();
|
|
|
|
|
|
|
|
return Graphics::PixelFormat();
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
const Graphics::Surface *VideoDecoder::decodeNextFrame() {
|
2012-07-27 03:42:35 +00:00
|
|
|
_needsUpdate = false;
|
|
|
|
|
2012-07-21 00:51:42 +00:00
|
|
|
readNextPacket();
|
|
|
|
VideoTrack *track = findNextVideoTrack();
|
|
|
|
|
|
|
|
if (!track)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const Graphics::Surface *frame = track->decodeNextFrame();
|
|
|
|
|
|
|
|
if (track->hasDirtyPalette()) {
|
|
|
|
_palette = track->getPalette();
|
|
|
|
_dirtyPalette = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
const byte *VideoDecoder::getPalette() {
|
2012-07-21 00:51:42 +00:00
|
|
|
_dirtyPalette = false;
|
|
|
|
return _palette;
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
int VideoDecoder::getCurFrame() const {
|
2012-07-21 00:51:42 +00:00
|
|
|
int32 frame = -1;
|
|
|
|
|
|
|
|
for (TrackList::const_iterator it = _tracks.begin(); it != _tracks.end(); it++)
|
|
|
|
if ((*it)->getTrackType() == Track::kTrackTypeVideo)
|
|
|
|
frame += ((VideoTrack *)*it)->getCurFrame() + 1;
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
uint32 VideoDecoder::getFrameCount() const {
|
2012-07-21 00:51:42 +00:00
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
for (TrackList::const_iterator it = _tracks.begin(); it != _tracks.end(); it++)
|
|
|
|
if ((*it)->getTrackType() == Track::kTrackTypeVideo)
|
|
|
|
count += ((VideoTrack *)*it)->getFrameCount();
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
uint32 VideoDecoder::getTime() const {
|
2012-08-07 18:24:32 +00:00
|
|
|
if (!isPlaying())
|
|
|
|
return _lastTimeChange.msecs();
|
|
|
|
|
2012-07-21 00:51:42 +00:00
|
|
|
if (isPaused())
|
|
|
|
return _pauseStartTime - _startTime;
|
|
|
|
|
2012-07-21 21:30:06 +00:00
|
|
|
if (useAudioSync()) {
|
|
|
|
for (TrackList::const_iterator it = _tracks.begin(); it != _tracks.end(); it++) {
|
2012-07-21 21:38:24 +00:00
|
|
|
if ((*it)->getTrackType() == Track::kTrackTypeAudio && !(*it)->endOfTrack()) {
|
2012-07-21 21:30:06 +00:00
|
|
|
uint32 time = ((const AudioTrack *)*it)->getRunningTime();
|
|
|
|
|
|
|
|
if (time != 0)
|
2012-08-07 18:24:32 +00:00
|
|
|
return time + _lastTimeChange.msecs();
|
2012-07-21 21:30:06 +00:00
|
|
|
}
|
2012-07-21 00:51:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_system->getMillis() - _startTime;
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
uint32 VideoDecoder::getTimeToNextFrame() const {
|
2012-07-27 03:42:35 +00:00
|
|
|
if (endOfVideo() || _needsUpdate)
|
2012-07-21 00:51:42 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
const VideoTrack *track = findNextVideoTrack();
|
|
|
|
|
|
|
|
if (!track)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
uint32 elapsedTime = getTime();
|
|
|
|
uint32 nextFrameStartTime = track->getNextFrameStartTime();
|
|
|
|
|
|
|
|
if (nextFrameStartTime <= elapsedTime)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return nextFrameStartTime - elapsedTime;
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
bool VideoDecoder::endOfVideo() const {
|
2012-07-21 00:51:42 +00:00
|
|
|
for (TrackList::const_iterator it = _tracks.begin(); it != _tracks.end(); it++)
|
2012-09-05 15:27:01 +00:00
|
|
|
if (!(*it)->endOfTrack() && (!isPlaying() || (*it)->getTrackType() != Track::kTrackTypeVideo || !_endTimeSet || ((VideoTrack *)*it)->getNextFrameStartTime() < (uint)_endTime.msecs()))
|
2012-07-21 00:51:42 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
bool VideoDecoder::isRewindable() const {
|
2012-07-27 15:01:42 +00:00
|
|
|
if (!isVideoLoaded())
|
2012-07-21 00:51:42 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
for (TrackList::const_iterator it = _tracks.begin(); it != _tracks.end(); it++)
|
|
|
|
if (!(*it)->isRewindable())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
bool VideoDecoder::rewind() {
|
2012-07-21 00:51:42 +00:00
|
|
|
if (!isRewindable())
|
|
|
|
return false;
|
|
|
|
|
2012-07-25 04:39:21 +00:00
|
|
|
// Stop all tracks so they can be rewound
|
2012-07-27 15:01:42 +00:00
|
|
|
if (isPlaying())
|
2012-07-29 16:30:34 +00:00
|
|
|
stopAudio();
|
2012-07-21 00:51:42 +00:00
|
|
|
|
|
|
|
for (TrackList::iterator it = _tracks.begin(); it != _tracks.end(); it++)
|
|
|
|
if (!(*it)->rewind())
|
|
|
|
return false;
|
|
|
|
|
2012-07-25 04:39:21 +00:00
|
|
|
// Now that we've rewound, start all tracks again
|
2012-07-27 15:01:42 +00:00
|
|
|
if (isPlaying())
|
2012-07-29 16:30:34 +00:00
|
|
|
startAudio();
|
2012-07-25 04:39:21 +00:00
|
|
|
|
2012-08-07 18:24:32 +00:00
|
|
|
_lastTimeChange = 0;
|
2012-07-24 01:03:49 +00:00
|
|
|
_startTime = g_system->getMillis();
|
2012-07-27 03:44:40 +00:00
|
|
|
resetPauseStartTime();
|
2012-07-21 00:51:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
bool VideoDecoder::isSeekable() const {
|
2012-07-27 15:01:42 +00:00
|
|
|
if (!isVideoLoaded())
|
2012-07-21 00:51:42 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
for (TrackList::const_iterator it = _tracks.begin(); it != _tracks.end(); it++)
|
|
|
|
if (!(*it)->isSeekable())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
bool VideoDecoder::seek(const Audio::Timestamp &time) {
|
2012-07-21 00:51:42 +00:00
|
|
|
if (!isSeekable())
|
|
|
|
return false;
|
|
|
|
|
2012-07-25 04:39:21 +00:00
|
|
|
// Stop all tracks so they can be seeked
|
2012-07-27 15:01:42 +00:00
|
|
|
if (isPlaying())
|
2012-07-29 16:30:34 +00:00
|
|
|
stopAudio();
|
2012-07-21 00:51:42 +00:00
|
|
|
|
|
|
|
for (TrackList::iterator it = _tracks.begin(); it != _tracks.end(); it++)
|
|
|
|
if (!(*it)->seek(time))
|
|
|
|
return false;
|
|
|
|
|
2012-08-12 04:09:23 +00:00
|
|
|
_lastTimeChange = time;
|
|
|
|
|
2012-07-25 04:39:21 +00:00
|
|
|
// Now that we've seeked, start all tracks again
|
2012-08-12 04:09:23 +00:00
|
|
|
// Also reset our start time
|
|
|
|
if (isPlaying()) {
|
2012-07-29 16:30:34 +00:00
|
|
|
startAudio();
|
2012-08-12 04:09:23 +00:00
|
|
|
_startTime = g_system->getMillis() - time.msecs();
|
|
|
|
}
|
2012-07-25 04:39:21 +00:00
|
|
|
|
2012-07-27 03:44:40 +00:00
|
|
|
resetPauseStartTime();
|
2012-07-27 03:42:35 +00:00
|
|
|
_needsUpdate = true;
|
2012-07-21 00:51:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
void VideoDecoder::start() {
|
2012-07-27 15:01:42 +00:00
|
|
|
if (isPlaying() || !isVideoLoaded())
|
2012-07-21 00:51:42 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
_isPlaying = true;
|
|
|
|
_startTime = g_system->getMillis();
|
2012-08-12 04:09:23 +00:00
|
|
|
|
2012-08-24 03:11:09 +00:00
|
|
|
// Adjust start time if we've seeked to something besides zero time
|
|
|
|
if (_lastTimeChange.totalNumberOfFrames() != 0)
|
|
|
|
_startTime -= _lastTimeChange.msecs();
|
|
|
|
|
2012-07-29 16:30:34 +00:00
|
|
|
startAudio();
|
2012-07-21 00:51:42 +00:00
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
void VideoDecoder::stop() {
|
2012-07-27 15:01:42 +00:00
|
|
|
if (!isPlaying())
|
2012-07-21 00:51:42 +00:00
|
|
|
return;
|
|
|
|
|
2012-09-19 04:36:03 +00:00
|
|
|
// Stop audio here so we don't have it affect getTime()
|
|
|
|
stopAudio();
|
|
|
|
|
|
|
|
// Keep the time marked down in case we start up again
|
|
|
|
// We do this before _isPlaying is set so we don't get
|
|
|
|
// _lastTimeChange returned, but before _pauseLevel is
|
|
|
|
// reset.
|
|
|
|
_lastTimeChange = getTime();
|
|
|
|
|
2012-07-21 00:51:42 +00:00
|
|
|
_isPlaying = false;
|
|
|
|
_startTime = 0;
|
|
|
|
_palette = 0;
|
|
|
|
_dirtyPalette = false;
|
2012-07-27 03:42:35 +00:00
|
|
|
_needsUpdate = false;
|
2012-07-21 00:51:42 +00:00
|
|
|
|
|
|
|
// Also reset the pause state.
|
|
|
|
_pauseLevel = 0;
|
2012-09-24 01:04:20 +00:00
|
|
|
|
|
|
|
// Reset the pause state of the tracks too
|
|
|
|
for (TrackList::iterator it = _tracks.begin(); it != _tracks.end(); it++)
|
|
|
|
(*it)->pause(false);
|
2012-07-21 00:51:42 +00:00
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
Audio::Timestamp VideoDecoder::getDuration() const {
|
2012-07-21 18:39:03 +00:00
|
|
|
Audio::Timestamp maxDuration(0, 1000);
|
|
|
|
|
|
|
|
for (TrackList::const_iterator it = _tracks.begin(); it != _tracks.end(); it++) {
|
|
|
|
Audio::Timestamp duration = (*it)->getDuration();
|
|
|
|
|
2012-07-27 15:01:21 +00:00
|
|
|
if (duration > maxDuration)
|
|
|
|
maxDuration = duration;
|
2012-07-21 18:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return maxDuration;
|
2012-07-21 00:51:42 +00:00
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
VideoDecoder::Track::Track() {
|
2012-07-21 00:51:42 +00:00
|
|
|
_paused = false;
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
bool VideoDecoder::Track::isRewindable() const {
|
2012-07-21 00:51:42 +00:00
|
|
|
return isSeekable();
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
bool VideoDecoder::Track::rewind() {
|
2012-07-27 15:01:21 +00:00
|
|
|
return seek(Audio::Timestamp(0, 1000));
|
2012-07-21 18:39:03 +00:00
|
|
|
}
|
|
|
|
|
2012-09-22 19:01:26 +00:00
|
|
|
void VideoDecoder::Track::pause(bool shouldPause) {
|
|
|
|
_paused = shouldPause;
|
|
|
|
pauseIntern(shouldPause);
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
Audio::Timestamp VideoDecoder::Track::getDuration() const {
|
2012-07-21 18:39:03 +00:00
|
|
|
return Audio::Timestamp(0, 1000);
|
2012-07-21 00:51:42 +00:00
|
|
|
}
|
|
|
|
|
2012-08-17 02:49:22 +00:00
|
|
|
bool VideoDecoder::VideoTrack::endOfTrack() const {
|
|
|
|
return getCurFrame() >= (getFrameCount() - 1);
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
uint32 VideoDecoder::FixedRateVideoTrack::getNextFrameStartTime() const {
|
2012-07-21 00:51:42 +00:00
|
|
|
if (endOfTrack() || getCurFrame() < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Common::Rational time = (getCurFrame() + 1) * 1000;
|
|
|
|
time /= getFrameRate();
|
|
|
|
return time.toInt();
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
Audio::Timestamp VideoDecoder::FixedRateVideoTrack::getDuration() const {
|
2012-07-21 18:39:03 +00:00
|
|
|
// Since Audio::Timestamp doesn't support a fractional frame rate, we're currently
|
|
|
|
// just converting to milliseconds.
|
|
|
|
Common::Rational time = getFrameCount() * 1000;
|
|
|
|
time /= getFrameRate();
|
|
|
|
return time.toInt();
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
bool VideoDecoder::AudioTrack::endOfTrack() const {
|
2012-07-21 00:51:42 +00:00
|
|
|
Audio::AudioStream *stream = getAudioStream();
|
2012-07-21 21:30:43 +00:00
|
|
|
return !stream || !g_system->getMixer()->isSoundHandleActive(_handle) || stream->endOfData();
|
2012-07-21 00:51:42 +00:00
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
void VideoDecoder::AudioTrack::setVolume(byte volume) {
|
2012-07-21 00:51:42 +00:00
|
|
|
_volume = volume;
|
|
|
|
|
|
|
|
if (g_system->getMixer()->isSoundHandleActive(_handle))
|
|
|
|
g_system->getMixer()->setChannelVolume(_handle, _volume);
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
void VideoDecoder::AudioTrack::setBalance(int8 balance) {
|
2012-07-21 00:51:42 +00:00
|
|
|
_balance = balance;
|
|
|
|
|
|
|
|
if (g_system->getMixer()->isSoundHandleActive(_handle))
|
|
|
|
g_system->getMixer()->setChannelBalance(_handle, _balance);
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
void VideoDecoder::AudioTrack::start() {
|
2012-07-21 00:51:42 +00:00
|
|
|
stop();
|
|
|
|
|
|
|
|
Audio::AudioStream *stream = getAudioStream();
|
|
|
|
assert(stream);
|
|
|
|
|
|
|
|
g_system->getMixer()->playStream(getSoundType(), &_handle, stream, -1, getVolume(), getBalance(), DisposeAfterUse::NO);
|
|
|
|
|
|
|
|
// Pause the audio again if we're still paused
|
|
|
|
if (isPaused())
|
|
|
|
g_system->getMixer()->pauseHandle(_handle, true);
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
void VideoDecoder::AudioTrack::stop() {
|
2012-07-21 00:51:42 +00:00
|
|
|
g_system->getMixer()->stopHandle(_handle);
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
void VideoDecoder::AudioTrack::start(const Audio::Timestamp &limit) {
|
2012-08-12 04:09:23 +00:00
|
|
|
stop();
|
|
|
|
|
|
|
|
Audio::AudioStream *stream = getAudioStream();
|
|
|
|
assert(stream);
|
|
|
|
|
|
|
|
stream = Audio::makeLimitingAudioStream(stream, limit, DisposeAfterUse::NO);
|
|
|
|
|
|
|
|
g_system->getMixer()->playStream(getSoundType(), &_handle, stream, -1, getVolume(), getBalance(), DisposeAfterUse::YES);
|
|
|
|
|
|
|
|
// Pause the audio again if we're still paused
|
|
|
|
if (isPaused())
|
|
|
|
g_system->getMixer()->pauseHandle(_handle, true);
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
uint32 VideoDecoder::AudioTrack::getRunningTime() const {
|
2012-07-21 00:51:42 +00:00
|
|
|
if (g_system->getMixer()->isSoundHandleActive(_handle))
|
|
|
|
return g_system->getMixer()->getSoundElapsedTime(_handle);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
void VideoDecoder::AudioTrack::pauseIntern(bool shouldPause) {
|
2012-07-21 00:51:42 +00:00
|
|
|
if (g_system->getMixer()->isSoundHandleActive(_handle))
|
|
|
|
g_system->getMixer()->pauseHandle(_handle, shouldPause);
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
Audio::AudioStream *VideoDecoder::RewindableAudioTrack::getAudioStream() const {
|
2012-07-21 00:51:42 +00:00
|
|
|
return getRewindableAudioStream();
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
bool VideoDecoder::RewindableAudioTrack::rewind() {
|
2012-07-21 00:51:42 +00:00
|
|
|
Audio::RewindableAudioStream *stream = getRewindableAudioStream();
|
|
|
|
assert(stream);
|
|
|
|
return stream->rewind();
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
Audio::Timestamp VideoDecoder::SeekableAudioTrack::getDuration() const {
|
2012-07-21 18:39:03 +00:00
|
|
|
Audio::SeekableAudioStream *stream = getSeekableAudioStream();
|
|
|
|
assert(stream);
|
|
|
|
return stream->getLength();
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
Audio::AudioStream *VideoDecoder::SeekableAudioTrack::getAudioStream() const {
|
2012-07-21 00:51:42 +00:00
|
|
|
return getSeekableAudioStream();
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
bool VideoDecoder::SeekableAudioTrack::seek(const Audio::Timestamp &time) {
|
2012-07-21 00:51:42 +00:00
|
|
|
Audio::SeekableAudioStream *stream = getSeekableAudioStream();
|
|
|
|
assert(stream);
|
|
|
|
return stream->seek(time);
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
VideoDecoder::StreamFileAudioTrack::StreamFileAudioTrack() {
|
2012-07-21 21:52:16 +00:00
|
|
|
_stream = 0;
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
VideoDecoder::StreamFileAudioTrack::~StreamFileAudioTrack() {
|
2012-07-21 21:52:16 +00:00
|
|
|
delete _stream;
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
bool VideoDecoder::StreamFileAudioTrack::loadFromFile(const Common::String &baseName) {
|
2012-07-21 21:52:16 +00:00
|
|
|
// TODO: Make sure the stream isn't being played
|
|
|
|
delete _stream;
|
|
|
|
_stream = Audio::SeekableAudioStream::openStreamFile(baseName);
|
|
|
|
return _stream != 0;
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
void VideoDecoder::addTrack(Track *track) {
|
2012-07-21 00:51:42 +00:00
|
|
|
_tracks.push_back(track);
|
2012-07-21 18:50:10 +00:00
|
|
|
|
2012-07-21 21:11:09 +00:00
|
|
|
// Update volume settings if it's an audio track
|
|
|
|
if (track->getTrackType() == Track::kTrackTypeAudio) {
|
|
|
|
((AudioTrack *)track)->setVolume(_audioVolume);
|
|
|
|
((AudioTrack *)track)->setBalance(_audioBalance);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep the track paused if we're paused
|
2012-07-21 18:50:10 +00:00
|
|
|
if (isPaused())
|
|
|
|
track->pause(true);
|
|
|
|
|
2012-07-21 21:11:09 +00:00
|
|
|
// Start the track if we're playing
|
2012-07-29 16:30:34 +00:00
|
|
|
if (isPlaying() && track->getTrackType() == Track::kTrackTypeAudio)
|
|
|
|
((AudioTrack *)track)->start();
|
2012-07-21 00:51:42 +00:00
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
bool VideoDecoder::addStreamFileTrack(const Common::String &baseName) {
|
2012-08-12 12:33:45 +00:00
|
|
|
// Only allow adding external tracks if a video is already loaded
|
|
|
|
if (!isVideoLoaded())
|
|
|
|
return false;
|
|
|
|
|
2012-07-24 17:23:45 +00:00
|
|
|
StreamFileAudioTrack *track = new StreamFileAudioTrack();
|
|
|
|
|
|
|
|
bool result = track->loadFromFile(baseName);
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
addTrack(track);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
void VideoDecoder::setEndTime(const Audio::Timestamp &endTime) {
|
2012-08-12 04:09:23 +00:00
|
|
|
Audio::Timestamp startTime = 0;
|
|
|
|
|
|
|
|
if (isPlaying()) {
|
|
|
|
startTime = getTime();
|
|
|
|
stopAudio();
|
|
|
|
}
|
|
|
|
|
2012-08-16 17:34:28 +00:00
|
|
|
_endTime = endTime;
|
|
|
|
_endTimeSet = true;
|
2012-08-12 04:09:23 +00:00
|
|
|
|
2012-08-16 17:34:28 +00:00
|
|
|
if (startTime > endTime)
|
2012-08-12 04:09:23 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (isPlaying()) {
|
|
|
|
// We'll assume the audio track is going to start up at the same time it just was
|
|
|
|
// and therefore not do any seeking.
|
|
|
|
// Might want to set it anyway if we're seekable.
|
2012-08-16 17:34:28 +00:00
|
|
|
startAudioLimit(_endTime.msecs() - startTime.msecs());
|
2012-08-12 04:09:23 +00:00
|
|
|
_lastTimeChange = startTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
VideoDecoder::Track *VideoDecoder::getTrack(uint track) {
|
2012-07-22 16:17:54 +00:00
|
|
|
if (track > _tracks.size())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return _tracks[track];
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
const VideoDecoder::Track *VideoDecoder::getTrack(uint track) const {
|
2012-07-22 16:17:54 +00:00
|
|
|
if (track > _tracks.size())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return _tracks[track];
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
bool VideoDecoder::endOfVideoTracks() const {
|
2012-07-25 04:44:22 +00:00
|
|
|
for (TrackList::const_iterator it = _tracks.begin(); it != _tracks.end(); it++)
|
|
|
|
if ((*it)->getTrackType() == Track::kTrackTypeVideo && !(*it)->endOfTrack())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
VideoDecoder::VideoTrack *VideoDecoder::findNextVideoTrack() {
|
2012-07-21 00:51:42 +00:00
|
|
|
VideoTrack *bestTrack = 0;
|
|
|
|
uint32 bestTime = 0xFFFFFFFF;
|
|
|
|
|
|
|
|
for (TrackList::iterator it = _tracks.begin(); it != _tracks.end(); it++) {
|
2012-07-27 03:20:25 +00:00
|
|
|
if ((*it)->getTrackType() == Track::kTrackTypeVideo && !(*it)->endOfTrack()) {
|
2012-07-21 00:51:42 +00:00
|
|
|
VideoTrack *track = (VideoTrack *)*it;
|
|
|
|
uint32 time = track->getNextFrameStartTime();
|
|
|
|
|
|
|
|
if (time < bestTime) {
|
|
|
|
bestTime = time;
|
|
|
|
bestTrack = track;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bestTrack;
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
const VideoDecoder::VideoTrack *VideoDecoder::findNextVideoTrack() const {
|
2012-07-21 00:51:42 +00:00
|
|
|
const VideoTrack *bestTrack = 0;
|
|
|
|
uint32 bestTime = 0xFFFFFFFF;
|
|
|
|
|
|
|
|
for (TrackList::const_iterator it = _tracks.begin(); it != _tracks.end(); it++) {
|
2012-07-27 03:20:25 +00:00
|
|
|
if ((*it)->getTrackType() == Track::kTrackTypeVideo && !(*it)->endOfTrack()) {
|
2012-07-21 00:51:42 +00:00
|
|
|
const VideoTrack *track = (const VideoTrack *)*it;
|
|
|
|
uint32 time = track->getNextFrameStartTime();
|
|
|
|
|
|
|
|
if (time < bestTime) {
|
|
|
|
bestTime = time;
|
|
|
|
bestTrack = track;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bestTrack;
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
void VideoDecoder::startAudio() {
|
2012-08-16 17:34:28 +00:00
|
|
|
if (_endTimeSet) {
|
2012-08-12 04:09:23 +00:00
|
|
|
// HACK: Timestamp's subtraction asserts out when subtracting two times
|
|
|
|
// with different rates.
|
2012-08-16 17:34:28 +00:00
|
|
|
startAudioLimit(_endTime - _lastTimeChange.convertToFramerate(_endTime.framerate()));
|
2012-08-12 04:09:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-25 04:39:21 +00:00
|
|
|
for (TrackList::iterator it = _tracks.begin(); it != _tracks.end(); it++)
|
2012-07-29 16:30:34 +00:00
|
|
|
if ((*it)->getTrackType() == Track::kTrackTypeAudio)
|
|
|
|
((AudioTrack *)*it)->start();
|
2012-07-25 04:39:21 +00:00
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
void VideoDecoder::stopAudio() {
|
2012-07-25 04:39:21 +00:00
|
|
|
for (TrackList::iterator it = _tracks.begin(); it != _tracks.end(); it++)
|
2012-07-29 16:30:34 +00:00
|
|
|
if ((*it)->getTrackType() == Track::kTrackTypeAudio)
|
|
|
|
((AudioTrack *)*it)->stop();
|
2012-07-25 04:39:21 +00:00
|
|
|
}
|
|
|
|
|
2012-08-16 18:00:14 +00:00
|
|
|
void VideoDecoder::startAudioLimit(const Audio::Timestamp &limit) {
|
2012-08-12 04:09:23 +00:00
|
|
|
for (TrackList::iterator it = _tracks.begin(); it != _tracks.end(); it++)
|
|
|
|
if ((*it)->getTrackType() == Track::kTrackTypeAudio)
|
|
|
|
((AudioTrack *)*it)->start(limit);
|
|
|
|
}
|
|
|
|
|
2012-09-01 01:47:52 +00:00
|
|
|
bool VideoDecoder::hasFramesLeft() const {
|
|
|
|
// This is similar to endOfVideo(), except it doesn't take Audio into account (and returns true if not the end of the video)
|
|
|
|
// This is only used for needsUpdate() atm so that setEndTime() works properly
|
|
|
|
// And unlike endOfVideoTracks(), this takes into account _endTime
|
|
|
|
for (TrackList::const_iterator it = _tracks.begin(); it != _tracks.end(); it++)
|
2012-09-05 15:27:01 +00:00
|
|
|
if ((*it)->getTrackType() == Track::kTrackTypeVideo && !(*it)->endOfTrack() && (!isPlaying() || !_endTimeSet || ((VideoTrack *)*it)->getNextFrameStartTime() < (uint)_endTime.msecs()))
|
2012-09-01 01:47:52 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-23 19:08:09 +00:00
|
|
|
} // End of namespace Video
|