Port video player

pull/638/head
scrawl 10 years ago
parent 68f93294da
commit 42f6d9e15b

@ -549,7 +549,7 @@ include_directories(libs)
add_subdirectory(libs/openengine) add_subdirectory(libs/openengine)
# Extern # Extern
#add_subdirectory (extern/ogre-ffmpeg-videoplayer) add_subdirectory (extern/ogre-ffmpeg-videoplayer)
add_subdirectory (extern/oics) add_subdirectory (extern/oics)
#add_subdirectory (extern/sdl4ogre) #add_subdirectory (extern/sdl4ogre)

@ -59,8 +59,7 @@ add_openmw_dir (mwscript
) )
add_openmw_dir (mwsound add_openmw_dir (mwsound
soundmanagerimp openal_output ffmpeg_decoder sound sound_decoder sound_output loudness libavwrapper soundmanagerimp openal_output ffmpeg_decoder sound sound_decoder sound_output loudness libavwrapper movieaudiofactory
# movieaudiofactory
) )
add_openmw_dir (mwworld add_openmw_dir (mwworld
@ -138,6 +137,7 @@ target_link_libraries(openmw
${MYGUI_LIBRARIES} ${MYGUI_LIBRARIES}
${SDL2_LIBRARY} ${SDL2_LIBRARY}
${MYGUI_PLATFORM_LIBRARIES} ${MYGUI_PLATFORM_LIBRARIES}
"ogre-ffmpeg-videoplayer"
"oics" "oics"
components components
) )

@ -167,6 +167,7 @@ namespace MWRender
{ {
mObjects->update(dt); mObjects->update(dt);
mEffectManager->update(dt); mEffectManager->update(dt);
mSky->update(dt);
} }
void RenderingManager::spawnEffect(const std::string &model, const std::string &texture, const osg::Vec3f &worldPosition, float scale) void RenderingManager::spawnEffect(const std::string &model, const std::string &texture, const osg::Vec3f &worldPosition, float scale)

@ -599,8 +599,8 @@ void SkyManager::update(float duration)
mCloudAnimationTimer += duration * mCloudSpeed; mCloudAnimationTimer += duration * mCloudSpeed;
/// \todo improve this /// \todo improve this
//mMasser->setPhase( static_cast<Moon::Phase>( (int) ((mDay % 32)/4.f)) ); mMasser->setPhase( static_cast<Moon::Phase>( (int) ((mDay % 32)/4.f)) );
//mSecunda->setPhase ( static_cast<Moon::Phase>( (int) ((mDay % 32)/4.f)) ); mSecunda->setPhase ( static_cast<Moon::Phase>( (int) ((mDay % 32)/4.f)) );
//mSecunda->setColour ( mMoonRed ? fallback->getFallbackColour("Moons_Script_Color") : ColourValue(1,1,1,1)); //mSecunda->setColour ( mMoonRed ? fallback->getFallbackColour("Moons_Script_Color") : ColourValue(1,1,1,1));
//mMasser->setColour (ColourValue(1,1,1,1)); //mMasser->setColour (ColourValue(1,1,1,1));
@ -684,13 +684,8 @@ void SkyManager::setWeather(const MWWorld::WeatherResult& weather)
DisableCullingVisitor visitor; DisableCullingVisitor visitor;
mParticleEffect->accept(visitor); mParticleEffect->accept(visitor);
/* SceneUtil::AssignControllerSourcesVisitor assignVisitor(boost::shared_ptr<SceneUtil::ControllerSource>(new SceneUtil::FrameTimeSource));
for (size_t i = 0; i < mParticle->mControllers.size(); ++i) mParticleEffect->accept(assignVisitor);
{
if (mParticle->mControllers[i].getSource().isNull())
mParticle->mControllers[i].setSource(Ogre::ControllerManager::getSingleton().getFrameTimeSource());
}
*/
} }
} }

@ -17,7 +17,8 @@ namespace MWSound
{ {
public: public:
MWSoundDecoderBridge(MWSound::MovieAudioDecoder* decoder) MWSoundDecoderBridge(MWSound::MovieAudioDecoder* decoder)
: mDecoder(decoder) : Sound_Decoder(NULL)
, mDecoder(decoder)
{ {
} }
@ -51,7 +52,7 @@ namespace MWSound
std::string getStreamName() std::string getStreamName()
{ {
return mVideoState->stream->getName(); return std::string();
} }
private: private:

@ -1,5 +1,7 @@
#include "videoplayer.hpp" #include "videoplayer.hpp"
#include <osg/Texture2D>
#include "audiofactory.hpp" #include "audiofactory.hpp"
#include "videostate.hpp" #include "videostate.hpp"
@ -23,7 +25,7 @@ void VideoPlayer::setAudioFactory(MovieAudioFactory *factory)
mAudioFactory.reset(factory); mAudioFactory.reset(factory);
} }
void VideoPlayer::playVideo(const std::string &resourceName) void VideoPlayer::playVideo(boost::shared_ptr<std::istream> inputstream)
{ {
if(mState) if(mState)
close(); close();
@ -31,10 +33,10 @@ void VideoPlayer::playVideo(const std::string &resourceName)
try { try {
mState = new VideoState; mState = new VideoState;
mState->setAudioFactory(mAudioFactory.get()); mState->setAudioFactory(mAudioFactory.get());
mState->init(resourceName); mState->init(inputstream);
// wait until we have the first picture // wait until we have the first picture
while (mState->video_st && mState->mTexture.isNull()) while (mState->video_st && !mState->mTexture.get())
{ {
if (!mState->update()) if (!mState->update())
break; break;
@ -53,27 +55,26 @@ bool VideoPlayer::update ()
return false; return false;
} }
std::string VideoPlayer::getTextureName() osg::ref_ptr<osg::Texture2D> VideoPlayer::getVideoTexture()
{ {
std::string name; if (mState)
if (mState && !mState->mTexture.isNull()) return mState->mTexture;
name = mState->mTexture->getName(); return osg::ref_ptr<osg::Texture2D>();
return name;
} }
int VideoPlayer::getVideoWidth() int VideoPlayer::getVideoWidth()
{ {
int width=0; int width=0;
if (mState && !mState->mTexture.isNull()) if (mState && mState->mTexture.get())
width = mState->mTexture->getWidth(); width = mState->mTexture->getTextureWidth();
return width; return width;
} }
int VideoPlayer::getVideoHeight() int VideoPlayer::getVideoHeight()
{ {
int height=0; int height=0;
if (mState && !mState->mTexture.isNull()) if (mState && mState->mTexture.get())
height = mState->mTexture->getHeight(); height = mState->mTexture->getTextureHeight();
return height; return height;
} }

@ -4,6 +4,17 @@
#include <string> #include <string>
#include <memory> #include <memory>
#include <iosfwd>
#include <osg/Texture2D>
#include <boost/shared_ptr.hpp>
namespace osg
{
class Texture2D;
}
namespace Video namespace Video
{ {
@ -30,7 +41,7 @@ namespace Video
/// Play the given video. If a video is already playing, the old video is closed first. /// Play the given video. If a video is already playing, the old video is closed first.
/// @note The video will be unpaused by default. Use the pause() and play() methods to control pausing. /// @note The video will be unpaused by default. Use the pause() and play() methods to control pausing.
void playVideo (const std::string& resourceName); void playVideo (boost::shared_ptr<std::istream> inputstream);
/// Get the current playback time position in the video, in seconds /// Get the current playback time position in the video, in seconds
double getCurrentTime(); double getCurrentTime();
@ -52,8 +63,9 @@ namespace Video
/// Stop the currently playing video, if a video is playing. /// Stop the currently playing video, if a video is playing.
void close(); void close();
/// Return the texture name of the currently playing video, or "" if no video is playing. /// Return the texture of the currently playing video, or a null pointer if no video is playing.
std::string getTextureName(); osg::ref_ptr<osg::Texture2D> getVideoTexture();
/// Return the width of the currently playing video, or 0 if no video is playing. /// Return the width of the currently playing video, or 0 if no video is playing.
int getVideoWidth(); int getVideoWidth();
/// Return the height of the currently playing video, or 0 if no video is playing. /// Return the height of the currently playing video, or 0 if no video is playing.

@ -11,6 +11,8 @@
#include <OgreResourceGroupManager.h> #include <OgreResourceGroupManager.h>
#include <OgreStringConverter.h> #include <OgreStringConverter.h>
#include <osg/Texture2D>
extern "C" extern "C"
{ {
#include <libavcodec/avcodec.h> #include <libavcodec/avcodec.h>
@ -172,12 +174,14 @@ void PacketQueue::clear()
this->mutex.unlock (); this->mutex.unlock ();
} }
int VideoState::OgreResource_Read(void *user_data, uint8_t *buf, int buf_size) int VideoState::istream_read(void *user_data, uint8_t *buf, int buf_size)
{ {
Ogre::DataStreamPtr stream = static_cast<VideoState*>(user_data)->stream;
try try
{ {
return stream->read(buf, buf_size); std::istream& stream = *static_cast<VideoState*>(user_data)->stream;
stream.read((char*)buf, buf_size);
stream.clear();
return stream.gcount();
} }
catch (std::exception& ) catch (std::exception& )
{ {
@ -185,57 +189,57 @@ int VideoState::OgreResource_Read(void *user_data, uint8_t *buf, int buf_size)
} }
} }
int VideoState::OgreResource_Write(void *user_data, uint8_t *buf, int buf_size) int VideoState::istream_write(void *, uint8_t *, int)
{ {
Ogre::DataStreamPtr stream = static_cast<VideoState*>(user_data)->stream; throw std::runtime_error("can't write to read-only stream");
try
{
return stream->write(buf, buf_size);
}
catch (std::exception& )
{
return 0;
}
} }
int64_t VideoState::OgreResource_Seek(void *user_data, int64_t offset, int whence) int64_t VideoState::istream_seek(void *user_data, int64_t offset, int whence)
{ {
Ogre::DataStreamPtr stream = static_cast<VideoState*>(user_data)->stream; std::istream& stream = *static_cast<VideoState*>(user_data)->stream;
whence &= ~AVSEEK_FORCE; whence &= ~AVSEEK_FORCE;
if(whence == AVSEEK_SIZE) if(whence == AVSEEK_SIZE)
return stream->size(); {
size_t prev = stream.tellg();
stream.seekg(0, std::ios_base::end);
size_t size = stream.tellg();
stream.seekg(prev, std::ios_base::beg);
return size;
}
if(whence == SEEK_SET) if(whence == SEEK_SET)
stream->seek(static_cast<size_t>(offset)); stream.seekg(offset, std::ios_base::beg);
else if(whence == SEEK_CUR) else if(whence == SEEK_CUR)
stream->seek(static_cast<size_t>(stream->tell()+offset)); stream.seekg(offset, std::ios_base::cur);
else if(whence == SEEK_END) else if(whence == SEEK_END)
stream->seek(static_cast<size_t>(stream->size() + offset)); stream.seekg(offset, std::ios_base::end);
else else
return -1; return -1;
return stream->tell(); return stream.tellg();
} }
void VideoState::video_display(VideoPicture *vp) void VideoState::video_display(VideoPicture *vp)
{ {
if((*this->video_st)->codec->width != 0 && (*this->video_st)->codec->height != 0) if((*this->video_st)->codec->width != 0 && (*this->video_st)->codec->height != 0)
{ {
if (mTexture.isNull()) if (!mTexture.get())
{ {
static int i = 0; mTexture = new osg::Texture2D;
mTexture = Ogre::TextureManager::getSingleton().createManual( mTexture->setDataVariance(osg::Object::DYNAMIC);
"ffmpeg/VideoTexture" + Ogre::StringConverter::toString(++i), mTexture->setResizeNonPowerOfTwoHint(false);
Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, mTexture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
Ogre::TEX_TYPE_2D, mTexture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
(*this->video_st)->codec->width, (*this->video_st)->codec->height,
0,
Ogre::PF_BYTE_RGBA,
Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
} }
Ogre::PixelBox pb((*this->video_st)->codec->width, (*this->video_st)->codec->height, 1, Ogre::PF_BYTE_RGBA, &vp->data[0]);
Ogre::HardwarePixelBufferSharedPtr buffer = mTexture->getBuffer(); osg::ref_ptr<osg::Image> image = new osg::Image;
buffer->blitFromMemory(pb);
image->setImage((*this->video_st)->codec->width, (*this->video_st)->codec->height,
1, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, &vp->data[0], osg::Image::NO_DELETE);
mTexture->setImage(image);
} }
} }
@ -250,7 +254,7 @@ void VideoState::video_refresh()
VideoPicture* vp = &this->pictq[this->pictq_rindex]; VideoPicture* vp = &this->pictq[this->pictq_rindex];
this->video_display(vp); this->video_display(vp);
this->pictq_rindex = (pictq_rindex+1) % VIDEO_PICTURE_QUEUE_SIZE; this->pictq_rindex = (pictq_rindex+1) % VIDEO_PICTURE_ARRAY_SIZE;
this->frame_last_pts = vp->pts; this->frame_last_pts = vp->pts;
this->pictq_size--; this->pictq_size--;
this->pictq_cond.notify_one(); this->pictq_cond.notify_one();
@ -267,12 +271,12 @@ void VideoState::video_refresh()
for (; i<this->pictq_size-1; ++i) for (; i<this->pictq_size-1; ++i)
{ {
if (this->pictq[pictq_rindex].pts + threshold <= this->get_master_clock()) if (this->pictq[pictq_rindex].pts + threshold <= this->get_master_clock())
this->pictq_rindex = (this->pictq_rindex+1) % VIDEO_PICTURE_QUEUE_SIZE; // not enough time to show this picture this->pictq_rindex = (this->pictq_rindex+1) % VIDEO_PICTURE_ARRAY_SIZE; // not enough time to show this picture
else else
break; break;
} }
assert (this->pictq_rindex < VIDEO_PICTURE_QUEUE_SIZE); assert (this->pictq_rindex < VIDEO_PICTURE_ARRAY_SIZE);
VideoPicture* vp = &this->pictq[this->pictq_rindex]; VideoPicture* vp = &this->pictq[this->pictq_rindex];
this->video_display(vp); this->video_display(vp);
@ -282,7 +286,7 @@ void VideoState::video_refresh()
this->pictq_size -= i; this->pictq_size -= i;
// update queue for next picture // update queue for next picture
this->pictq_size--; this->pictq_size--;
this->pictq_rindex = (this->pictq_rindex+1) % VIDEO_PICTURE_QUEUE_SIZE; this->pictq_rindex = (this->pictq_rindex+1) % VIDEO_PICTURE_ARRAY_SIZE;
this->pictq_cond.notify_one(); this->pictq_cond.notify_one();
} }
} }
@ -328,7 +332,7 @@ int VideoState::queue_picture(AVFrame *pFrame, double pts)
0, (*this->video_st)->codec->height, &dst, this->rgbaFrame->linesize); 0, (*this->video_st)->codec->height, &dst, this->rgbaFrame->linesize);
// now we inform our display thread that we have a pic ready // now we inform our display thread that we have a pic ready
this->pictq_windex = (this->pictq_windex+1) % VIDEO_PICTURE_QUEUE_SIZE; this->pictq_windex = (this->pictq_windex+1) % VIDEO_PICTURE_ARRAY_SIZE;
this->pictq_size++; this->pictq_size++;
this->pictq_mutex.unlock(); this->pictq_mutex.unlock();
@ -605,7 +609,7 @@ int VideoState::stream_open(int stream_index, AVFormatContext *pFormatCtx)
return 0; return 0;
} }
void VideoState::init(const std::string& resourceName) void VideoState::init(boost::shared_ptr<std::istream> inputstream)
{ {
int video_index = -1; int video_index = -1;
int audio_index = -1; int audio_index = -1;
@ -614,17 +618,19 @@ void VideoState::init(const std::string& resourceName)
this->av_sync_type = AV_SYNC_DEFAULT; this->av_sync_type = AV_SYNC_DEFAULT;
this->mQuit = false; this->mQuit = false;
this->stream = Ogre::ResourceGroupManager::getSingleton().openResource(resourceName); this->stream = inputstream;
if(this->stream.isNull()) if(!this->stream.get())
throw std::runtime_error("Failed to open video resource"); throw std::runtime_error("Failed to open video resource");
AVIOContext *ioCtx = avio_alloc_context(NULL, 0, 0, this, OgreResource_Read, OgreResource_Write, OgreResource_Seek); AVIOContext *ioCtx = avio_alloc_context(NULL, 0, 0, this, istream_read, istream_write, istream_seek);
if(!ioCtx) throw std::runtime_error("Failed to allocate AVIOContext"); if(!ioCtx) throw std::runtime_error("Failed to allocate AVIOContext");
this->format_ctx = avformat_alloc_context(); this->format_ctx = avformat_alloc_context();
if(this->format_ctx) if(this->format_ctx)
this->format_ctx->pb = ioCtx; this->format_ctx->pb = ioCtx;
std::string videoName;
// Open video file // Open video file
/// ///
/// format_ctx->pb->buffer must be freed by hand, /// format_ctx->pb->buffer must be freed by hand,
@ -632,7 +638,7 @@ void VideoState::init(const std::string& resourceName)
/// ///
/// https://trac.ffmpeg.org/ticket/1357 /// https://trac.ffmpeg.org/ticket/1357
/// ///
if(!this->format_ctx || avformat_open_input(&this->format_ctx, resourceName.c_str(), NULL, NULL)) if(!this->format_ctx || avformat_open_input(&this->format_ctx, videoName.c_str(), NULL, NULL))
{ {
if (this->format_ctx != NULL) if (this->format_ctx != NULL)
{ {
@ -656,7 +662,7 @@ void VideoState::init(const std::string& resourceName)
throw std::runtime_error("Failed to retrieve stream information"); throw std::runtime_error("Failed to retrieve stream information");
// Dump information about file onto standard error // Dump information about file onto standard error
av_dump_format(this->format_ctx, 0, resourceName.c_str(), 0); av_dump_format(this->format_ctx, 0, videoName.c_str(), 0);
for(i = 0;i < this->format_ctx->nb_streams;i++) for(i = 0;i < this->format_ctx->nb_streams;i++)
{ {
@ -724,11 +730,7 @@ void VideoState::deinit()
avformat_close_input(&this->format_ctx); avformat_close_input(&this->format_ctx);
} }
if (!mTexture.isNull()) mTexture = NULL;
{
Ogre::TextureManager::getSingleton().remove(mTexture->getName());
mTexture.setNull();
}
} }
double VideoState::get_external_clock() double VideoState::get_external_clock()

@ -3,11 +3,17 @@
#include <boost/thread.hpp> #include <boost/thread.hpp>
#include <OgreTexture.h> #include <osg/ref_ptr>
namespace osg
{
class Texture2D;
}
#include "videodefs.hpp" #include "videodefs.hpp"
#define VIDEO_PICTURE_QUEUE_SIZE 50 #define VIDEO_PICTURE_QUEUE_SIZE 50
// allocate one extra to make sure we do not overwrite the osg::Image currently set on the texture
#define VIDEO_PICTURE_ARRAY_SIZE (VIDEO_PICTURE_QUEUE_SIZE+1)
extern "C" extern "C"
{ {
@ -78,7 +84,7 @@ struct VideoState {
void setAudioFactory(MovieAudioFactory* factory); void setAudioFactory(MovieAudioFactory* factory);
void init(const std::string& resourceName); void init(boost::shared_ptr<std::istream> inputstream);
void deinit(); void deinit();
void setPaused(bool isPaused); void setPaused(bool isPaused);
@ -104,18 +110,18 @@ struct VideoState {
double get_external_clock(); double get_external_clock();
double get_master_clock(); double get_master_clock();
static int OgreResource_Read(void *user_data, uint8_t *buf, int buf_size); static int istream_read(void *user_data, uint8_t *buf, int buf_size);
static int OgreResource_Write(void *user_data, uint8_t *buf, int buf_size); static int istream_write(void *user_data, uint8_t *buf, int buf_size);
static int64_t OgreResource_Seek(void *user_data, int64_t offset, int whence); static int64_t istream_seek(void *user_data, int64_t offset, int whence);
Ogre::TexturePtr mTexture; osg::ref_ptr<osg::Texture2D> mTexture;
MovieAudioFactory* mAudioFactory; MovieAudioFactory* mAudioFactory;
boost::shared_ptr<MovieAudioDecoder> mAudioDecoder; boost::shared_ptr<MovieAudioDecoder> mAudioDecoder;
ExternalClock mExternalClock; ExternalClock mExternalClock;
Ogre::DataStreamPtr stream; boost::shared_ptr<std::istream> stream;
AVFormatContext* format_ctx; AVFormatContext* format_ctx;
int av_sync_type; int av_sync_type;
@ -130,7 +136,7 @@ struct VideoState {
double video_clock; ///<pts of last decoded frame / predicted pts of next decoded frame double video_clock; ///<pts of last decoded frame / predicted pts of next decoded frame
PacketQueue videoq; PacketQueue videoq;
SwsContext* sws_context; SwsContext* sws_context;
VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE]; VideoPicture pictq[VIDEO_PICTURE_ARRAY_SIZE];
AVFrame* rgbaFrame; // used as buffer for the frame converted from its native format to RGBA AVFrame* rgbaFrame; // used as buffer for the frame converted from its native format to RGBA
int pictq_size, pictq_rindex, pictq_windex; int pictq_size, pictq_rindex, pictq_windex;
boost::mutex pictq_mutex; boost::mutex pictq_mutex;

Loading…
Cancel
Save