openmw-tes3coop/apps/openmw/mwrender/videoplayer.hpp

170 lines
4.4 KiB
C++
Raw Normal View History

2012-12-11 21:49:31 +00:00
#ifndef VIDEOPLAYER_H
#define VIDEOPLAYER_H
2012-09-25 00:35:50 +00:00
2012-12-11 21:49:31 +00:00
#include <OgreRoot.h>
#include <OgreHardwarePixelBuffer.h>
2012-09-25 00:35:50 +00:00
2012-12-11 21:49:31 +00:00
#include <boost/thread.hpp>
2012-09-25 00:35:50 +00:00
2012-12-11 21:49:31 +00:00
#define __STDC_CONSTANT_MACROS
#include <stdint.h>
extern "C"
2012-09-25 00:35:50 +00:00
{
2012-12-11 21:49:31 +00:00
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
2012-09-25 00:35:50 +00:00
}
2012-12-11 21:49:31 +00:00
#include <cstdio>
#include <cmath>
#include "../mwbase/soundmanager.hpp"
2012-12-11 21:49:31 +00:00
#define MAX_AUDIOQ_SIZE (5 * 16 * 1024)
#define MAX_VIDEOQ_SIZE (5 * 256 * 1024)
#define AV_SYNC_THRESHOLD 0.01
#define AV_NOSYNC_THRESHOLD 10.0
#define SAMPLE_CORRECTION_PERCENT_MAX 10
#define AUDIO_DIFF_AVG_NB 20
#define VIDEO_PICTURE_QUEUE_SIZE 1
2012-12-13 14:11:32 +00:00
#define DEFAULT_AV_SYNC_TYPE AV_SYNC_EXTERNAL_MASTER
2012-12-11 21:49:31 +00:00
2012-09-25 00:35:50 +00:00
namespace MWRender
{
struct VideoState;
2012-12-11 21:49:31 +00:00
struct PacketQueue {
PacketQueue()
: first_pkt(NULL), last_pkt(NULL), nb_packets(0), size(0)
{ }
2012-12-11 21:49:31 +00:00
AVPacketList *first_pkt, *last_pkt;
int nb_packets;
int size;
2012-12-11 21:49:31 +00:00
boost::mutex mutex;
boost::condition_variable cond;
void put(AVPacket *pkt);
int get(AVPacket *pkt, VideoState *is, int block);
void flush();
2012-12-11 21:49:31 +00:00
};
2012-12-11 21:49:31 +00:00
struct VideoPicture {
VideoPicture () : data(NULL), pts(0)
{ }
2012-12-11 21:49:31 +00:00
uint8_t *data;
2012-12-11 21:49:31 +00:00
double pts;
};
struct VideoState {
VideoState ()
: videoStream(-1), audioStream(-1), av_sync_type(0), external_clock(0),
2012-12-14 01:53:22 +00:00
external_clock_time(0), audio_clock(0), audio_st(NULL), audio_diff_cum(0),
audio_diff_avg_coef(0), audio_diff_threshold(0), audio_diff_avg_count(0), frame_timer(0),
frame_last_pts(0), frame_last_delay(0), video_clock(0), video_current_pts(0),
video_current_pts_time(0), video_st(NULL), rgbaFrame(NULL), pictq_size(0), pictq_rindex(0),
pictq_windex(0), quit(false), refresh(0), format_ctx(0), sws_context(NULL), display_ready(0)
2012-12-11 21:49:31 +00:00
{}
~VideoState()
{
audioq.flush();
videoq.flush();
2012-12-11 21:49:31 +00:00
if(pictq_size >= 1)
free(pictq[0].data);
2012-12-11 21:49:31 +00:00
}
void init(const std::string& resourceName);
void deinit();
static int OgreResource_Read(void *user_data, uint8_t *buf, int buf_size);
static int OgreResource_Write(void *user_data, uint8_t *buf, int buf_size);
static int64_t OgreResource_Seek(void *user_data, int64_t offset, int whence);
int videoStream, audioStream;
int av_sync_type;
double external_clock; /* external clock base */
int64_t external_clock_time;
2012-12-11 21:49:31 +00:00
double audio_clock;
AVStream *audio_st;
PacketQueue audioq;
AVPacket audio_pkt;
double audio_diff_cum; /* used for AV difference average computation */
double audio_diff_avg_coef;
double audio_diff_threshold;
int audio_diff_avg_count;
double frame_timer;
double frame_last_pts;
double frame_last_delay;
double video_clock; ///<pts of last decoded frame / predicted pts of next decoded frame
double video_current_pts; ///<current displayed pts (different from video_clock if frame fifos are used)
int64_t video_current_pts_time; ///<time (av_gettime) at which we updated video_current_pts - used to have running video pts
AVStream *video_st;
PacketQueue videoq;
2012-12-11 21:49:31 +00:00
2012-12-11 22:06:06 +00:00
Ogre::DataStreamPtr stream;
MWBase::SoundPtr AudioTrack;
AVFormatContext* format_ctx;
2012-12-11 21:49:31 +00:00
SwsContext* sws_context;
VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
AVFrame* rgbaFrame; // used as buffer for the frame converted from its native format to RGBA
int pictq_size, pictq_rindex, pictq_windex;
2012-12-11 21:49:31 +00:00
boost::mutex pictq_mutex;
boost::condition_variable pictq_cond;
boost::thread parse_thread;
boost::thread video_thread;
2012-12-13 11:13:44 +00:00
volatile int quit;
2012-12-14 02:24:57 +00:00
volatile bool refresh;
2012-12-11 21:49:31 +00:00
int display_ready;
2012-12-11 21:49:31 +00:00
};
enum {
AV_SYNC_AUDIO_MASTER,
AV_SYNC_VIDEO_MASTER,
AV_SYNC_EXTERNAL_MASTER
};
2012-09-25 00:35:50 +00:00
class VideoPlayer
{
2012-12-11 21:49:31 +00:00
2012-09-25 00:35:50 +00:00
public:
VideoPlayer(Ogre::SceneManager* sceneMgr);
~VideoPlayer();
2012-12-11 21:49:31 +00:00
void playVideo (const std::string& resourceName);
2012-09-25 00:35:50 +00:00
void update();
void close();
2012-12-11 21:49:31 +00:00
bool isPlaying();
2012-12-11 21:49:31 +00:00
private:
VideoState* mState;
2012-12-11 21:49:31 +00:00
Ogre::SceneManager* mSceneMgr;
Ogre::Rectangle2D* mRectangle;
Ogre::MaterialPtr mVideoMaterial;
2012-09-25 00:35:50 +00:00
};
}
#endif