mf
Media Framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
filter.h
Go to the documentation of this file.
1 #ifndef MF_FLOW_FILTER_H_
2 #define MF_FLOW_FILTER_H_
3 
4 #include "../flow/node.h"
5 #include "../flow/node_job.h"
6 #include "../flow/filter_node.h"
7 #include "../queue/frame.h"
8 #include "filter_parameter.h"
9 #include <string>
10 
11 namespace mf { namespace flow {
12 
14 
15 class filter {
16 private:
17  template<std::size_t Dim, typename Elem> class port;
18  template<std::size_t Dim, typename Elem> class input_port;
19  template<std::size_t Dim, typename Elem> class output_port;
20 
21  flow::filter_node& node_;
22 
23 public:
24  template<std::size_t Dim, typename Elem> using input_type = input_port<Dim, Elem>;
25  template<std::size_t Dim, typename Elem> using output_type = output_port<Dim, Elem>;
26  template<typename Value> using parameter_type = filter_parameter<Value>;
27 
28  explicit filter(filter_node& nd) : node_(nd) { }
29 
30  filter(const filter&) = delete;
31  filter& operator=(const filter&) = delete;
32 
33  filter_node& this_node() noexcept { return node_; }
34  const filter_node& this_node() const noexcept { return node_; }
35 
36  bool reached_end() const { return node_.reached_end(); }
37 
38  virtual ~filter() { }
39 
41  virtual void setup() { }
42 
44  virtual void pre_process(node_job&) { }
45 
47  virtual void process(node_job&) = 0;
48 };
49 
50 
52 
53 class source_filter : public filter {
54 public:
55  explicit source_filter(filter_node& nd, bool seekable = false, time_unit stream_duration = -1) :
56  filter(nd) {
57  nd.define_source_stream_properties(seekable, stream_duration);
58  }
59 };
60 
61 
63 
64 class sink_filter : public filter {
65 public:
66  explicit sink_filter(filter_node& nd) : filter(nd) { }
67 };
68 
69 
70 template<std::size_t Dim, typename Elem>
71 class filter::port {
72 public:
73  using elem_type = Elem;
74  constexpr static std::size_t dimension = Dim;
75 
76 private:
77  filter& filter_;
78 
79 protected:
80  static frame_format default_format() { return frame_format::default_format<Elem>(); }
81 
82  explicit port(filter& filt) : filter_(filt) { }
83  port(const port&) = delete;
84 
85 public:
86  const filter& this_filter() const { return filter_; }
87  filter& this_filter() { return filter_; }
88 };
89 
90 
91 template<std::size_t Dim, typename Elem>
92 class filter::output_port : public filter::port<Dim, Elem> {
93  using base = filter::port<Dim, Elem>;
94 
95 public:
96  using frame_shape_type = ndsize<Dim>;
97 
98 private:
99  flow::node_output& node_output_;
100  frame_shape_type frame_shape_;
101 
102 public:
103  explicit output_port(filter& filt) :
104  base(filt),
105  node_output_(filt.this_node().add_output(base::default_format())) { }
106 
107  node_output& this_node_output() { return node_output_; }
108  std::ptrdiff_t index() const { return node_output_.index(); }
109 
110  void define_frame_shape(const frame_shape_type& shp) {
111  frame_shape_ = shp;
112  node_output_.define_frame_length(shp.product());
113  }
114 
115  const frame_shape_type& frame_shape() const { return frame_shape_; }
116 };
117 
118 
119 template<std::size_t Dim, typename Elem>
120 class filter::input_port : public filter::port<Dim, Elem> {
121  using base = filter::port<Dim, Elem>;
122 
123 public:
124  using frame_shape_type = ndsize<Dim>;
125 
126 private:
127  flow::node_input& node_input_;
128  const frame_shape_type* shp_=nullptr; // TODO improve, add polymorph. connection with casting
129 
130 public:
131  explicit input_port(filter& filt, time_unit past_window = 0, time_unit future_window = 0) :
132  base(filt),
133  node_input_(filt.this_node().add_input(past_window, future_window)) { }
134 
135  node_input& this_node_input() { return node_input_; }
136  std::ptrdiff_t index() const { return node_input_.index(); }
137 
138  void set_activated(bool activated) {
139  node_input_.set_activated(activated);
140  }
141 
142  bool is_activated() {
143  return node_input_.is_activated();
144  }
145 
146  /*
147  template<std::size_t Output_dim, typename Output_elem>
148  void connect(output_port<Output_dim, Output_elem>& output);
149  */
150 
151  void connect(output_port<Dim, Elem>& output) {
152  node_input_.connect(output.this_node_output());
153  shp_ = &output.frame_shape();
154  }
155 
156 
157  const frame_shape_type& frame_shape() const { return *shp_; }
158 };
159 
160 
161 }}
162 
163 #endif
virtual void pre_process(node_job &)
Prepare for processing a frame.
Definition: filter.h:44
std::ptrdiff_t time_unit
Discrete time unit type.
Definition: common.h:52
bool reached_end() const noexcept
Definition: node.cc:141
const filter_node & this_node() const noexcept
Definition: filter.h:34
sink_filter(filter_node &nd)
Definition: filter.h:66
filter_node & this_node() noexcept
Definition: filter.h:33
input_port< Dim, Elem > input_type
Definition: filter.h:24
filter(filter_node &nd)
Definition: filter.h:28
Filter which performs concrete processing, base class.
Definition: filter.h:15
bool reached_end() const
Definition: filter.h:36
Node which delegates concrete frame processing to associated filter object.
Definition: filter_node.h:15
Source filter.
Definition: filter.h:53
source_filter(filter_node &nd, bool seekable=false, time_unit stream_duration=-1)
Definition: filter.h:55
void define_source_stream_properties(bool seekable, time_unit stream_duration=-1)
Definition: node.cc:70
filter & operator=(const filter &)=delete
Work unit of flow graph node.
Definition: node_job.h:13
Definition: filter_parameter.h:11
virtual ~filter()
Definition: filter.h:38
virtual void setup()
Set up the filter, called prior to any frame being processed.
Definition: filter.h:41
output_port< Dim, Elem > output_type
Definition: filter.h:25
Format information of type-erased frame of ndarray_view_generic.
Definition: frame_format.h:14
Sink filter.
Definition: filter.h:64
virtual void process(node_job &)=0
Process a frame.