frame_impl.cpp
Go to the documentation of this file.
1 /*
2  * BSD 3-Clause License
3  *
4  * Copyright (c) 2019, Analog Devices, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "frame_impl.h"
35 
36 #include <algorithm>
37 #include <cmath>
38 #include <cstring>
39 #include <fstream>
40 #ifdef USE_GLOG
41 #include <glog/logging.h>
42 #else
43 #include <aditof/log.h>
44 #endif
45 #include <memory>
46 #include <unordered_map>
47 
48 static const int skMetaDataBytesCount = 128;
49 
51  std::unordered_map<std::string, uint16_t *> m_dataLocations;
52  std::shared_ptr<uint16_t[]> m_allData;
54 };
55 
56 FrameImpl::FrameImpl() : m_implData(std::make_unique<FrameImpl::ImplData>()){};
57 FrameImpl::~FrameImpl() = default;
58 
61  memcpy(m_implData->m_allData.get(), op.m_implData->m_allData.get(),
62  m_implData->allDataNbBytes);
63  m_details = op.m_details;
64 }
65 
67  if (this != &op) {
69  memcpy(m_implData->m_allData.get(), op.m_implData->m_allData.get(),
70  m_implData->allDataNbBytes);
71  m_details = op.m_details;
72  }
73 
74  return *this;
75 }
76 
78  using namespace aditof;
79  Status status = Status::OK;
80 
81  if (details == m_details) {
82  LOG(INFO) << "Same details provided. Doing nothing.";
83  return status;
84  }
85 
86  allocFrameData(details);
87  m_details = details;
88 
89  return status;
90 }
91 
93  details = m_details;
94 
95  return aditof::Status::OK;
96 }
97 
100  aditof::FrameDataDetails &details) const {
101  auto detailsIter =
102  std::find_if(m_details.dataDetails.begin(), m_details.dataDetails.end(),
103  [&dataType](const aditof::FrameDataDetails &details) {
104  return dataType == details.type;
105  });
106  if (detailsIter == m_details.dataDetails.end()) {
107  LOG(WARNING) << "Could not find any details for type: " << dataType;
109  }
110 
111  details = *detailsIter;
112 
113  return aditof::Status::OK;
114 }
115 
117  uint16_t **dataPtr) {
118  using namespace aditof;
119  if (m_implData->m_dataLocations.count(dataType) > 0) {
120  *dataPtr = m_implData->m_dataLocations[dataType];
121  } else {
122  dataPtr = nullptr;
123  if (dataType !=
124  "header") // TO DO: Silence this for now, handle it later
125  LOG(ERROR) << dataType << " is not supported by this frame!";
127  }
128 
129  return Status::OK;
130 }
131 
134  const std::string name) {
135  auto frame_detail =
136  std::find_if(details.dataDetails.begin(), details.dataDetails.end(),
137  [&name](const aditof::FrameDataDetails frame_detail) {
138  return frame_detail.type == name;
139  });
140 
141  if (frame_detail == details.dataDetails.end()) {
142  LOG(WARNING) << "Could not find any attribute with name: " << name;
143  }
144 
145  return *frame_detail;
146 }
147 
149  using namespace aditof;
150  unsigned int totalSize = 0;
151  unsigned int pos = 0;
152  uint16_t embed_hdr_length = 0;
153  uint8_t total_captures = 0;
154 
155  auto getSubframeSize = [embed_hdr_length,
156  total_captures](FrameDataDetails frameDetail) {
157  if (frameDetail.type == "header") {
158  return (unsigned long int)(embed_hdr_length / 2) * total_captures;
159  } else if (frameDetail.type == "xyz") {
160  return (unsigned long int)(frameDetail.height * frameDetail.width *
161  sizeof(Point3I_sdk) / 2);
162  } else if (frameDetail.type == "conf") {
163  return (unsigned long int)(frameDetail.height * frameDetail.width *
164  2);
165  } else {
166  return (unsigned long int)frameDetail.height * frameDetail.width;
167  }
168  };
169 
170  //compute total size TODO this could be precomputed TBD @dNechita
171  for (FrameDataDetails frameDetail : details.dataDetails) {
172  totalSize += getSubframeSize(frameDetail);
173  }
174 
175  //store pointers to the contents described by FrameDetails
176  m_implData->m_allData = std::shared_ptr<uint16_t[]>(
177  new uint16_t[totalSize], // Allocate the array
178  [](uint16_t *p) {
179  delete[] p;
180  } // Custom deleter to ensure correct deallocation
181  );
182 
183  //TODO wouldn`t it be safer to store relative position to .get() instead of absolute address ? TBD @dNechita
184  m_implData->m_dataLocations.emplace(
185  "frameData", m_implData->m_allData.get()); //frame data
186  for (FrameDataDetails frameDetail : details.dataDetails) {
187  m_implData->m_dataLocations.emplace(
188  frameDetail.type, m_implData->m_allData.get() + pos); //raw data
189 
190  pos += getSubframeSize(frameDetail);
191  }
192 }
193 
195  using namespace aditof;
196 
197  uint8_t *header;
198  if (m_implData->m_dataLocations.count("metadata") > 0) {
199  header = reinterpret_cast<uint8_t *>(
200  m_implData->m_dataLocations["metadata"]);
201  } else {
203  }
204 
205  memcpy(&metadata, header, sizeof(Metadata));
206 
207  return aditof::Status::OK;
208 }
INFO
const int INFO
Definition: log_severity.h:59
name
GLuint const GLchar * name
Definition: glcorearb.h:3055
FrameImpl::allocFrameData
void allocFrameData(const aditof::FrameDetails &details)
Definition: frame_impl.cpp:148
FrameImpl::ImplData::allDataNbBytes
size_t allDataNbBytes
Definition: frame_impl.cpp:53
ERROR
const int ERROR
Definition: log_severity.h:60
FrameImpl::getFrameDetailByName
aditof::FrameDataDetails getFrameDetailByName(const aditof::FrameDetails &details, const std::string name)
Definition: frame_impl.cpp:133
FrameImpl::getDataDetails
aditof::Status getDataDetails(const std::string &dataType, aditof::FrameDataDetails &details) const
Definition: frame_impl.cpp:99
FrameImpl::m_details
aditof::FrameDetails m_details
Definition: frame_impl.h:63
benchmarks.util.result_uploader.metadata
def metadata
Definition: result_uploader.py:97
log.h
FrameImpl::getDetails
aditof::Status getDetails(aditof::FrameDetails &details) const
Definition: frame_impl.cpp:92
FrameImpl::ImplData::m_allData
std::shared_ptr< uint16_t[]> m_allData
Definition: frame_impl.cpp:52
FrameImpl::ImplData
Definition: frame_impl.cpp:50
aditof::FrameDataDetails::type
std::string type
The type of data that can be found in a frame. For example it could be depth data or IR data,...
Definition: frame_definitions.h:53
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
aditof::Status::UNAVAILABLE
@ UNAVAILABLE
The requested action or resource is unavailable.
WARNING
const int WARNING
Definition: log_severity.h:59
aditof::Point3I_sdk
Holds the xyz values of a frame.
Definition: frame_definitions.h:136
FrameImpl::getMetadataStruct
aditof::Status getMetadataStruct(aditof::Metadata &metadata) const
Definition: frame_impl.cpp:194
FrameImpl::m_implData
std::unique_ptr< ImplData > m_implData
Definition: frame_impl.h:65
aditof::FrameDetails::dataDetails
std::vector< FrameDataDetails > dataDetails
A frame can have multiple types of data. For example it could hold data about depth and/or data about...
Definition: frame_definitions.h:104
aditof::FrameDetails
Describes the properties of a frame.
Definition: frame_definitions.h:93
aditof::Metadata
Contains all of the metadata components.
Definition: frame_definitions.h:147
aditof
Namespace aditof.
Definition: adsd_errs.h:40
skMetaDataBytesCount
static const int skMetaDataBytesCount
Definition: frame_impl.cpp:48
google::protobuf::util::error::OK
@ OK
Definition: status.h:47
p
const char * p
Definition: gmock-matchers_test.cc:3863
aditof::FrameDataDetails
Describes the properties of a data that embedded within the frame.
Definition: frame_definitions.h:48
FrameImpl::operator=
FrameImpl & operator=(const FrameImpl &op)
Definition: frame_impl.cpp:66
google::protobuf::util::error::INVALID_ARGUMENT
@ INVALID_ARGUMENT
Definition: status.h:50
FrameImpl::~FrameImpl
~FrameImpl()
aditof::Status::INVALID_ARGUMENT
@ INVALID_ARGUMENT
Invalid arguments provided.
aditof::Status
Status
Status of any operation that the TOF sdk performs.
Definition: status_definitions.h:48
LOG
#define LOG(x)
Definition: sdk/include/aditof/log.h:72
FrameImpl::setDetails
aditof::Status setDetails(const aditof::FrameDetails &details)
Definition: frame_impl.cpp:77
aditof::Status::OK
@ OK
Success.
FrameImpl::ImplData::m_dataLocations
std::unordered_map< std::string, uint16_t * > m_dataLocations
Definition: frame_impl.cpp:51
std
FrameImpl
Definition: frame_impl.h:43
FrameImpl::getData
aditof::Status getData(const std::string &dataType, uint16_t **dataPtr)
Definition: frame_impl.cpp:116
frame_operations.h
frame_impl.h
header
const std::string header
FrameImpl::FrameImpl
FrameImpl()
Definition: frame_impl.cpp:56


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:51