archive.h
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
3 
4 #pragma once
5 
6 #include "types.h"
7 #include "core/streaming.h"
8 #include <atomic>
9 #include <array>
10 #include <math.h>
11 
12 namespace librealsense
13 {
14  class archive_interface;
15  class md_attribute_parser_base;
16  class frame;
17 
18  // multimap is necessary here in order to permit registration to some metadata value in multiple places in metadata
19  // as it is required for D405, in which exposure should be available from the same sensor both for depth and color frames
20  typedef std::multimap<rs2_frame_metadata_value, std::shared_ptr<md_attribute_parser_base>> metadata_parser_map;
21 
22  /*
23  Each frame is attached with a static header
24  This is a quick and dirty way to manage things like timestamp,
25  frame-number, metadata, etc... Things shared between all frame extensions
26  The point of this class is to be **fixed-sized**, avoiding per frame allocations
27  */
29  {
31  unsigned long long frame_number = 0;
33  rs2_time_t system_time = 0; // sys-clock at the time the frame was received from the backend
34  rs2_time_t frame_callback_started = 0; // time when the frame was sent to user callback
36  bool fisheye_ae_mode = false; // TODO: remove in future release
37  std::array<uint8_t, MAX_META_DATA_SIZE> metadata_blob;
38  rs2_time_t backend_timestamp = 0; // time when the frame arrived to the backend (OS dependent)
40  unsigned long long last_frame_number = 0;
41  bool is_blocking = false; // when running from recording, this bit indicates
42  // if the recorder was configured to realtime mode or not
43  // if true, this will force any queue receiving this frame not to drop it
44  uint32_t raw_size = 0; // The frame transmitted size (payload only)
45 
47 
48  frame_additional_data(double in_timestamp,
49  unsigned long long in_frame_number,
50  double in_system_time,
51  uint8_t md_size,
52  const uint8_t* md_buf,
53  double backend_time,
54  rs2_time_t last_timestamp,
55  unsigned long long last_frame_number,
56  bool in_is_blocking,
57  uint32_t transmitted_size = 0)
58  : timestamp(in_timestamp),
59  frame_number(in_frame_number),
60  system_time(in_system_time),
61  metadata_size(md_size),
62  backend_timestamp(backend_time),
63  last_timestamp(last_timestamp),
64  last_frame_number(last_frame_number),
65  is_blocking(in_is_blocking),
66  raw_size(transmitted_size)
67  {
68  // Copy up to 255 bytes to preserve metadata as raw data
69  if (metadata_size)
70  std::copy(md_buf, md_buf + std::min(md_size, MAX_META_DATA_SIZE), metadata_blob.begin());
71  }
72  };
73 
75  {
76  public:
77  virtual callback_invocation_holder begin_callback() = 0;
78 
79  virtual frame_interface* alloc_and_track(const size_t size, const frame_additional_data& additional_data, bool requires_memory) = 0;
80 
81  virtual std::shared_ptr<metadata_parser_map> get_md_parsers() const = 0;
82 
83  virtual void flush() = 0;
84 
85  virtual frame_interface* publish_frame(frame_interface* frame) = 0;
86  virtual void unpublish_frame(frame_interface* frame) = 0;
87  virtual void keep_frame(frame_interface* frame) = 0;
88  virtual ~archive_interface() = default;
89  };
90 
91  std::shared_ptr<archive_interface> make_archive(rs2_extension type,
92  std::atomic<uint32_t>* in_max_frame_queue_size,
93  std::shared_ptr<platform::time_service> ts,
94  std::shared_ptr<metadata_parser_map> parsers);
95 
96  // Define a movable but explicitly noncopyable buffer type to hold our frame data
98  {
99  public:
100  std::vector<byte> data;
102  std::shared_ptr<metadata_parser_map> metadata_parsers = nullptr;
103  explicit frame() : ref_count(0), owner(nullptr), on_release(),_kept(false) {}
104  frame(const frame& r) = delete;
106  : ref_count(r.ref_count.exchange(0)), owner(r.owner), on_release(), _kept(r._kept.exchange(false))
107  {
108  *this = std::move(r);
109  if (owner) metadata_parsers = owner->get_md_parsers();
110  if (r.metadata_parsers) metadata_parsers = std::move(r.metadata_parsers);
111  }
112 
113  frame& operator=(const frame& r) = delete;
115  {
116  data = move(r.data);
117  owner = r.owner;
118  ref_count = r.ref_count.exchange(0);
119  _kept = r._kept.exchange(false);
120  on_release = std::move(r.on_release);
121  additional_data = std::move(r.additional_data);
122  r.owner.reset();
123  if (owner) metadata_parsers = owner->get_md_parsers();
124  if (r.metadata_parsers) metadata_parsers = std::move(r.metadata_parsers);
125  return *this;
126  }
127 
128  virtual ~frame() { on_release.reset(); }
129  rs2_metadata_type get_frame_metadata(const rs2_frame_metadata_value& frame_metadata) const override;
130  bool supports_frame_metadata(const rs2_frame_metadata_value& frame_metadata) const override;
131  int get_frame_data_size() const override;
132  const byte* get_frame_data() const override;
133  rs2_time_t get_frame_timestamp() const override;
134  rs2_timestamp_domain get_frame_timestamp_domain() const override;
135  void set_timestamp(double new_ts) override { additional_data.timestamp = new_ts; }
136  unsigned long long get_frame_number() const override;
138  {
139  additional_data.timestamp_domain = timestamp_domain;
140  }
141 
142  rs2_time_t get_frame_system_time() const override;
143 
144  std::shared_ptr<stream_profile_interface> get_stream() const override { return stream; }
145  void set_stream(std::shared_ptr<stream_profile_interface> sp) override { stream = std::move(sp); }
146 
147  rs2_time_t get_frame_callback_start_time_point() const override;
148  void update_frame_callback_start_ts(rs2_time_t ts) override;
149 
150  void acquire() override { ref_count.fetch_add(1); }
151  void release() override;
152  void keep() override;
153 
154  frame_interface* publish(std::shared_ptr<archive_interface> new_owner) override;
155  void unpublish() override {}
156  void attach_continuation(frame_continuation&& continuation) override { on_release = std::move(continuation); }
157  void disable_continuation() override { on_release.reset(); }
158 
159  archive_interface* get_owner() const override { return owner.get(); }
160 
161  std::shared_ptr<sensor_interface> get_sensor() const override;
162  void set_sensor(std::shared_ptr<sensor_interface> s) override;
163 
164 
165  void log_callback_start(rs2_time_t timestamp) override;
166  void log_callback_end(rs2_time_t timestamp) const override;
167 
168  void mark_fixed() override { _fixed = true; }
169  bool is_fixed() const override { return _fixed; }
170 
171  void set_blocking(bool state) override { additional_data.is_blocking = state; }
172  bool is_blocking() const override { return additional_data.is_blocking; }
173 
174  private:
175  // TODO: check boost::intrusive_ptr or an alternative
176  std::atomic<int> ref_count; // the reference count is on how many times this placeholder has been observed (not lifetime, not content)
177  std::shared_ptr<archive_interface> owner; // pointer to the owner to be returned to by last observe
178  std::weak_ptr<sensor_interface> sensor;
180  bool _fixed = false;
181  std::atomic_bool _kept;
182  std::shared_ptr<stream_profile_interface> stream;
183  };
184 
185  class points : public frame
186  {
187  public:
188  float3* get_vertices();
189  void export_to_ply(const std::string& fname, const frame_holder& texture);
190  size_t get_vertex_count() const;
191  float2* get_texture_coordinates();
192  };
193 
195 
196  class composite_frame : public frame
197  {
198  public:
200 
202  {
203  auto frames = get_frames();
204  return frames[i];
205  }
206 
207  frame_interface** get_frames() const { return (frame_interface**)data.data(); }
208 
209  const frame_interface* first() const
210  {
211  return get_frame(0);
212  }
214  {
215  return get_frame(0);
216  }
217 
218  void keep() override
219  {
220  auto frames = get_frames();
221  for (size_t i = 0; i < get_embedded_frames_count(); i++)
222  if (frames[i]) frames[i]->keep();
223  frame::keep();
224  }
225 
226  size_t get_embedded_frames_count() const { return data.size() / sizeof(rs2_frame*); }
227 
228  // In the next section we make the composite frame "look and feel" like the first of its children
230  {
231  return first()->get_frame_metadata(frame_metadata);
232  }
234  {
235  return first()->supports_frame_metadata(frame_metadata);
236  }
237  int get_frame_data_size() const override
238  {
239  return first()->get_frame_data_size();
240  }
241  const byte* get_frame_data() const override
242  {
243  return first()->get_frame_data();
244  }
246  {
247  return first()->get_frame_timestamp();
248  }
250  {
251  return first()->get_frame_timestamp_domain();
252  }
253  unsigned long long get_frame_number() const override
254  {
255  if (first())
256  return first()->get_frame_number();
257  else
258  return frame::get_frame_number();
259  }
261  {
262  return first()->get_frame_system_time();
263  }
264  std::shared_ptr<sensor_interface> get_sensor() const override
265  {
266  return first()->get_sensor();
267  }
268  };
269 
271 
272  class video_frame : public frame
273  {
274  public:
276  : frame(), _width(0), _height(0), _bpp(0), _stride(0)
277  {}
278 
279  int get_width() const { return _width; }
280  int get_height() const { return _height; }
281  int get_stride() const { return _stride; }
282  int get_bpp() const { return _bpp; }
283 
284  void assign(int width, int height, int stride, int bpp)
285  {
286  _width = width;
287  _height = height;
288  _stride = stride;
289  _bpp = bpp;
290  }
291 
292  private:
293  int _width, _height, _bpp, _stride;
294  };
295 
297 
298  class depth_frame : public video_frame
299  {
300  public:
301  depth_frame() : video_frame(), _depth_units()
302  {
303  }
304 
305  frame_interface* publish(std::shared_ptr<archive_interface> new_owner) override
306  {
307  _depth_units = optional_value<float>();
308  return video_frame::publish(new_owner);
309  }
310 
311  void keep() override
312  {
313  if (_original) _original->keep();
315  }
316 
317  float get_distance(int x, int y) const
318  {
319  // If this frame does not itself contain Z16 depth data,
320  // fall back to the original frame it was created from
321  if (_original && get_stream()->get_format() != RS2_FORMAT_Z16)
322  return((depth_frame*)_original.frame)->get_distance(x, y);
323 
324  uint64_t pixel = 0;
325  switch (get_bpp() / 8) // bits per pixel
326  {
327  case 1: pixel = get_frame_data()[y*get_width() + x]; break;
328  case 2: pixel = reinterpret_cast<const uint16_t*>(get_frame_data())[y*get_width() + x]; break;
329  case 4: pixel = reinterpret_cast<const uint32_t*>(get_frame_data())[y*get_width() + x]; break;
330  case 8: pixel = reinterpret_cast<const uint64_t*>(get_frame_data())[y*get_width() + x]; break;
331  default: throw std::runtime_error(to_string() << "Unrecognized depth format " << int(get_bpp() / 8) << " bytes per pixel");
332  }
333 
334  return pixel * get_units();
335  }
336 
337  float get_units() const
338  {
339  if (!_depth_units)
340  _depth_units = query_units(get_sensor());
341  return _depth_units.value();
342  }
343 
345  {
346  _original = std::move(h);
347  attach_continuation(frame_continuation([this]() {
348  if (_original)
349  {
350  _original = {};
351  }
352  }, nullptr));
353  }
354 
355  protected:
356  static float query_units(const std::shared_ptr<sensor_interface>& sensor)
357  {
358  if (sensor != nullptr)
359  {
360  try
361  {
362  auto depth_sensor = As<librealsense::depth_sensor>(sensor);
363  if (depth_sensor != nullptr)
364  {
365  return depth_sensor->get_depth_scale();
366  }
367  else
368  {
369  //For playback sensors
370  auto extendable = As<librealsense::extendable_interface>(sensor);
371  if (extendable && extendable->extend_to(TypeToExtension<librealsense::depth_sensor>::value, (void**)(&depth_sensor)))
372  {
373  return depth_sensor->get_depth_scale();
374  }
375  }
376  }
377  catch (const std::exception& e)
378  {
379  LOG_ERROR("Failed to query depth units from sensor. " << e.what());
380  }
381  catch (...)
382  {
383  LOG_ERROR("Failed to query depth units from sensor");
384  }
385  }
386  else
387  {
388  LOG_WARNING("sensor was nullptr");
389  }
390 
391  return 0;
392  }
393 
396  };
397 
399 
400  // Disparity frame provides an alternative representation of the depth data for stereo-based depth sensors
401  // In addition for the depth frame API it allows to query the stereoscopic baseline required to transform depth to disparity and vice versa
403  {
404  public:
406  {
407  }
408 
409  // TODO Refactor to framemetadata
410  float get_stereo_baseline(void) const { return query_stereo_baseline(this->get_sensor()); }
411 
412  protected:
413 
414  static float query_stereo_baseline(const std::shared_ptr<sensor_interface>& sensor)
415  {
416  if (sensor != nullptr)
417  {
418  try
419  {
420  auto stereo_sensor = As<librealsense::depth_stereo_sensor>(sensor);
421  if (stereo_sensor != nullptr)
422  {
423  return stereo_sensor->get_stereo_baseline_mm();
424  }
425  else
426  {
427  //For playback sensors
428  auto extendable = As<librealsense::extendable_interface>(sensor);
429  if (extendable && extendable->extend_to(TypeToExtension<librealsense::depth_stereo_sensor>::value, (void**)(&stereo_sensor)))
430  {
431  return stereo_sensor->get_stereo_baseline_mm();
432  }
433  }
434  }
435  catch (const std::exception& e)
436  {
437  LOG_ERROR("Failed to query stereo baseline from sensor. " << e.what());
438  }
439  catch (...)
440  {
441  LOG_ERROR("Failed to query stereo baseline from sensor");
442  }
443  }
444  else
445  {
446  LOG_WARNING("sensor was nullptr");
447  }
448 
449  return 0;
450  }
451  };
452 
454 
455  class motion_frame : public frame
456  {
457  public:
459  {}
460  };
461 
463 
464  class pose_frame : public frame
465  {
466  public:
467  // pose frame data buffer is pose info struct
468  struct pose_info
469  {
478  };
479 
480  pose_frame() : frame() {}
481 
482  float3 get_translation() const { return reinterpret_cast<const pose_info*>(get_frame_data())->translation; }
483  float3 get_velocity() const { return reinterpret_cast<const pose_info*>(get_frame_data())->velocity; }
484  float3 get_acceleration() const { return reinterpret_cast<const pose_info*>(get_frame_data())->acceleration; }
485  float4 get_rotation() const { return reinterpret_cast<const pose_info*>(get_frame_data())->rotation; }
486  float3 get_angular_velocity() const { return reinterpret_cast<const pose_info*>(get_frame_data())->angular_velocity; }
487  float3 get_angular_acceleration() const { return reinterpret_cast<const pose_info*>(get_frame_data())->angular_acceleration; }
488  uint32_t get_tracker_confidence() const { return reinterpret_cast<const pose_info*>(get_frame_data())->tracker_confidence; }
489  uint32_t get_mapper_confidence() const { return reinterpret_cast<const pose_info*>(get_frame_data())->mapper_confidence; }
490  };
491 
493 }
int get_width() const
Definition: archive.h:279
const frame_interface * first() const
Definition: archive.h:209
std::shared_ptr< stream_profile_interface > get_stream() const override
Definition: archive.h:144
float4 get_rotation() const
Definition: archive.h:485
std::atomic_bool _kept
Definition: archive.h:181
GLint y
void disable_continuation() override
Definition: archive.h:157
frame_interface * get_frame(int i) const
Definition: archive.h:201
float3 get_acceleration() const
Definition: archive.h:484
GLdouble s
frame_interface * publish(std::shared_ptr< archive_interface > new_owner) override
Definition: archive.h:305
frame(frame &&r)
Definition: archive.h:105
void keep() override
Definition: archive.h:218
void export_to_ply(Mat points, Mat normals)
Definition: rs-kinfu.cpp:101
std::atomic< int > ref_count
Definition: archive.h:176
int get_frame_data_size() const override
Definition: archive.h:237
void keep() override
Definition: archive.h:311
std::shared_ptr< archive_interface > make_archive(rs2_extension type, std::atomic< uint32_t > *in_max_frame_queue_size, std::shared_ptr< platform::time_service > ts, std::shared_ptr< metadata_parser_map > parsers)
Definition: archive.cpp:158
#define LOG_WARNING(...)
Definition: src/types.h:241
rs2_time_t get_frame_system_time() const override
Definition: archive.h:260
const uint8_t MAX_META_DATA_SIZE
Definition: backend.h:34
unsigned long long get_frame_number() const override
Definition: archive.cpp:298
std::shared_ptr< stream_profile_interface > stream
Definition: archive.h:182
std::shared_ptr< archive_interface > owner
Definition: archive.h:177
void mark_fixed() override
Definition: archive.h:168
float3 get_angular_velocity() const
Definition: archive.h:486
optional_value< float > _depth_units
Definition: archive.h:395
unsigned short uint16_t
Definition: stdint.h:79
void set_timestamp_domain(rs2_timestamp_domain timestamp_domain) override
Definition: archive.h:137
GLsizei const GLchar *const * string
size_t get_embedded_frames_count() const
Definition: archive.h:226
void set_original(frame_holder h)
Definition: archive.h:344
void unpublish() override
Definition: archive.h:155
archive_interface * get_owner() const override
Definition: archive.h:159
GLuint GLuint stream
Definition: glext.h:1790
#define LRS_EXTENSION_API
Definition: src/types.h:20
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:1960
unsigned char uint8_t
Definition: stdint.h:78
e
Definition: rmse.py:177
The texture class.
Definition: example.hpp:402
frame & operator=(frame &&r)
Definition: archive.h:114
frame_additional_data additional_data
Definition: archive.h:101
float3 get_angular_acceleration() const
Definition: archive.h:487
virtual float get_depth_scale() const =0
float get_distance(int x, int y) const
Definition: archive.h:317
void set_timestamp(double new_ts) override
Definition: archive.h:135
GLsizeiptr size
GLdouble GLdouble r
float3 get_translation() const
Definition: archive.h:482
std::shared_ptr< sensor_interface > get_sensor() const override
Definition: archive.h:264
frame_interface ** get_frames() const
Definition: archive.h:207
GLdouble x
unsigned int uint32_t
Definition: stdint.h:80
std::vector< rs2::frame > get_frames(std::vector< rs2::sensor > sensors)
void set_stream(std::shared_ptr< stream_profile_interface > sp) override
Definition: archive.h:145
std::multimap< rs2_frame_metadata_value, std::shared_ptr< md_attribute_parser_base > > metadata_parser_map
Definition: archive.h:16
GLint GLsizei GLsizei height
rs2_time_t get_frame_timestamp() const override
Definition: archive.h:245
float3 get_velocity() const
Definition: archive.h:483
void attach_continuation(frame_continuation &&continuation) override
Definition: archive.h:156
unsigned __int64 uint64_t
Definition: stdint.h:90
unsigned long long frame_number
Definition: archive.h:31
void set_blocking(bool state) override
Definition: archive.h:171
GLdouble GLdouble GLint stride
GLint first
static float query_units(const std::shared_ptr< sensor_interface > &sensor)
Definition: archive.h:356
#define LOG_ERROR(...)
Definition: src/types.h:242
bool supports_frame_metadata(const rs2_frame_metadata_value &frame_metadata) const override
Definition: archive.h:233
uint32_t get_tracker_confidence() const
Definition: archive.h:488
int get_stride() const
Definition: archive.h:281
const byte * get_frame_data() const override
Definition: archive.h:241
std::shared_ptr< metadata_parser_map > metadata_parsers
Definition: archive.h:102
uint32_t get_mapper_confidence() const
Definition: archive.h:489
frame_continuation on_release
Definition: archive.h:179
float get_stereo_baseline(void) const
Definition: archive.h:410
unsigned long long get_frame_number() const override
Definition: archive.h:253
frame_additional_data(double in_timestamp, unsigned long long in_frame_number, double in_system_time, uint8_t md_size, const uint8_t *md_buf, double backend_time, rs2_time_t last_timestamp, unsigned long long last_frame_number, bool in_is_blocking, uint32_t transmitted_size=0)
Definition: archive.h:48
float get_units() const
Definition: archive.h:337
void keep() override
Definition: archive.cpp:201
void assign(int width, int height, int stride, int bpp)
Definition: archive.h:284
fname
Definition: rmse.py:13
unsigned char byte
Definition: src/types.h:52
std::weak_ptr< sensor_interface > sensor
Definition: archive.h:178
frame_interface * publish(std::shared_ptr< archive_interface > new_owner) override
Definition: archive.cpp:209
rs2_extension
Specifies advanced interfaces (capabilities) objects may implement.
Definition: rs_types.h:166
rs2_metadata_type get_frame_metadata(const rs2_frame_metadata_value &frame_metadata) const override
Definition: archive.h:229
bool is_blocking() const override
Definition: archive.h:172
long long rs2_metadata_type
Definition: rs_types.h:301
GLenum type
rs2_timestamp_domain timestamp_domain
Definition: archive.h:32
int min(int a, int b)
Definition: lz4s.c:73
virtual ~frame()
Definition: archive.h:128
bool is_fixed() const override
Definition: archive.h:169
typename::boost::move_detail::remove_reference< T >::type && move(T &&t) BOOST_NOEXCEPT
frame_holder _original
Definition: archive.h:394
int i
frame_interface * first()
Definition: archive.h:213
static float query_stereo_baseline(const std::shared_ptr< sensor_interface > &sensor)
Definition: archive.h:414
double rs2_time_t
Definition: rs_types.h:300
rs2_timestamp_domain get_frame_timestamp_domain() const override
Definition: archive.h:249
std::array< uint8_t, MAX_META_DATA_SIZE > metadata_blob
Definition: archive.h:37
int get_bpp() const
Definition: archive.h:282
void acquire() override
Definition: archive.h:150
Definition: parser.hpp:150
rs2_frame_metadata_value
Per-Frame-Metadata is the set of read-only properties that might be exposed for each individual frame...
Definition: rs_frame.h:29
int get_height() const
Definition: archive.h:280
MAP_EXTENSION(RS2_EXTENSION_POINTS, librealsense::points)
struct rs2_frame rs2_frame
Definition: rs_types.h:261
GLint GLsizei width
void copy(void *dst, void const *src, size_t size)
Definition: types.cpp:836
std::vector< byte > data
Definition: archive.h:100
unsigned long long last_frame_number
Definition: archive.h:40
std::string to_string(T value)
rs2_timestamp_domain
Specifies the clock in relation to which the frame timestamp was measured.
Definition: rs_frame.h:19


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:45:06