Loading...
Searching...
No Matches
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>
7extern "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
23namespace Video
24{
25class SCORE_PLUGIN_MEDIA_EXPORT VideoDecoder final
26 : public VideoInterface
27 , public LibAVDecoder
28{
29public:
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) override;
41
42 AVFrame* dequeue_frame() noexcept override;
43 void release_frame(AVFrame*) noexcept override;
44
48 struct SeekHold
49 {
50 VideoDecoder* decoder{};
51 SeekHold(VideoDecoder* d) noexcept
52 : decoder{d}
53 {
54 }
55 SeekHold(const SeekHold&) = delete;
56 SeekHold& operator=(const SeekHold&) = delete;
57 ~SeekHold()
58 {
59 {
60 std::lock_guard l{decoder->m_condMut};
61 decoder->m_holdSeeks = false;
62 }
63 decoder->m_condVar.notify_one();
64 }
65 };
66 [[nodiscard]] SeekHold holdSeekRequests() noexcept
67 {
68 std::lock_guard l{m_condMut};
69 m_holdSeeks = true;
70 return SeekHold{this};
71 }
72
73private:
74 void buffer_thread() noexcept;
75 void close_file() noexcept;
76 bool seek_impl(int64_t dts) noexcept;
77 AVFrame* read_frame_impl() noexcept;
78 bool open_stream() noexcept;
79 void close_video() noexcept;
80
81 static const constexpr int frames_to_buffer = 16;
82
83 std::string m_inputFile;
84
85 std::thread m_thread;
86 std::mutex m_condMut;
87 std::condition_variable m_condVar;
89 bool m_holdSeeks{};
90
91 int64_t m_duration{}; // in flicks
92
93 std::atomic_int64_t m_seekTo = -1;
95 std::atomic_int64_t m_last_dequeued_pts = AV_NOPTS_VALUE;
96 std::atomic_int64_t m_dequeued = 0;
97
98 std::atomic_bool m_running{};
99};
100
101}
102#endif
Definition VideoDecoder.hpp:28
STL namespace.
Definition Rescale.hpp:48
Definition Rescale.hpp:61
Definition VideoDecoder.hpp:49
Definition VideoInterface.hpp:91