CVideoHandler.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. bool startTimeInitialized;
  65. bool deactivationStartTimeHandling;
  66. std::chrono::steady_clock::time_point startTime;
  67. std::chrono::steady_clock::time_point deactivationStartTime;
  68. void prepareOutput(float scaleFactor, bool useTextureOutput);
  69. const int MAX_FRAMESKIP = 5;
  70. public:
  71. CVideoInstance();
  72. ~CVideoInstance();
  73. void openVideo();
  74. bool loadNextFrame();
  75. double timeStamp() final;
  76. bool videoEnded() final;
  77. Point size() final;
  78. void show(const Point & position, Canvas & canvas) final;
  79. void tick(uint32_t msPassed) final;
  80. void activate() final;
  81. void deactivate() final;
  82. };
  83. class CVideoPlayer final : public IVideoPlayer
  84. {
  85. void openVideoFile(CVideoInstance & state, const VideoPath & fname);
  86. public:
  87. std::unique_ptr<IVideoInstance> open(const VideoPath & name, float scaleFactor) final;
  88. std::pair<std::unique_ptr<ui8[]>, si64> getAudio(const VideoPath & videoToOpen) final;
  89. };
  90. #endif