CVideoHandler.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 current progress, in seconds
  64. double frameTime = 0.0;
  65. void prepareOutput(bool scaleToScreenSize, bool useTextureOutput);
  66. public:
  67. ~CVideoInstance();
  68. void openVideo();
  69. bool loadNextFrame();
  70. bool videoEnded() final;
  71. Point size() final;
  72. void show(const Point & position, Canvas & canvas) final;
  73. void tick(uint32_t msPassed) final;
  74. };
  75. class CVideoPlayer final : public IVideoPlayer
  76. {
  77. bool openAndPlayVideoImpl(const VideoPath & name, const Point & position, bool useOverlay, bool scale, bool stopOnKey);
  78. void openVideoFile(CVideoInstance & state, const VideoPath & fname);
  79. public:
  80. bool playIntroVideo(const VideoPath & name) final;
  81. void playSpellbookAnimation(const VideoPath & name, const Point & position) final;
  82. std::unique_ptr<IVideoInstance> open(const VideoPath & name, bool scaleToScreen) final;
  83. std::pair<std::unique_ptr<ui8[]>, si64> getAudio(const VideoPath & videoToOpen) final;
  84. };
  85. #endif