CVideoHandler.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #ifndef DISABLE_VIDEO
  12. #include "../lib/Point.h"
  13. #include "IVideoPlayer.h"
  14. struct SDL_Surface;
  15. struct SDL_Texture;
  16. struct AVFormatContext;
  17. struct AVCodecContext;
  18. struct AVCodecParameters;
  19. struct AVCodec;
  20. struct AVFrame;
  21. struct AVIOContext;
  22. VCMI_LIB_NAMESPACE_BEGIN
  23. class CInputStream;
  24. class Point;
  25. VCMI_LIB_NAMESPACE_END
  26. class FFMpegStream : boost::noncopyable
  27. {
  28. std::unique_ptr<CInputStream> input;
  29. AVIOContext * context = nullptr;
  30. AVFormatContext * formatContext = nullptr;
  31. const AVCodec * codec = nullptr;
  32. AVCodecContext * codecContext = nullptr;
  33. int streamIndex = -1;
  34. AVFrame * frame = nullptr;
  35. protected:
  36. void openContext();
  37. void openCodec(int streamIndex);
  38. int findVideoStream() const;
  39. int findAudioStream() const;
  40. const AVCodecParameters * getCodecParameters() const;
  41. const AVCodecContext * getCodecContext() const;
  42. void decodeNextFrame();
  43. const AVFrame * getCurrentFrame() const;
  44. double getCurrentFrameEndTime() const;
  45. double getCurrentFrameDuration() const;
  46. public:
  47. virtual ~FFMpegStream();
  48. bool openInput(const VideoPath & fname);
  49. };
  50. class CAudioInstance final : public FFMpegStream
  51. {
  52. public:
  53. std::pair<std::unique_ptr<ui8[]>, si64> extractAudio(const VideoPath & videoToOpen);
  54. };
  55. class CVideoInstance final : public IVideoInstance, public FFMpegStream
  56. {
  57. friend class CVideoPlayer;
  58. struct SwsContext * sws = nullptr;
  59. SDL_Texture * textureRGB = nullptr;
  60. SDL_Texture * textureYUV = nullptr;
  61. SDL_Surface * surface = nullptr;
  62. Point dimensions;
  63. /// video playback start time point
  64. std::chrono::steady_clock::time_point startTime;
  65. std::chrono::steady_clock::time_point deactivationStartTime;
  66. void prepareOutput(float scaleFactor, bool useTextureOutput);
  67. const int MAX_FRAMESKIP = 5;
  68. public:
  69. ~CVideoInstance();
  70. void openVideo();
  71. bool loadNextFrame();
  72. double timeStamp() final;
  73. bool videoEnded() final;
  74. Point size() final;
  75. void show(const Point & position, Canvas & canvas) final;
  76. void tick(uint32_t msPassed) final;
  77. void activate() final;
  78. void deactivate() final;
  79. };
  80. class CVideoPlayer final : public IVideoPlayer
  81. {
  82. bool openAndPlayVideoImpl(const VideoPath & name, const Point & position, bool useOverlay, bool stopOnKey);
  83. void openVideoFile(CVideoInstance & state, const VideoPath & fname);
  84. public:
  85. void playSpellbookAnimation(const VideoPath & name, const Point & position) final;
  86. std::unique_ptr<IVideoInstance> open(const VideoPath & name, float scaleFactor) final;
  87. std::pair<std::unique_ptr<ui8[]>, si64> getAudio(const VideoPath & videoToOpen) final;
  88. };
  89. #endif