-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NHPCD - Network Hardware Point Cloud Decoder (proof-of-concept)
- working proof of concept for NHPCD - some hardcoded data for Realsense D435 Proof of concept: - to be turned into final clean code Related to: bmegli/unity-network-hardware-video-decoder#5
- Loading branch information
Showing
6 changed files
with
115 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule hardware-depth-unprojector
added at
0729c2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/* | ||
* NHVD Network Hardware Video Decoder C++ library implementation | ||
* | ||
* Copyright 2019 (C) Bartosz Meglicki <[email protected]> | ||
* Copyright 2020 (C) Bartosz Meglicki <[email protected]> | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
|
@@ -15,18 +15,27 @@ | |
#include "mlsp.h" | ||
// Hardware Video Decoder library | ||
#include "hvd.h" | ||
// Hardware Depth Unprojector library | ||
#include "hdu.h" | ||
|
||
#include <thread> | ||
#include <mutex> | ||
#include <fstream> | ||
#include <iostream> | ||
#include <string.h> //memset | ||
|
||
using namespace std; | ||
|
||
static void nhvd_network_decoder_thread(nhvd *n); | ||
static int nhvd_decode_frame(nhvd *n, hvd_packet* packet); | ||
static nhvd *nhvd_close_and_return_null(nhvd *n); | ||
|
||
const float PPX=421.353; | ||
const float PPY=240.93; | ||
const float FX=426.768; | ||
const float FY=426.768; | ||
const float DEPTH_UNIT=0.0001; | ||
|
||
struct nhvd | ||
{ | ||
mlsp *network_streamer; | ||
|
@@ -35,10 +44,18 @@ struct nhvd | |
AVFrame *frame; | ||
std::mutex frame_mutex; | ||
|
||
hdu *hardware_unprojector; | ||
hdu_point_cloud point_cloud; | ||
|
||
thread network_thread; | ||
bool keep_working; | ||
|
||
nhvd(): network_streamer(NULL), hardware_decoder(NULL), frame(NULL), keep_working(true) {} | ||
nhvd(): network_streamer(NULL), | ||
hardware_decoder(NULL), | ||
frame(NULL), | ||
hardware_unprojector(NULL), | ||
keep_working(true) | ||
{} | ||
}; | ||
|
||
struct nhvd *nhvd_init(const nhvd_net_config *net_config,const nhvd_hw_config *hw_config) | ||
|
@@ -71,6 +88,14 @@ struct nhvd *nhvd_init(const nhvd_net_config *net_config,const nhvd_hw_config *h | |
} | ||
n->frame->data[0]=NULL; | ||
|
||
if( (n->hardware_unprojector = hdu_init(PPX, PPY, FX, FY, DEPTH_UNIT)) == NULL ) | ||
{ | ||
cerr << "nhvd: failed to initialize hardware unprojector" << endl; | ||
return nhvd_close_and_return_null(n); | ||
} | ||
|
||
n->point_cloud.data = NULL, n->point_cloud.size = 0, n->point_cloud.used = 0; | ||
|
||
n->network_thread = thread(nhvd_network_decoder_thread, n); | ||
|
||
return n; | ||
|
@@ -123,9 +148,29 @@ static int nhvd_decode_frame(nhvd *n, hvd_packet* packet) | |
|
||
while( (frame = hvd_receive_frame(n->hardware_decoder, &error) ) != NULL ) | ||
{ | ||
std::lock_guard<std::mutex> frame_guard(n->frame_mutex); | ||
// std::lock_guard<std::mutex> frame_guard(n->frame_mutex); | ||
// av_frame_unref(n->frame); | ||
// av_frame_ref(n->frame, frame); | ||
av_frame_unref(n->frame); | ||
av_frame_ref(n->frame, frame); | ||
|
||
//guard the point cloud, not the frame | ||
std::lock_guard<std::mutex> frame_guard(n->frame_mutex); | ||
//unproject | ||
int size = n->frame->width * n->frame->height; | ||
if(size != n->point_cloud.size) | ||
{ | ||
delete [] n->point_cloud.data; | ||
n->point_cloud.data = new float3[size]; | ||
n->point_cloud.size = size; | ||
n->point_cloud.used = 0; | ||
} | ||
|
||
hdu_depth depth = {(uint16_t*)n->frame->data[0], n->frame->width, n->frame->height, n->frame->linesize[0]}; | ||
//this could be moved to separate thread | ||
hdu_unproject(n->hardware_unprojector, &depth, &n->point_cloud); | ||
//temp | ||
memset(n->point_cloud.data + n->point_cloud.used, 0, (n->point_cloud.size-n->point_cloud.used)*sizeof(n->point_cloud.data[0])); | ||
} | ||
|
||
if(error != HVD_OK) | ||
|
@@ -170,6 +215,36 @@ int nhvd_get_frame_end(struct nhvd *n) | |
return NHVD_OK; | ||
} | ||
|
||
//NULL if there is no fresh data, non NULL otherwise | ||
int nhvd_get_point_cloud_begin(nhvd *n, nhvd_point_cloud *pc) | ||
{ | ||
if(n == NULL) | ||
return NHVD_ERROR; | ||
|
||
n->frame_mutex.lock(); | ||
|
||
//for user convinience, return ERROR if there is no data | ||
if(n->point_cloud.data == NULL) | ||
return NHVD_ERROR; | ||
|
||
pc->data = n->point_cloud.data; | ||
pc->size = n->point_cloud.size; | ||
pc->used = n->point_cloud.used; | ||
|
||
return NHVD_OK; | ||
|
||
} | ||
//returns HVE_OK on success, HVE_ERROR on fatal error | ||
int nhvd_get_point_cloud_end(nhvd *n) | ||
{ | ||
if(n == NULL) | ||
return NHVD_ERROR; | ||
|
||
n->frame_mutex.unlock(); | ||
|
||
return NHVD_OK; | ||
} | ||
|
||
static nhvd *nhvd_close_and_return_null(nhvd *n) | ||
{ | ||
nhvd_close(n); | ||
|
@@ -187,6 +262,8 @@ void nhvd_close(nhvd *n) | |
|
||
mlsp_close(n->network_streamer); | ||
hvd_close(n->hardware_decoder); | ||
hdu_close(n->hardware_unprojector); | ||
delete [] n->point_cloud.data; | ||
|
||
av_frame_free(&n->frame); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters