buffer_processor.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 // TO DO: This exists in linux_utils.h which is not included on Dragoboard.
34 // Should not have duplicated code if possible.
35 
36 #include <algorithm>
37 #include <arm_neon.h>
38 #include <cmath>
39 #include <fcntl.h>
40 #include <fstream>
41 #include <unistd.h>
42 #ifdef USE_GLOG
43 #include <glog/logging.h>
44 #else
45 #include <aditof/log.h>
46 #endif
47 #include <linux/videodev2.h>
48 #include <memory>
49 #include <sstream>
50 #include <sys/ioctl.h>
51 #include <sys/mman.h>
52 #include <sys/stat.h>
53 #include <unordered_map>
54 
55 #include "buffer_processor.h"
56 
58 
59 #define CLEAR(x) memset(&(x), 0, sizeof(x))
60 
61 static int xioctl(int fh, unsigned int request, void *arg) {
62  int r;
63 
64  do {
65  r = ioctl(fh, request, arg);
66  } while (-1 == r && EINTR == errno && errno != 0);
67 
68  return r;
69 }
70 
72  : m_vidPropSet(false), m_processorPropSet(false), m_outputFrameWidth(0),
73  m_outputFrameHeight(0), m_processedBuffer(nullptr), m_tofiConfig(nullptr),
74  m_tofiComputeContext(nullptr), m_inputVideoDev(nullptr) {
75  m_outputVideoDev = new VideoDev();
76 }
77 
79  if (NULL != m_tofiComputeContext) {
80  LOG(INFO) << "freeComputeLibrary";
83  }
84 
85  if (m_tofiConfig != NULL) {
88  }
89 
90  if (m_outputVideoDev->fd != -1) {
91  if (::close(m_outputVideoDev->fd) == -1) {
92  LOG(ERROR) << "Failed to close " << m_videoDeviceName
93  << " error: " << strerror(errno);
94  }
95  }
96 }
97 
99  using namespace aditof;
100  Status status = Status::OK;
101 
102  //TO DO: remove when we re-enable uvc
103  return aditof::Status::OK;
104 
106  if (m_outputVideoDev->fd == -1) {
107  LOG(ERROR) << "Cannot open " << OUTPUT_DEVICE << "errno: " << errno
108  << "error: " << strerror(errno);
109  return Status::GENERIC_ERROR;
110  }
111 
112  if (xioctl(m_outputVideoDev->fd, VIDIOC_QUERYCAP, &m_videoCap) == -1) {
113  LOG(ERROR) << m_videoDeviceName << " VIDIOC_QUERYCAP error";
114  return Status::GENERIC_ERROR;
115  }
116 
117  memset(&m_videoFormat, 0, sizeof(m_videoFormat));
118  if (xioctl(m_outputVideoDev->fd, VIDIOC_G_FMT, &m_videoFormat) == -1) {
119  // LOG(ERROR) << m_videoDeviceName << " VIDIOC_G_FMT error";
120  // return Status::GENERIC_ERROR;
121  }
122 
123  return status;
124 }
125 
127  m_inputVideoDev = inputVideoDev;
128 
129  return aditof::Status::OK;
130 }
131 
133  int frameHeight) {
134  using namespace aditof;
135  Status status = Status::OK;
136 
137  m_outputFrameWidth = frameWidth;
138  m_outputFrameHeight = frameHeight;
139 
140  //m_videoFormat.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
141  //m_videoFormat.fmt.pix.width = frameWidth / 2;
142  //m_videoFormat.fmt.pix.height = frameHeight;
143  //m_videoFormat.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
144  //m_videoFormat.fmt.pix.sizeimage = frameWidth * frameHeight;
145  //m_videoFormat.fmt.pix.field = V4L2_FIELD_NONE;
146  //m_videoFormat.fmt.pix.bytesperline = frameWidth;
147  //m_videoFormat.fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
148 
149  //if (xioctl(m_outputVideoDev->fd, VIDIOC_S_FMT, &m_videoFormat) == -1) {
150  // LOG(ERROR) << "Failed to set format!";
151  // return Status::GENERIC_ERROR;
152  //}
153 
154  if (m_processedBuffer != nullptr) {
155  delete[] m_processedBuffer;
156  }
158 
159  return status;
160 }
161 
163  uint8_t *iniFile, uint16_t iniFileLength, uint8_t *calData,
164  uint16_t calDataLength, uint16_t mode, bool ispEnabled) {
165 
166  if (ispEnabled) {
167  uint32_t status = ADI_TOFI_SUCCESS;
168  ConfigFileData calDataStruct = {calData, calDataLength};
169  if (iniFile != nullptr) {
170  ConfigFileData depth_ini = {iniFile, iniFileLength};
171  if (ispEnabled) {
172  memcpy(m_xyzDealiasData, calData, calDataLength);
173  m_tofiConfig =
174  InitTofiConfig_isp((ConfigFileData *)&depth_ini, mode,
175  &status, m_xyzDealiasData);
176  } else {
177  if (calDataStruct.p_data != NULL) {
178  m_tofiConfig = InitTofiConfig(&calDataStruct, NULL,
179  &depth_ini, mode, &status);
180  } else {
181  LOG(ERROR) << "Failed to get calibration data";
182  }
183  }
184 
185  } else {
186  m_tofiConfig =
187  InitTofiConfig(&calDataStruct, NULL, NULL, mode, &status);
188  }
189 
190  if ((m_tofiConfig == NULL) ||
192  (status != ADI_TOFI_SUCCESS)) {
193  LOG(ERROR) << "InitTofiConfig failed";
195 
196  } else {
199  if (m_tofiComputeContext == NULL || status != ADI_TOFI_SUCCESS) {
200  LOG(ERROR) << "InitTofiCompute failed";
202  }
203  }
204  } else {
205  LOG(ERROR) << "Could not initialize compute library because config "
206  "data hasn't been loaded";
208  }
209 
210  return aditof::Status::OK;
211 }
212 
215  const uint16_t &chipID = CHIP_ID_SINGLE,
216  const uint8_t &mode_num = DEFAULT_MODE) {
217  using namespace aditof;
218  struct v4l2_buffer buf[4];
219  struct VideoDev *dev;
220  Status status;
221  unsigned int buf_data_len;
222  uint8_t *pdata;
223  dev = m_inputVideoDev;
224  uint8_t *pdata_user_space = nullptr;
225 
226  status = waitForBufferPrivate(dev);
227  if (status != Status::OK) {
228  return status;
229  }
230 
231  status = dequeueInternalBufferPrivate(buf[0], dev);
232  if (status != Status::OK) {
233  return status;
234  }
235 
236  status = getInternalBufferPrivate(&pdata, buf_data_len, buf[0], dev);
237  if (status != Status::OK) {
238  return status;
239  }
240 
241  pdata_user_space = (uint8_t *)malloc(sizeof(uint8_t) * buf_data_len);
242  memcpy(pdata_user_space, pdata, buf_data_len);
243 
244  uint16_t *tempDepthFrame = m_tofiComputeContext->p_depth_frame;
245  uint16_t *tempAbFrame = m_tofiComputeContext->p_ab_frame;
246  float *tempConfFrame = m_tofiComputeContext->p_conf_frame;
247 
248  if (buffer != nullptr) {
249 
254  (float *)(buffer + m_outputFrameWidth * m_outputFrameHeight / 2);
255 #ifdef DUAL
256  if (mode_num == 0 ||
257  mode_num ==
258  1) { // For dual pulsatrix mode 1 and 0 confidance frame is not enabled
259  memcpy(m_tofiComputeContext->p_depth_frame, pdata_user_space,
262  pdata_user_space +
267  } else {
268  uint32_t ret = TofiCompute((uint16_t *)pdata_user_space,
270 
271  if (ret != ADI_TOFI_SUCCESS) {
272  LOG(ERROR) << "TofiCompute failed";
273  return Status::GENERIC_ERROR;
274  }
275  }
276 
277 #else
278  uint32_t ret = TofiCompute((uint16_t *)pdata_user_space,
280 
281  if (ret != ADI_TOFI_SUCCESS) {
282  LOG(ERROR) << "TofiCompute failed";
283  return Status::GENERIC_ERROR;
284  }
285 #endif
286 
287  } else {
288 
293  (float *)(m_processedBuffer +
295 
296 #ifdef DUAL
297  if (mode_num == 0 ||
298  mode_num ==
299  1) { // For dual pulsatrix mode 1 and 0 confidance frame is not enabled
300  memcpy(m_tofiComputeContext->p_depth_frame, pdata_user_space,
303  pdata_user_space +
308  } else {
309  uint32_t ret = TofiCompute((uint16_t *)pdata_user_space,
311 
312  if (ret != ADI_TOFI_SUCCESS) {
313  LOG(ERROR) << "TofiCompute failed";
314  return Status::GENERIC_ERROR;
315  }
316  }
317 
318 #else
319  uint32_t ret = TofiCompute((uint16_t *)pdata_user_space,
321 
322  if (ret != ADI_TOFI_SUCCESS) {
323  LOG(ERROR) << "TofiCompute failed";
324  return Status::GENERIC_ERROR;
325  }
326 #endif
327 
330  }
331 
332  m_tofiComputeContext->p_depth_frame = tempDepthFrame;
333  m_tofiComputeContext->p_ab_frame = tempAbFrame;
334  m_tofiComputeContext->p_conf_frame = tempConfFrame;
335 
336  if (pdata_user_space)
337  free(pdata_user_space);
338 
339  status = enqueueInternalBufferPrivate(buf[0], dev);
340  if (status != Status::OK) {
341  return status;
342  }
343 
344  return status;
345 }
346 
348  fd_set fds;
349  struct timeval tv;
350  int r;
351 
352  if (dev == nullptr)
353  dev = m_inputVideoDev;
354 
355  FD_ZERO(&fds);
356  FD_SET(dev->fd, &fds);
357 
358  tv.tv_sec = 20;
359  tv.tv_usec = 0;
360 
361  r = select(dev->fd + 1, &fds, NULL, NULL, &tv);
362 
363  if (r == -1) {
364  LOG(WARNING) << "select error "
365  << "errno: " << errno << " error: " << strerror(errno);
367  } else if (r == 0) {
368  LOG(WARNING) << "select timeout";
370  }
371  return aditof ::Status::OK;
372 }
373 
376  struct VideoDev *dev) {
377  using namespace aditof;
378  Status status = Status::OK;
379 
380  if (dev == nullptr)
381  dev = m_inputVideoDev;
382 
383  CLEAR(buf);
384  buf.type = dev->videoBuffersType;
385  buf.memory = V4L2_MEMORY_MMAP;
386  buf.length = 1;
387  buf.m.planes = dev->planes;
388 
389  if (xioctl(dev->fd, VIDIOC_DQBUF, &buf) == -1) {
390  LOG(WARNING) << "VIDIOC_DQBUF error "
391  << "errno: " << errno << " error: " << strerror(errno);
392  switch (errno) {
393  case EAGAIN:
394  case EIO:
395  break;
396  default:
397  return Status::GENERIC_ERROR;
398  }
399  }
400 
401  if (buf.index >= dev->nVideoBuffers) {
402  LOG(WARNING) << "Not enough buffers avaialable";
403  return Status::GENERIC_ERROR;
404  }
405 
406  return status;
407 }
408 
410  uint8_t **buffer, uint32_t &buf_data_len, const struct v4l2_buffer &buf,
411  struct VideoDev *dev) {
412  if (dev == nullptr)
413  dev = m_inputVideoDev;
414 
415  *buffer = static_cast<uint8_t *>(dev->videoBuffers[buf.index].start);
416  buf_data_len = buf.bytesused;
417 
418  return aditof::Status::OK;
419 }
420 
423  struct VideoDev *dev) {
424  if (dev == nullptr)
425  dev = m_inputVideoDev;
426 
427  if (xioctl(dev->fd, VIDIOC_QBUF, &buf) == -1) {
428  LOG(WARNING) << "VIDIOC_QBUF error "
429  << "errno: " << errno << " error: " << strerror(errno);
431  }
432 
433  return aditof::Status::OK;
434 }
435 
437  fileDescriptor = m_outputVideoDev->fd;
438  return aditof::Status::OK;
439 }
440 
442 
443  return waitForBufferPrivate();
444 }
445 
447 
449 }
450 
452 BufferProcessor::getInternalBuffer(uint8_t **buffer, uint32_t &buf_data_len,
453  const struct v4l2_buffer &buf) {
454 
455  return getInternalBufferPrivate(buffer, buf_data_len, buf);
456 }
457 
459 
461 }
462 
464 
467  return aditof::Status::OK;
468 }
BufferProcessor::m_inputVideoDev
struct VideoDev * m_inputVideoDev
Definition: buffer_processor.h:121
INFO
const int INFO
Definition: log_severity.h:59
BufferProcessor::getDeviceFileDescriptor
virtual aditof::Status getDeviceFileDescriptor(int &fileDescriptor) override
Definition: buffer_processor.cpp:436
CHIP_ID_SINGLE
#define CHIP_ID_SINGLE
Definition: buffer_processor.h:40
TofiComputeContext::p_ab_frame
uint16_t * p_ab_frame
Pointer to the AB Frame.
Definition: tofi_compute.h:47
TofiConfig::p_tofi_cal_config
const void * p_tofi_cal_config
Pointer to the calibration config block.
Definition: tofi_config.h:52
TofiComputeContext::p_depth_frame
uint16_t * p_depth_frame
Pointer to the Depth Frame.
Definition: tofi_compute.h:46
BufferProcessor::getTofiCongfig
TofiConfig * getTofiCongfig()
Definition: buffer_processor.cpp:463
BufferProcessor::waitForBufferPrivate
aditof::Status waitForBufferPrivate(struct VideoDev *dev=nullptr)
Definition: buffer_processor.cpp:347
NULL
NULL
Definition: test_security_zap.cpp:405
ERROR
const int ERROR
Definition: log_severity.h:60
EINTR
#define EINTR
Definition: errno.hpp:7
VideoDev::fd
int fd
Definition: buffer_processor.h:49
aditof::Status::GENERIC_ERROR
@ GENERIC_ERROR
An error occured but there are no details available.
BufferProcessor::setVideoProperties
aditof::Status setVideoProperties(int frameWidth, int frameHeight)
Definition: buffer_processor.cpp:132
InitTofiConfig
TofiConfig * InitTofiConfig(ConfigFileData *p_cal_file_data, ConfigFileData *p_config_file_data, ConfigFileData *p_ini_file_data, uint16_t mode, uint32_t *p_status)
Function to Initialize the configuration for TOFI cal config (p_tofi_cal_config) using calibration/co...
Definition: tofiConfig.cpp:9
mode
GLenum mode
Definition: glcorearb.h:2764
InitTofiCompute
TofiComputeContext * InitTofiCompute(const void *p_tofi_cal_config, uint32_t *p_status)
Definition: tofiCompute.cpp:22
EAGAIN
#define EAGAIN
Definition: errno.hpp:14
BufferProcessor::setInputDevice
aditof::Status setInputDevice(VideoDev *inputVideoDev)
Definition: buffer_processor.cpp:126
log.h
OUTPUT_DEVICE
#define OUTPUT_DEVICE
Definition: buffer_processor.h:39
VideoDev
Definition: buffer_processor.h:48
errno
int errno
DEFAULT_MODE
#define DEFAULT_MODE
Definition: buffer_processor.h:41
TofiConfig
Definition: tofi_config.h:40
WARNING
const int WARNING
Definition: log_severity.h:59
BufferProcessor::m_videoDeviceName
const char * m_videoDeviceName
Definition: buffer_processor.h:119
ConfigFileData::p_data
unsigned char * p_data
Pointer to the data.
Definition: tofi_config.h:30
BufferProcessor::dequeueInternalBuffer
virtual aditof::Status dequeueInternalBuffer(struct v4l2_buffer &buf) override
Definition: buffer_processor.cpp:446
BufferProcessor::waitForBuffer
virtual aditof::Status waitForBuffer() override
Definition: buffer_processor.cpp:441
enabled
GLenum GLenum GLsizei const GLuint GLboolean enabled
Definition: glcorearb.h:4174
BufferProcessor::m_outputFrameWidth
uint16_t m_outputFrameWidth
Definition: buffer_processor.h:108
ConfigFileData
Definition: tofi_config.h:29
xioctl
static int xioctl(int fh, unsigned int request, void *arg)
Definition: buffer_processor.cpp:61
FreeTofiCompute
void FreeTofiCompute(TofiComputeContext *p_tofi_compute_context)
Definition: tofiCompute.cpp:147
BufferProcessor::~BufferProcessor
~BufferProcessor()
Definition: buffer_processor.cpp:78
mode_num
uint8_t mode_num
Definition: adsd3500_sensor.cpp:69
BufferProcessor::m_processedBuffer
uint16_t * m_processedBuffer
Definition: buffer_processor.h:111
BufferProcessor::open
aditof::Status open()
Definition: buffer_processor.cpp:98
aditof
Namespace aditof.
Definition: adsd_errs.h:40
BufferProcessor::BufferProcessor
BufferProcessor()
Definition: buffer_processor.cpp:71
buffer
GLuint buffer
Definition: glcorearb.h:2939
BufferProcessor::m_videoCap
struct v4l2_capability m_videoCap
Definition: buffer_processor.h:117
google::protobuf::util::error::OK
@ OK
Definition: status.h:47
buffer::start
void * start
Definition: buffer_processor.h:44
VideoDev::planes
struct v4l2_plane planes[8]
Definition: buffer_processor.h:53
BufferProcessor::enqueueInternalBufferPrivate
aditof::Status enqueueInternalBufferPrivate(struct v4l2_buffer &buf, struct VideoDev *dev=nullptr)
Definition: buffer_processor.cpp:422
buffer_processor.h
BufferProcessor::setProcessorProperties
aditof::Status setProcessorProperties(uint8_t *iniFile, uint16_t iniFileLength, uint8_t *calData, uint16_t calDataLength, uint16_t mode, bool ispEnabled)
Definition: buffer_processor.cpp:162
buffer
Definition: buffer_processor.h:43
BufferProcessor::m_tofiConfig
TofiConfig * m_tofiConfig
Definition: buffer_processor.h:113
buf
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:4175
BufferProcessor::dequeueInternalBufferPrivate
aditof::Status dequeueInternalBufferPrivate(struct v4l2_buffer &buf, struct VideoDev *dev=nullptr)
Definition: buffer_processor.cpp:375
aditof::Status
Status
Status of any operation that the TOF sdk performs.
Definition: status_definitions.h:48
BufferProcessor::processBuffer
aditof::Status processBuffer(uint16_t *buffer, const uint16_t &chipID, const uint8_t &mode_num)
Definition: buffer_processor.cpp:214
BufferProcessor::m_videoFormat
struct v4l2_format m_videoFormat
Definition: buffer_processor.h:118
VideoDev::videoBuffersType
enum v4l2_buf_type videoBuffersType
Definition: buffer_processor.h:54
BufferProcessor::getDepthComputeVersion
aditof::Status getDepthComputeVersion(uint8_t &enabled)
Definition: buffer_processor.cpp:465
TofiCompute
int TofiCompute(const uint16_t *const input_frame, TofiComputeContext *const p_tofi_compute_context, TemperatureInfo *p_temperature)
Definition: tofiCompute.cpp:106
InitTofiConfig_isp
TofiConfig * InitTofiConfig_isp(ConfigFileData *p_ini_file_data, uint16_t mode, uint32_t *p_status, TofiXYZDealiasData *p_xyz_dealias_data)
Function to Initialize the configuration for TOFI cal config incase of isp.
Definition: tofiConfig.cpp:52
LOG
#define LOG(x)
Definition: sdk/include/aditof/log.h:72
aditof::Status::OK
@ OK
Success.
BufferProcessor::getInternalBufferPrivate
aditof::Status getInternalBufferPrivate(uint8_t **buffer, uint32_t &buf_data_len, const struct v4l2_buffer &buf, struct VideoDev *dev=nullptr)
Definition: buffer_processor.cpp:409
r
GLboolean r
Definition: glcorearb.h:3228
strerror
char * strerror(int errno)
TofiComputeContext::p_conf_frame
float * p_conf_frame
Pointer to the Confidence Frame.
Definition: tofi_compute.h:48
depthComputeOpenSourceEnabled
uint8_t depthComputeOpenSourceEnabled
Definition: buffer_processor.cpp:57
BufferProcessor::m_xyzDealiasData
TofiXYZDealiasData m_xyzDealiasData[11]
Definition: buffer_processor.h:115
CLEAR
#define CLEAR(x)
Definition: buffer_processor.cpp:59
BufferProcessor::m_outputVideoDev
struct VideoDev * m_outputVideoDev
Definition: buffer_processor.h:122
VideoDev::nVideoBuffers
unsigned int nVideoBuffers
Definition: buffer_processor.h:52
BufferProcessor::getInternalBuffer
virtual aditof::Status getInternalBuffer(uint8_t **buffer, uint32_t &buf_data_len, const struct v4l2_buffer &buf) override
Definition: buffer_processor.cpp:452
ADI_TOFI_SUCCESS
@ ADI_TOFI_SUCCESS
0
Definition: tofi_error.h:11
false
#define false
Definition: cJSON.c:70
BufferProcessor::enqueueInternalBuffer
virtual aditof::Status enqueueInternalBuffer(struct v4l2_buffer &buf) override
Definition: buffer_processor.cpp:458
BufferProcessor::m_outputFrameHeight
uint16_t m_outputFrameHeight
Definition: buffer_processor.h:109
VideoDev::videoBuffers
struct buffer * videoBuffers
Definition: buffer_processor.h:51
BufferProcessor::m_tofiComputeContext
TofiComputeContext * m_tofiComputeContext
Definition: buffer_processor.h:114
FreeTofiConfig
void FreeTofiConfig(TofiConfig *p_tofi_cal_config)
Definition: tofiConfig.cpp:102


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