Dewobble
Video motion stabilization with awareness of lens projection
filter_threaded.hpp
1 #ifndef DEWOBBLE_FILTER_HPP
2 #define DEWOBBLE_FILTER_HPP
3 
4 #include <CL/opencl.hpp>
5 #include <queue>
6 #include <thread>
7 #include <variant>
8 
9 #include "camera.hpp"
10 #include "filter_base.hpp"
11 #include "filter_config.hpp"
12 #include "rotation_detector.hpp"
13 #include "safe_queue.hpp"
14 #include "stabilizer.hpp"
15 
16 namespace dewobble
17 {
18 class MessageInit
19 {
20  public:
21  const FilterConfig filter_config;
22  MessageInit(FilterConfig filter_config) : filter_config(filter_config) {}
23 };
24 
25 class MessageInputFrame
26 {
27  public:
28  const cl_mem buffer;
29  void *extra;
30  MessageInputFrame(cl_mem buffer, void *extra) : buffer(buffer), extra(extra)
31  {
32  }
33 };
34 
35 class MessageReleaseOutputFrameBuffer
36 {
37  public:
38  const cl_mem buffer;
39  MessageReleaseOutputFrameBuffer(cl_mem buffer) : buffer(buffer) {}
40 };
41 
42 class MessageEndInput
43 {
44 };
45 
46 class MessageTerminate
47 {
48 };
49 
50 class MessageAcknowledgeFrame
51 {
52 };
53 
54 class MessageOutputFrame
55 {
56  public:
57  const cl_mem output_buffer;
58  const cl_mem input_buffer;
59  void *extra;
60  MessageOutputFrame(cl_mem output_buffer, cl_mem input_buffer, void *extra)
61  : output_buffer(output_buffer), input_buffer(input_buffer), extra(extra)
62  {
63  }
64 };
65 
66 typedef std::variant<
67  MessageInit,
68  MessageInputFrame,
69  MessageReleaseOutputFrameBuffer,
70  MessageEndInput,
71  MessageTerminate>
72  MessageInput;
73 typedef std::variant<MessageOutputFrame, MessageAcknowledgeFrame> MessageOutput;
74 
82 class FilterThreaded : public FilterBase
83 {
84  SafeQueue<MessageInput> m_input_queue;
85  SafeQueue<MessageOutput> m_output_queue;
86  std::thread m_worker_thread;
87  bool m_input_ended = false;
88  int m_num_unacknowledged_frames = 0;
89  int m_num_frames_in_flight = 0;
90  std::queue<void *> m_extra_queue;
91  std::queue<cl_mem> m_output_buffer_queue;
92  std::queue<cl_mem> m_input_buffer_queue;
93  void read_from_worker();
94 
95  public:
96  ~FilterThreaded();
100  FilterThreaded(FilterConfig filter_config);
101 
102  void push_frame(cl_mem input_buffer, void *extra);
103 
104  void end_input();
105 
106  bool frame_ready();
107 
108  void pull_frame(cl_mem *output_buffer, cl_mem *input_buffer, void **extra);
109 
110  void release_output_frame_buffer(cl_mem *output_buffer);
111 };
112 } // namespace dewobble
113 
114 #endif // DEWOBBLE_FILTER_HPP
Definition: filter_base.hpp:19
Definition: filter_config.hpp:83
Definition: filter_threaded.hpp:83
FilterThreaded(FilterConfig filter_config)
void pull_frame(cl_mem *output_buffer, cl_mem *input_buffer, void **extra)
void release_output_frame_buffer(cl_mem *output_buffer)
void push_frame(cl_mem input_buffer, void *extra)