CVideoHandler.h 4.3 KB

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