2017-07-13 10:26:03 +02:00
/*
* CVideoHandler . h , part of VCMI engine
*
* Authors : listed in file AUTHORS in main folder
*
* License : GNU General Public License v2 .0 or later
* Full text of license available in license . txt file , in main folder
*
*/
2011-12-14 00:23:17 +03:00
# pragma once
2009-06-28 02:33:59 +03:00
struct SDL_Surface ;
2011-08-08 17:20:22 +03:00
class IVideoPlayer
{
public :
2014-07-10 13:18:21 +03:00
virtual bool open ( std : : string name , bool scale = false ) = 0 ; //true - succes
2011-08-08 17:20:22 +03:00
virtual void close ( ) = 0 ;
virtual bool nextFrame ( ) = 0 ;
virtual void show ( int x , int y , SDL_Surface * dst , bool update = true ) = 0 ;
virtual void redraw ( int x , int y , SDL_Surface * dst , bool update = true ) = 0 ; //reblits buffer
virtual bool wait ( ) = 0 ;
virtual int curFrame ( ) const = 0 ;
virtual int frameCount ( ) const = 0 ;
} ;
class IMainVideoPlayer : public IVideoPlayer
{
public :
std : : string fname ; //name of current video file (empty if idle)
virtual void update ( int x , int y , SDL_Surface * dst , bool forceRedraw , bool update = true ) { }
2018-07-25 00:36:48 +02:00
virtual bool openAndPlayVideo ( std : : string name , int x , int y , bool stopOnKey = false , bool scale = false )
2011-08-08 17:20:22 +03:00
{
return false ;
}
} ;
class CEmptyVideoPlayer : public IMainVideoPlayer
{
2015-01-28 21:46:28 +02:00
public :
2014-07-10 13:18:21 +03:00
int curFrame ( ) const override { return - 1 ; } ;
int frameCount ( ) const override { return - 1 ; } ;
2014-09-04 20:59:07 +03:00
void redraw ( int x , int y , SDL_Surface * dst , bool update = true ) override { } ;
void show ( int x , int y , SDL_Surface * dst , bool update = true ) override { } ;
2014-07-10 13:18:21 +03:00
bool nextFrame ( ) override { return false ; } ;
void close ( ) override { } ;
bool wait ( ) override { return false ; } ;
bool open ( std : : string name , bool scale = false ) override { return false ; } ;
2011-08-08 17:20:22 +03:00
} ;
2012-08-18 13:29:54 +03:00
# ifndef DISABLE_VIDEO
2013-04-07 13:48:07 +03:00
# include "../lib/filesystem/CInputStream.h"
2013-02-06 02:11:48 +03:00
2011-11-17 03:24:27 +03:00
# include <SDL.h>
2009-06-24 15:14:48 +03:00
# include <SDL_video.h>
2012-08-21 20:37:06 +03:00
extern " C " {
# include <libavformat/avformat.h>
2022-05-10 18:25:48 +02:00
# include <libavcodec/avcodec.h>
# include <libavutil/imgutils.h>
2012-08-21 20:37:06 +03:00
# include <libswscale/swscale.h>
}
2009-06-24 15:14:48 +03:00
2015-11-11 14:18:05 +02:00
//compatibility for libav 9.18 in ubuntu 14.04, 52.66.100 is ffmpeg 2.2.3
2015-11-14 02:59:16 +02:00
# if (LIBAVUTIL_VERSION_INT < AV_VERSION_INT(52, 66, 100))
2015-11-11 14:44:17 +02:00
inline AVFrame * av_frame_alloc ( )
{
return avcodec_alloc_frame ( ) ;
}
inline void av_frame_free ( AVFrame * * frame )
{
av_free ( * frame ) ;
* frame = nullptr ;
}
# endif // VCMI_USE_OLD_AVUTIL
2015-11-14 02:59:16 +02:00
//fix for travis-ci
# if (LIBAVUTIL_VERSION_INT < AV_VERSION_INT(52, 0, 0))
# define AVPixelFormat PixelFormat
# define AV_PIX_FMT_NONE PIX_FMT_NONE
# define AV_PIX_FMT_YUV420P PIX_FMT_YUV420P
# define AV_PIX_FMT_BGR565 PIX_FMT_BGR565
# define AV_PIX_FMT_BGR24 PIX_FMT_BGR24
# define AV_PIX_FMT_BGR32 PIX_FMT_BGR32
# define AV_PIX_FMT_RGB565 PIX_FMT_RGB565
# define AV_PIX_FMT_RGB24 PIX_FMT_RGB24
# define AV_PIX_FMT_RGB32 PIX_FMT_RGB32
# endif
2011-08-08 17:20:22 +03:00
class CVideoPlayer : public IMainVideoPlayer
2009-06-24 15:14:48 +03:00
{
int stream ; // stream index in video
AVFormatContext * format ;
AVCodecContext * codecContext ; // codec context for stream
2022-05-10 18:25:48 +02:00
const AVCodec * codec ;
2015-11-14 02:59:16 +02:00
AVFrame * frame ;
2009-06-24 15:14:48 +03:00
struct SwsContext * sws ;
2012-08-21 20:37:06 +03:00
AVIOContext * context ;
2009-06-28 02:33:59 +03:00
// Destination. Either overlay or dest.
2014-05-24 20:12:07 +03:00
2015-06-21 00:13:45 +02:00
SDL_Texture * texture ;
2009-06-28 02:33:59 +03:00
SDL_Surface * dest ;
SDL_Rect destRect ; // valid when dest is used
SDL_Rect pos ; // destination on screen
2009-06-24 15:14:48 +03:00
2009-06-28 02:33:59 +03:00
int refreshWait ; // Wait several refresh before updating the image
int refreshCount ;
bool doLoop ; // loop through video
2018-07-25 00:36:48 +02:00
bool playVideo ( int x , int y , bool stopOnKey ) ;
2014-07-10 13:18:21 +03:00
bool open ( std : : string fname , bool loop , bool useOverlay = false , bool scale = false ) ;
2011-08-08 17:20:22 +03:00
2009-06-24 15:14:48 +03:00
public :
CVideoPlayer ( ) ;
~ CVideoPlayer ( ) ;
bool init ( ) ;
2014-07-10 13:18:21 +03:00
bool open ( std : : string fname , bool scale = false ) override ;
void close ( ) override ;
bool nextFrame ( ) override ; // display next frame
2009-06-24 15:14:48 +03:00
2014-07-10 13:18:21 +03:00
void show ( int x , int y , SDL_Surface * dst , bool update = true ) override ; //blit current frame
void redraw ( int x , int y , SDL_Surface * dst , bool update = true ) override ; //reblits buffer
void update ( int x , int y , SDL_Surface * dst , bool forceRedraw , bool update = true ) override ; //moves to next frame if appropriate, and blits it or blits only if redraw parameter is set true
2015-11-14 02:59:16 +02:00
2009-06-28 02:33:59 +03:00
// Opens video, calls playVideo, closes video; returns playVideo result (if whole video has been played)
2018-07-25 00:36:48 +02:00
bool openAndPlayVideo ( std : : string name , int x , int y , bool stopOnKey = false , bool scale = false ) override ;
2009-06-28 02:33:59 +03:00
2011-08-08 17:20:22 +03:00
//TODO:
2014-07-10 13:18:21 +03:00
bool wait ( ) override { return false ; } ;
int curFrame ( ) const override { return - 1 ; } ;
int frameCount ( ) const override { return - 1 ; } ;
2013-02-06 02:11:48 +03:00
// public to allow access from ffmpeg IO functions
std : : unique_ptr < CInputStream > data ;
2009-06-24 15:14:48 +03:00
} ;
2012-08-18 13:29:54 +03:00
# endif
2014-07-08 15:40:25 +03:00