VideoDecoder.hpp
1 #pragma once
2 #include <Media/Libav.hpp>
3 #if SCORE_HAS_LIBAV
4 #include <Video/FrameQueue.hpp>
5 #include <Video/Rescale.hpp>
6 #include <Video/VideoInterface.hpp>
7 extern "C" {
8 #include <libavformat/avformat.h>
9 #include <libswscale/swscale.h>
10 }
11 
12 #include <ossia/detail/lockfree_queue.hpp>
13 
14 #include <score_plugin_media_export.h>
15 
16 #include <atomic>
17 #include <condition_variable>
18 #include <mutex>
19 #include <string>
20 #include <thread>
21 #include <vector>
22 
23 namespace Video
24 {
25 class SCORE_PLUGIN_MEDIA_EXPORT VideoDecoder final
26  : public VideoInterface
27  , public LibAVDecoder
28 {
29 public:
30  explicit VideoDecoder(DecoderConfiguration) noexcept;
31  ~VideoDecoder() noexcept;
32 
33  bool open(const std::string& inputFile) noexcept;
34  bool load(const std::string& inputFile) noexcept;
35 
36  const std::string& file() const noexcept { return m_inputFile; }
37 
38  int64_t duration() const noexcept;
39 
40  void seek(int64_t flicks);
41 
42  AVFrame* dequeue_frame() noexcept override;
43  void release_frame(AVFrame*) noexcept override;
44 
45 private:
46  void buffer_thread() noexcept;
47  void close_file() noexcept;
48  bool seek_impl(int64_t dts) noexcept;
49  AVFrame* read_frame_impl() noexcept;
50  bool open_stream() noexcept;
51  void close_video() noexcept;
52 
53  static const constexpr int frames_to_buffer = 16;
54 
55  std::string m_inputFile;
56 
57  std::thread m_thread;
58  std::mutex m_condMut;
59  std::condition_variable m_condVar;
60 
61  int64_t m_duration{}; // in flicks
62 
63  std::atomic_int64_t m_seekTo = -1;
64  std::atomic_int64_t m_last_dequeued_dts = 0;
65  std::atomic_int64_t m_dequeued = 0;
66 
67  std::atomic_bool m_running{};
68 };
69 
70 }
71 #endif
Definition: VideoDecoder.hpp:28
Definition: Rescale.hpp:36
Definition: Rescale.hpp:45
Definition: VideoInterface.hpp:31