CVideoHandler.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #pragma once
  2. struct SDL_Surface;
  3. class IVideoPlayer
  4. {
  5. public:
  6. virtual bool open(std::string name)=0; //true - succes
  7. virtual void close()=0;
  8. virtual bool nextFrame()=0;
  9. virtual void show(int x, int y, SDL_Surface *dst, bool update = true)=0;
  10. virtual void redraw(int x, int y, SDL_Surface *dst, bool update = true)=0; //reblits buffer
  11. virtual bool wait()=0;
  12. virtual int curFrame() const =0;
  13. virtual int frameCount() const =0;
  14. };
  15. class IMainVideoPlayer : public IVideoPlayer
  16. {
  17. public:
  18. std::string fname; //name of current video file (empty if idle)
  19. virtual void update(int x, int y, SDL_Surface *dst, bool forceRedraw, bool update = true){}
  20. virtual bool openAndPlayVideo(std::string name, int x, int y, SDL_Surface *dst, bool stopOnKey = false)
  21. {
  22. return false;
  23. }
  24. };
  25. class CEmptyVideoPlayer : public IMainVideoPlayer
  26. {
  27. public:
  28. virtual int curFrame() const {return -1;};
  29. virtual int frameCount() const {return -1;};
  30. virtual void redraw( int x, int y, SDL_Surface *dst, bool update = true ) {};
  31. virtual void show( int x, int y, SDL_Surface *dst, bool update = true ) {};
  32. virtual bool nextFrame() {return false;};
  33. virtual void close() {};
  34. virtual bool wait() {return false;};
  35. virtual bool open( std::string name ) {return false;};
  36. };
  37. #if defined(VCMI_WINDOWS) && (_MSC_VER < 1800 || !defined(USE_FFMPEG))
  38. #include <windows.h>
  39. #pragma pack(push,1)
  40. struct BINK_STRUCT
  41. {
  42. si32 width;
  43. si32 height;
  44. si32 frameCount;
  45. si32 currentFrame;
  46. si32 lastFrame;
  47. si32 FPSMul;
  48. si32 FPSDiv;
  49. si32 unknown0;
  50. ui8 flags;
  51. ui8 unknown1[260];
  52. si32 CurPlane; // current plane
  53. void *plane0; // posi32er to plane 0
  54. void *plane1; // posi32er to plane 1
  55. si32 unknown2;
  56. si32 unknown3;
  57. si32 yWidth; // Y plane width
  58. si32 yHeight; // Y plane height
  59. si32 uvWidth; // U&V plane width
  60. si32 uvHeight; // U&V plane height
  61. };
  62. #pragma pack(pop)
  63. typedef BINK_STRUCT* HBINK;
  64. class DLLHandler
  65. {
  66. public:
  67. std::string name;
  68. HINSTANCE dll;
  69. void Instantiate(const char *filename);
  70. const char *GetLibExtension();
  71. void *FindAddress(const char *symbol);
  72. DLLHandler();
  73. virtual ~DLLHandler(); //d-tor
  74. };
  75. typedef void*(__stdcall* BinkSetSoundSystem)(void * soundfun, void*);
  76. typedef HBINK(__stdcall* BinkOpen)(HANDLE bikfile, int flags);
  77. typedef void(__stdcall* BinkClose)(HBINK);
  78. //typedef si32(__stdcall* BinkGetPalette)(HBINK);
  79. typedef void(__stdcall* BinkNextFrame)(HBINK);
  80. typedef void(__stdcall* BinkDoFrame)(HBINK);
  81. typedef ui8(__stdcall* BinkWait)(HBINK);
  82. typedef si32(__stdcall* BinkCopyToBuffer)(HBINK, void* buffer, int stride, int height, int x, int y, int mode);
  83. class CBIKHandler : public DLLHandler, public IVideoPlayer
  84. {
  85. void allocBuffer(int Bpp = 0);
  86. void freeBuffer();
  87. public:
  88. HANDLE hBinkFile;
  89. HBINK hBink;
  90. char * buffer;
  91. int bufferSize;
  92. BinkSetSoundSystem binkSetSoundSystem;
  93. BinkOpen binkOpen;
  94. //BinkGetPalette getPalette;
  95. BinkNextFrame binkNextFrame;
  96. BinkDoFrame binkDoFrame;
  97. BinkCopyToBuffer binkCopyToBuffer;
  98. BinkWait binkWait;
  99. BinkClose binkClose;
  100. CBIKHandler();
  101. bool open(std::string name);
  102. void close();
  103. bool nextFrame();
  104. void show(int x, int y, SDL_Surface *dst, bool update = true);
  105. void redraw(int x, int y, SDL_Surface *dst, bool update = true); //reblits buffer
  106. bool wait();
  107. int curFrame() const;
  108. int frameCount() const;
  109. };
  110. //////////SMK Player ///////////////////////////////////////////////////////
  111. struct SmackStruct
  112. {
  113. si32 version;
  114. si32 width;
  115. si32 height;
  116. si32 frameCount;
  117. si32 mspf;
  118. ui8 unk1[88];
  119. ui8 palette[776];
  120. si32 currentFrame; // Starting with 0
  121. ui8 unk[56];
  122. ui32 fileHandle; // exact type is HANDLE in windows.h
  123. };
  124. // defines function pointer types
  125. typedef SmackStruct* (__stdcall* SmackOpen)(void* , ui32, si32 );
  126. typedef int (__stdcall* SmackDoFrame)( SmackStruct * );
  127. typedef void (__stdcall * SmackGoto )(SmackStruct *, int frameNumber);
  128. typedef void (__stdcall* SmackNextFrame)(SmackStruct*);
  129. typedef void (__stdcall* SmackClose)(SmackStruct*);
  130. typedef void (__stdcall* SmackToBuffer) (SmackStruct*, int, int, int, int, char *, ui32);
  131. typedef bool (__stdcall* SmackWait)(SmackStruct*);
  132. typedef void (__stdcall* SmackSoundOnOff) (SmackStruct*, bool);
  133. typedef int (__stdcall* SmackVolumePan)(SmackStruct *, int SmackTrack, int volume, int pan);
  134. class CSmackPlayer: public DLLHandler, public IVideoPlayer
  135. {
  136. public:
  137. SmackOpen ptrSmackOpen;
  138. SmackDoFrame ptrSmackDoFrame;
  139. SmackToBuffer ptrSmackToBuffer;
  140. SmackNextFrame ptrSmackNextFrame;
  141. SmackWait ptrSmackWait;
  142. SmackSoundOnOff ptrSmackSoundOnOff;
  143. SmackClose ptrSmackClose;
  144. SmackVolumePan ptrVolumePan;
  145. char *buffer, *buf;
  146. SmackStruct* data;
  147. CSmackPlayer();
  148. ~CSmackPlayer();
  149. bool open(std::string name);
  150. void close();
  151. bool nextFrame();
  152. void show(int x, int y, SDL_Surface *dst, bool update = true);
  153. void redraw(int x, int y, SDL_Surface *dst, bool update = true); //reblits buffer
  154. bool wait();
  155. int curFrame() const;
  156. int frameCount() const;
  157. };
  158. class CVidHandler;
  159. class CVideoPlayer : public IMainVideoPlayer
  160. {
  161. private:
  162. CSmackPlayer smkPlayer; //for .SMK
  163. CBIKHandler bikPlayer; //for .BIK
  164. IVideoPlayer *current; //points to bik or smk player, appropriate to type of currently played video
  165. bool first; //are we about to display the first frame (blocks update)
  166. public:
  167. CVideoPlayer(); //c-tor
  168. ~CVideoPlayer(); //d-tor
  169. bool open(std::string name);
  170. void close();
  171. bool nextFrame(); //move animation to the next frame
  172. void show(int x, int y, SDL_Surface *dst, bool update = true); //blit current frame
  173. void redraw(int x, int y, SDL_Surface *dst, bool update = true); //reblits buffer
  174. void update(int x, int y, SDL_Surface *dst, bool forceRedraw, bool update = true); //moves to next frame if appropriate, and blits it or blits only if redraw paremeter is set true
  175. bool wait(); //true if we should wait before displaying next frame (for keeping FPS)
  176. int curFrame() const; //current frame number <1, framecount>
  177. int frameCount() const;
  178. bool openAndPlayVideo(std::string name, int x, int y, SDL_Surface *dst, bool stopOnKey = false); //opens video, calls playVideo, closes video; returns playVideo result (if whole video has been played)
  179. bool playVideo(int x, int y, SDL_Surface *dst, bool stopOnKey = false); //plays whole opened video; returns: true when whole video has been shown, false when it has been interrupted
  180. };
  181. #else
  182. #ifndef DISABLE_VIDEO
  183. #include "../lib/filesystem/CInputStream.h"
  184. #include <SDL.h>
  185. #include <SDL_video.h>
  186. #if SDL_VERSION_ATLEAST(1,3,0) && !SDL_VERSION_ATLEAST(2,0,0)
  187. #include <SDL_compat.h>
  188. #endif
  189. extern "C" {
  190. #include <libavformat/avformat.h>
  191. #include <libswscale/swscale.h>
  192. }
  193. class CVideoPlayer : public IMainVideoPlayer
  194. {
  195. int stream; // stream index in video
  196. AVFormatContext *format;
  197. AVCodecContext *codecContext; // codec context for stream
  198. AVCodec *codec;
  199. AVFrame *frame;
  200. struct SwsContext *sws;
  201. AVIOContext * context;
  202. // Destination. Either overlay or dest.
  203. #ifdef VCMI_SDL1
  204. SDL_Overlay * overlay;
  205. #else
  206. SDL_Texture *texture;
  207. #endif
  208. SDL_Surface *dest;
  209. SDL_Rect destRect; // valid when dest is used
  210. SDL_Rect pos; // destination on screen
  211. int refreshWait; // Wait several refresh before updating the image
  212. int refreshCount;
  213. bool doLoop; // loop through video
  214. bool playVideo(int x, int y, SDL_Surface *dst, bool stopOnKey);
  215. bool open(std::string fname, bool loop, bool useOverlay = false);
  216. public:
  217. CVideoPlayer();
  218. ~CVideoPlayer();
  219. bool init();
  220. bool open(std::string fname);
  221. void close();
  222. bool nextFrame(); // display next frame
  223. void show(int x, int y, SDL_Surface *dst, bool update = true); //blit current frame
  224. void redraw(int x, int y, SDL_Surface *dst, bool update = true); //reblits buffer
  225. void update(int x, int y, SDL_Surface *dst, bool forceRedraw, bool update = true); //moves to next frame if appropriate, and blits it or blits only if redraw parameter is set true
  226. // Opens video, calls playVideo, closes video; returns playVideo result (if whole video has been played)
  227. bool openAndPlayVideo(std::string name, int x, int y, SDL_Surface *dst, bool stopOnKey = false);
  228. //TODO:
  229. bool wait(){return false;};
  230. int curFrame() const {return -1;};
  231. int frameCount() const {return -1;};
  232. // public to allow access from ffmpeg IO functions
  233. std::unique_ptr<CInputStream> data;
  234. };
  235. #endif
  236. #endif