CVideoHandler.h 4.3 KB

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