CVideoHandler.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #pragma once
  2. struct SDL_Surface;
  3. class IVideoPlayer
  4. {
  5. public:
  6. virtual bool open(std::string name, bool scale = false)=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, bool scale = false)
  21. {
  22. return false;
  23. }
  24. };
  25. class CEmptyVideoPlayer : public IMainVideoPlayer
  26. {
  27. public:
  28. int curFrame() const override {return -1;};
  29. int frameCount() const override {return -1;};
  30. void redraw( int x, int y, SDL_Surface *dst, bool update = true ) override {};
  31. void show( int x, int y, SDL_Surface *dst, bool update = true ) override {};
  32. bool nextFrame() override {return false;};
  33. void close() override {};
  34. bool wait() override {return false;};
  35. bool open(std::string name, bool scale = false) override {return false;};
  36. };
  37. #ifndef DISABLE_VIDEO
  38. #include "../lib/filesystem/CInputStream.h"
  39. #include <SDL.h>
  40. #include <SDL_video.h>
  41. extern "C" {
  42. #include <libavformat/avformat.h>
  43. #include <libswscale/swscale.h>
  44. }
  45. //compatibility for libav 9.18 in ubuntu 14.04, 52.66.100 is ffmpeg 2.2.3
  46. #if (LIBAVUTIL_VERSION_INT < AV_VERSION_INT(52, 66, 100))
  47. inline AVFrame * av_frame_alloc()
  48. {
  49. return avcodec_alloc_frame();
  50. }
  51. inline void av_frame_free(AVFrame ** frame)
  52. {
  53. av_free(*frame);
  54. *frame = nullptr;
  55. }
  56. #endif // VCMI_USE_OLD_AVUTIL
  57. //fix for travis-ci
  58. #if (LIBAVUTIL_VERSION_INT < AV_VERSION_INT(52, 0, 0))
  59. #define AVPixelFormat PixelFormat
  60. #define AV_PIX_FMT_NONE PIX_FMT_NONE
  61. #define AV_PIX_FMT_YUV420P PIX_FMT_YUV420P
  62. #define AV_PIX_FMT_BGR565 PIX_FMT_BGR565
  63. #define AV_PIX_FMT_BGR24 PIX_FMT_BGR24
  64. #define AV_PIX_FMT_BGR32 PIX_FMT_BGR32
  65. #define AV_PIX_FMT_RGB565 PIX_FMT_RGB565
  66. #define AV_PIX_FMT_RGB24 PIX_FMT_RGB24
  67. #define AV_PIX_FMT_RGB32 PIX_FMT_RGB32
  68. #endif
  69. class CVideoPlayer : public IMainVideoPlayer
  70. {
  71. int stream; // stream index in video
  72. AVFormatContext *format;
  73. AVCodecContext *codecContext; // codec context for stream
  74. AVCodec *codec;
  75. AVFrame *frame;
  76. struct SwsContext *sws;
  77. AVIOContext * context;
  78. // Destination. Either overlay or dest.
  79. SDL_Texture *texture;
  80. SDL_Surface *dest;
  81. SDL_Rect destRect; // valid when dest is used
  82. SDL_Rect pos; // destination on screen
  83. int refreshWait; // Wait several refresh before updating the image
  84. int refreshCount;
  85. bool doLoop; // loop through video
  86. bool playVideo(int x, int y, SDL_Surface *dst, bool stopOnKey);
  87. bool open(std::string fname, bool loop, bool useOverlay = false, bool scale = false);
  88. public:
  89. CVideoPlayer();
  90. ~CVideoPlayer();
  91. bool init();
  92. bool open(std::string fname, bool scale = false) override;
  93. void close() override;
  94. bool nextFrame() override; // display next frame
  95. void show(int x, int y, SDL_Surface *dst, bool update = true) override; //blit current frame
  96. void redraw(int x, int y, SDL_Surface *dst, bool update = true) override; //reblits buffer
  97. 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
  98. // Opens video, calls playVideo, closes video; returns playVideo result (if whole video has been played)
  99. bool openAndPlayVideo(std::string name, int x, int y, SDL_Surface *dst, bool stopOnKey = false, bool scale = false) override;
  100. //TODO:
  101. bool wait() override {return false;};
  102. int curFrame() const override {return -1;};
  103. int frameCount() const override {return -1;};
  104. // public to allow access from ffmpeg IO functions
  105. std::unique_ptr<CInputStream> data;
  106. };
  107. #endif