nndeploy C++ API  0.2.0
nndeploy C++ API
codec.h
Go to the documentation of this file.
1 #ifndef _NNDEPLOY_CODEC_CODEC_H_
2 #define _NNDEPLOY_CODEC_CODEC_H_
3 
4 #include "nndeploy/base/any.h"
5 #include "nndeploy/base/common.h"
7 #include "nndeploy/base/log.h"
8 #include "nndeploy/base/macro.h"
9 #include "nndeploy/base/object.h"
11 #include "nndeploy/base/status.h"
12 #include "nndeploy/base/string.h"
13 #include "nndeploy/dag/edge.h"
14 #include "nndeploy/dag/node.h"
15 #include "nndeploy/device/buffer.h"
16 #include "nndeploy/device/device.h"
18 #include "nndeploy/device/tensor.h"
19 
20 namespace nndeploy {
21 namespace codec {
22 
24  public:
25  Decode(const std::string &name) : dag::Node(name) {
26  node_type_ = dag::NodeType::kNodeTypeInput;
27  this->addRequiredParam("path_");
28  }
29  Decode(const std::string &name, std::vector<dag::Edge *> inputs,
30  std::vector<dag::Edge *> outputs)
31  : dag::Node(name) {
32  node_type_ = dag::NodeType::kNodeTypeInput;
33  // if (inputs.size() > 0) {
34  // NNDEPLOY_LOGE("Decode not support inputs");
35  // constructed_ = false;
36  // return;
37  // }
38  // if (outputs.size() > 1) {
39  // NNDEPLOY_LOGE("Decode only support one output");
40  // constructed_ = false;
41  // return;
42  // }
43  inputs_ = inputs;
44  outputs_ = outputs;
45  this->addRequiredParam("path_");
46  }
47  Decode(const std::string &name, base::CodecFlag flag) : dag::Node(name) {
48  flag_ = flag;
49  node_type_ = dag::NodeType::kNodeTypeInput;
50  // this->setOutputTypeInfo<cv::Mat>();
51  this->addRequiredParam("path_");
52  }
53  Decode(const std::string &name, std::vector<dag::Edge *> inputs,
54  std::vector<dag::Edge *> outputs, base::CodecFlag flag)
55  : dag::Node(name) {
56  flag_ = flag;
57  node_type_ = dag::NodeType::kNodeTypeInput;
58  // this->setOutputTypeInfo<cv::Mat>();
59  // if (inputs.size() > 0) {
60  // NNDEPLOY_LOGE("Decode not support inputs");
61  // constructed_ = false;
62  // return;
63  // }
64  // if (outputs.size() > 1) {
65  // NNDEPLOY_LOGE("Decode only support one output");
66  // constructed_ = false;
67  // return;
68  // }
69  inputs_ = inputs;
70  outputs_ = outputs;
71  this->addRequiredParam("path_");
72  }
73  virtual ~Decode() {}
74 
76  flag_ = flag;
77  return base::kStatusCodeOk;
78  }
79  base::CodecFlag getCodecFlag() { return flag_; }
80  virtual base::Status setPath(const std::string &path) = 0;
81 
82  void setSize(int size) {
83  if (size > 0) {
84  size_ = size;
85  }
86  }
87  int getSize() { return size_; }
88 
89  double getFps() { return fps_; };
90  int getWidth() { return width_; };
91  int getHeight() { return height_; };
92 
94  if (index_ < size_) {
96  } else {
97  if (size_ == 0) {
99  } else {
101  }
102  }
103  }
104 
105  void setLoopCount(int loop_count) {
106  if (loop_count > 0) {
107  loop_count_ = loop_count;
108  size_ = loop_count;
109  }
110  }
111 
112  virtual base::Status run() = 0;
113 
114  using dag::Node::serialize;
116  rapidjson::Value &json, rapidjson::Document::AllocatorType &allocator) {
117  base::Status status = dag::Node::serialize(json, allocator);
118  if (status != base::kStatusCodeOk) {
119  return status;
120  }
121  std::string flag_str = base::codecFlagToString(flag_);
122  // json.AddMember("flag_", rapidjson::Value(flag_str.c_str(), allocator),
123  // allocator);
124  json.AddMember("path_", rapidjson::Value(path_.c_str(), allocator),
125  allocator);
126  // json.AddMember("size_", size_, allocator);
127  // json.AddMember("fps_", fps_, allocator);
128  // json.AddMember("width_", width_, allocator);
129  // json.AddMember("height_", height_, allocator);
130  return status;
131  }
133  virtual base::Status deserialize(rapidjson::Value &json) {
134  base::Status status = dag::Node::deserialize(json);
135  if (status != base::kStatusCodeOk) {
136  return status;
137  }
138  // if (json.HasMember("flag_") && json["flag_"].IsString()) {
139  // base::CodecFlag flag =
140  // base::stringToCodecFlag(json["flag_"].GetString());
141  // this->setCodecFlag(flag);
142  // }
143  if (json.HasMember("size_") && json["size_"].IsInt()) {
144  int size = json["size_"].GetInt();
145  if (size > 0) {
146  this->setSize(size);
147  }
148  }
149  // if (json.HasMember("fps_") && json["fps_"].IsDouble()) {
150  // // this->setFps(json["fps_"].GetDouble());
151  // fps_ = json["fps_"].GetDouble();
152  // }
153  // if (json.HasMember("width_") && json["width_"].IsInt()) {
154  // // this->setWidth(json["width_"].GetInt());
155  // width_ = json["width_"].GetInt();
156  // }
157  // if (json.HasMember("height_") && json["height_"].IsInt()) {
158  // // this->setHeight(json["height_"].GetInt());
159  // height_ = json["height_"].GetInt();
160  // }
161  if (json.HasMember("path_") && json["path_"].IsString()) {
162  std::string path = json["path_"].GetString();
163  this->setPath(path);
164  }
165  return status;
166  }
167 
168  protected:
170  std::string path_ = "";
171  bool path_changed_ = false;
172  int size_ = 0;
173  double fps_ = 0.0;
174  int width_ = 0;
175  int height_ = 0;
176  int index_ = 0;
177  //
178  std::mutex path_mutex_;
179  std::condition_variable path_cv_;
180  bool path_ready_ = false;
181 };
182 
184  public:
185  Encode(const std::string &name) : dag::Node(name) {
186  node_type_ = dag::NodeType::kNodeTypeOutput;
187  // this->setInputTypeInfo<cv::Mat>();
188  this->addRequiredParam("path_");
189  }
190  Encode(const std::string &name, std::vector<dag::Edge *> inputs,
191  std::vector<dag::Edge *> outputs)
192  : dag::Node(name) {
193  // this->setInputTypeInfo<cv::Mat>();
194  node_type_ = dag::NodeType::kNodeTypeOutput;
195  // if (inputs.size() > 1) {
196  // NNDEPLOY_LOGE("Encode only support one input");
197  // constructed_ = false;
198  // return;
199  // }
200  // if (outputs.size() > 0) {
201  // NNDEPLOY_LOGE("Encode not support outputs");
202  // constructed_ = false;
203  // return;
204  // }
205  inputs_ = inputs;
206  outputs_ = outputs;
207  this->addRequiredParam("path_");
208  }
209  Encode(const std::string &name, base::CodecFlag flag) : dag::Node(name) {
210  flag_ = flag;
211  node_type_ = dag::NodeType::kNodeTypeOutput;
212  // this->setInputTypeInfo<cv::Mat>();
213  this->addRequiredParam("path_");
214  }
215  Encode(const std::string &name, std::vector<dag::Edge *> inputs,
216  std::vector<dag::Edge *> outputs, base::CodecFlag flag)
217  : dag::Node(name) {
218  flag_ = flag;
219  // this->setInputTypeInfo<cv::Mat>();
220  node_type_ = dag::NodeType::kNodeTypeOutput;
221  // if (inputs.size() > 1) {
222  // NNDEPLOY_LOGE("Encode only support one input");
223  // constructed_ = false;
224  // return;
225  // }
226  // if (outputs.size() > 0) {
227  // NNDEPLOY_LOGE("Encode not support outputs");
228  // constructed_ = false;
229  // return;
230  // }
231  inputs_ = inputs;
232  outputs_ = outputs;
233  this->addRequiredParam("path_");
234  }
235  virtual ~Encode() {};
236 
238  flag_ = flag;
239  return base::kStatusCodeOk;
240  }
241  base::CodecFlag getCodecFlag() { return flag_; }
242  // virtual base::Status setPath(const std::string &path) {
243  // path_ = path;
244  // path_changed_ = true;
245  // return base::kStatusCodeOk;
246  // }
247  // virtual base::Status setRefPath(const std::string &ref_path) {
248  // ref_path_ = ref_path;
249  // path_changed_ = true;
250  // return base::kStatusCodeOk;
251  // }
252  virtual base::Status setPath(const std::string &path) = 0;
253  virtual base::Status setRefPath(const std::string &ref_path) = 0;
254  void setFourcc(const std::string &fourcc) { fourcc_ = fourcc; }
255  void setFps(double fps) { fps_ = fps; };
256  void setWidth(int width) { width_ = width; };
257  void setHeight(int height) { height_ = height; };
258  void setSize(int size) {
259  if (size > 0) {
260  size_ = size;
261  }
262  }
263  int getSize() { return size_; }
264  int getIndex() { return index_; };
265 
266  virtual base::Status run() = 0;
267  using dag::Node::serialize;
269  rapidjson::Value &json, rapidjson::Document::AllocatorType &allocator) {
270  base::Status status = dag::Node::serialize(json, allocator);
271  if (status != base::kStatusCodeOk) {
272  return status;
273  }
274  std::string flag_str = base::codecFlagToString(flag_);
275  // json.AddMember("flag_", rapidjson::Value(flag_str.c_str(), allocator),
276  // allocator);
277  // json.AddMember("fps_", fps_, allocator);
278  // json.AddMember("width_", width_, allocator);
279  // json.AddMember("height_", height_, allocator);
280  // json.AddMember("size_", size_, allocator);
281  json.AddMember("path_", rapidjson::Value(path_.c_str(), allocator),
282  allocator);
283  if (flag_ == base::kCodecFlagVideo) {
284  json.AddMember("ref_path_",
285  rapidjson::Value(ref_path_.c_str(), allocator), allocator);
286  json.AddMember("fourcc_", rapidjson::Value(fourcc_.c_str(), allocator),
287  allocator);
288  }
289  return status;
290  }
292  virtual base::Status deserialize(rapidjson::Value &json) {
293  base::Status status = dag::Node::deserialize(json);
294  if (status != base::kStatusCodeOk) {
295  return status;
296  }
297  // if (json.HasMember("flag_") && json["flag_"].IsString()) {
298  // flag_ = base::stringToCodecFlag(json["flag_"].GetString());
299  // }
300  if (json.HasMember("fps_") && json["fps_"].IsNumber()) {
301  setFps(json["fps_"].GetDouble());
302  }
303  if (json.HasMember("width_") && json["width_"].IsInt()) {
304  setWidth(json["width_"].GetInt());
305  }
306  if (json.HasMember("height_") && json["height_"].IsInt()) {
307  setHeight(json["height_"].GetInt());
308  }
309  if (json.HasMember("size_") && json["size_"].IsInt()) {
310  int size = json["size_"].GetInt();
311  if (size > 0) {
312  this->setSize(size);
313  }
314  }
315  if (json.HasMember("fourcc_") && json["fourcc_"].IsString()) {
316  setFourcc(json["fourcc_"].GetString());
317  }
318  if (json.HasMember("ref_path_") && json["ref_path_"].IsString()) {
319  status = setRefPath(json["ref_path_"].GetString());
320  NNDEPLOY_RETURN_ON_NEQ(status, base::kStatusCodeOk, "setRefPath failed");
321  }
322 
323  if (json.HasMember("path_") && json["path_"].IsString()) {
324  status = setPath(json["path_"].GetString());
325  NNDEPLOY_RETURN_ON_NEQ(status, base::kStatusCodeOk, "setPath failed");
326  }
327 
328  return status;
329  }
330 
331  protected:
333  std::string path_ = "";
334  bool path_changed_ = false;
335  std::string ref_path_ = "";
336  // std::string fourcc_ = "MJPG";
337  // std::string fourcc_ = "AVC1";
338  std::string fourcc_ = "avc1";
339  double fps_ = 0.0;
340  int width_ = 0;
341  int height_ = 0;
342  int size_ = 0;
343  int index_ = 0;
344 };
345 
346 using createDecodeFunc = std::function<Decode *(
347  base::CodecFlag flag, const std::string &name, dag::Edge *output)>;
348 using createDecodeSharedPtrFunc = std::function<std::shared_ptr<Decode>(
349  base::CodecFlag flag, const std::string &name, dag::Edge *output)>;
350 
351 std::map<base::CodecType, createDecodeFunc> &getGlobalCreateDecodeFuncMap();
352 
353 std::map<base::CodecType, createDecodeSharedPtrFunc> &
355 
357  public:
359  createDecodeFunc func) {
360  getGlobalCreateDecodeFuncMap()[type] = func;
361  }
362 };
363 
365  public:
369  }
370 };
372  base::CodecFlag flag,
373  const std::string &name,
374  dag::Edge *output);
375 
376 extern NNDEPLOY_CC_API std::shared_ptr<Decode> createDecodeSharedPtr(
377  base::CodecType type, base::CodecFlag flag, const std::string &name,
378  dag::Edge *output);
379 
380 using createEncodeFunc = std::function<Encode *(
381  base::CodecFlag flag, const std::string &name, dag::Edge *input)>;
382 using createEncodeSharedPtrFunc = std::function<std::shared_ptr<Encode>(
383  base::CodecFlag flag, const std::string &name, dag::Edge *input)>;
384 
385 std::map<base::CodecType, createEncodeFunc> &getGlobalCreateEncodeFuncMap();
386 
387 std::map<base::CodecType, createEncodeSharedPtrFunc> &
389 
391  public:
393  createEncodeFunc func) {
394  getGlobalCreateEncodeFuncMap()[type] = func;
395  }
396 };
397 
399  public:
403  }
404 };
405 
407  base::CodecFlag flag,
408  const std::string &name,
409  dag::Edge *input);
410 
411 extern NNDEPLOY_CC_API std::shared_ptr<Encode> createEncodeSharedPtr(
412  base::CodecType type, base::CodecFlag flag, const std::string &name,
413  dag::Edge *input);
414 
415 } // namespace codec
416 } // namespace nndeploy
417 
418 #endif /* _NNDEPLOY_CODEC_CODEC_H_ */
Decode(const std::string &name)
Definition: codec.h:25
virtual ~Decode()
Definition: codec.h:73
virtual base::Status run()=0
Run node (pure virtual function)
virtual base::EdgeUpdateFlag updateInput()
Update input.
Definition: codec.h:93
std::condition_variable path_cv_
Definition: codec.h:179
Decode(const std::string &name, std::vector< dag::Edge * > inputs, std::vector< dag::Edge * > outputs)
Definition: codec.h:29
virtual base::Status serialize(rapidjson::Value &json, rapidjson::Document::AllocatorType &allocator)
Serialize to JSON.
Definition: codec.h:115
std::mutex path_mutex_
Definition: codec.h:178
Decode(const std::string &name, base::CodecFlag flag)
Definition: codec.h:47
Decode(const std::string &name, std::vector< dag::Edge * > inputs, std::vector< dag::Edge * > outputs, base::CodecFlag flag)
Definition: codec.h:53
void setSize(int size)
Definition: codec.h:82
base::Status setCodecFlag(base::CodecFlag flag)
Definition: codec.h:75
virtual base::Status deserialize(rapidjson::Value &json)
Deserialize from JSON.
Definition: codec.h:133
void setLoopCount(int loop_count)
Set loop count.
Definition: codec.h:105
virtual base::Status setPath(const std::string &path)=0
base::CodecFlag getCodecFlag()
Definition: codec.h:79
void setFourcc(const std::string &fourcc)
Definition: codec.h:254
base::Status setCodecFlag(base::CodecFlag flag)
Definition: codec.h:237
virtual base::Status deserialize(rapidjson::Value &json)
Deserialize from JSON.
Definition: codec.h:292
base::CodecFlag getCodecFlag()
Definition: codec.h:241
virtual base::Status run()=0
Run node (pure virtual function)
void setHeight(int height)
Definition: codec.h:257
Encode(const std::string &name, std::vector< dag::Edge * > inputs, std::vector< dag::Edge * > outputs)
Definition: codec.h:190
void setSize(int size)
Definition: codec.h:258
virtual base::Status setRefPath(const std::string &ref_path)=0
Encode(const std::string &name, std::vector< dag::Edge * > inputs, std::vector< dag::Edge * > outputs, base::CodecFlag flag)
Definition: codec.h:215
virtual ~Encode()
Definition: codec.h:235
virtual base::Status serialize(rapidjson::Value &json, rapidjson::Document::AllocatorType &allocator)
Serialize to JSON.
Definition: codec.h:268
void setWidth(int width)
Definition: codec.h:256
Encode(const std::string &name)
Definition: codec.h:185
Encode(const std::string &name, base::CodecFlag flag)
Definition: codec.h:209
void setFps(double fps)
Definition: codec.h:255
virtual base::Status setPath(const std::string &path)=0
TypeCreatelDecodeRegister(base::CodecType type, createDecodeFunc func)
Definition: codec.h:358
TypeCreatelDecodeSharedPtrRegister(base::CodecType type, createDecodeSharedPtrFunc func)
Definition: codec.h:366
TypeCreatelEncodeRegister(base::CodecType type, createEncodeFunc func)
Definition: codec.h:392
TypeCreatelEncodeSharedPtrRegister(base::CodecType type, createEncodeSharedPtrFunc func)
Definition: codec.h:400
Edge class in DAG graph for connecting nodes and transferring data.
Definition: edge.h:35
Node base class.
Definition: node.h:171
virtual std::string serialize()
Serialize to JSON string.
virtual base::Status deserialize(rapidjson::Value &json)
Deserialize from JSON.
#define NNDEPLOY_CC_API
api
Definition: macro.h:29
@ kStatusCodeOk
Definition: status.h:13
std::string codecFlagToString(CodecFlag src)
@ kCodecFlagImage
Definition: common.h:345
@ kCodecFlagVideo
Definition: common.h:347
@ kEdgeUpdateFlagComplete
Definition: common.h:366
@ kEdgeUpdateFlagTerminate
Definition: common.h:367
Encode * createEncode(base::CodecType type, base::CodecFlag flag, const std::string &name, dag::Edge *input)
Decode * createDecode(base::CodecType type, base::CodecFlag flag, const std::string &name, dag::Edge *output)
std::map< base::CodecType, createEncodeSharedPtrFunc > & getGlobalCreateEncodeSharedPtrFuncMap()
std::function< std::shared_ptr< Encode >(base::CodecFlag flag, const std::string &name, dag::Edge *input)> createEncodeSharedPtrFunc
Definition: codec.h:383
std::function< Encode *(base::CodecFlag flag, const std::string &name, dag::Edge *input)> createEncodeFunc
Definition: codec.h:381
std::map< base::CodecType, createDecodeSharedPtrFunc > & getGlobalCreateDecodeSharedPtrFuncMap()
std::function< std::shared_ptr< Decode >(base::CodecFlag flag, const std::string &name, dag::Edge *output)> createDecodeSharedPtrFunc
Definition: codec.h:349
std::shared_ptr< Decode > createDecodeSharedPtr(base::CodecType type, base::CodecFlag flag, const std::string &name, dag::Edge *output)
std::map< base::CodecType, createDecodeFunc > & getGlobalCreateDecodeFuncMap()
std::function< Decode *(base::CodecFlag flag, const std::string &name, dag::Edge *output)> createDecodeFunc
Definition: codec.h:347
std::map< base::CodecType, createEncodeFunc > & getGlobalCreateEncodeFuncMap()
std::shared_ptr< Encode > createEncodeSharedPtr(base::CodecType type, base::CodecFlag flag, const std::string &name, dag::Edge *input)
#define NNDEPLOY_RETURN_ON_NEQ(status, expected, str)
Definition: status.h:183