Skip to content

Commit

Permalink
Use custom libfreenect to perform registration afterwards
Browse files Browse the repository at this point in the history
  • Loading branch information
SirLynix committed Dec 29, 2021
1 parent 157717a commit 5e67696
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
45 changes: 32 additions & 13 deletions src/obs-kinect-freenect/FreenectDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
******************************************************************************/

#include "FreenectDevice.hpp"
#include <libfreenect/libfreenect_registration.h>
#include <util/threading.h>
#include <cstring>
#include <mutex>
Expand All @@ -24,7 +25,7 @@
KinectFreenectDevice::KinectFreenectDevice(freenect_device* device, const char* serial) :
m_device(device)
{
SetSupportedSources(Source_Color | Source_Depth);
SetSupportedSources(Source_Color | Source_Depth | Source_ColorMappedDepth);
SetUniqueName("Kinect " + std::string(serial));
}

Expand Down Expand Up @@ -56,7 +57,7 @@ void KinectFreenectDevice::ThreadFunc(std::condition_variable& cv, std::mutex& m

currentColorMode = colorMode;

freenect_frame_mode depthMode = freenect_find_depth_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_MM);
freenect_frame_mode depthMode = freenect_find_depth_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_11BIT_PACKED);

if (freenect_set_depth_mode(m_device, depthMode) < 0)
throw std::runtime_error("failed to set video mode");
Expand Down Expand Up @@ -166,25 +167,43 @@ void KinectFreenectDevice::ThreadFunc(std::condition_variable& cv, std::mutex& m
frameData.format = GS_RGBA;
}

// Depth
// Depth (and color mapped depth)
{
std::scoped_lock lock(ud.depthMutex);

const std::uint16_t* frameMem = ud.depthFrontBuffer.data();
std::uint16_t* frameMem = ud.depthFrontBuffer.data();
if (!frameMem)
continue;

DepthFrameData& frameData = framePtr->depthFrame.emplace();
frameData.width = currentDepthMode.width;
frameData.height = currentDepthMode.height;
// Depth
{
DepthFrameData& frameData = framePtr->depthFrame.emplace();
frameData.width = currentDepthMode.width;
frameData.height = currentDepthMode.height;

// Convert to R16
std::size_t memSize = frameData.width * frameData.height * 2;
frameData.memory.resize(memSize);
std::memcpy(frameData.memory.data(), frameMem, memSize);
// Copy it to buffer memory
std::size_t memSize = frameData.width * frameData.height * 2;
frameData.memory.resize(memSize);
freenect_convert_packed_to_16bit(reinterpret_cast<std::uint8_t*>(frameMem), reinterpret_cast<std::uint16_t*>(frameData.memory.data()), 11, frameData.width*frameData.height);

frameData.ptr.reset(reinterpret_cast<std::uint16_t*>(frameData.memory.data()));
frameData.pitch = static_cast<std::uint32_t>(frameData.width * 2);
}

frameData.ptr.reset(reinterpret_cast<std::uint16_t*>(frameData.memory.data()));
frameData.pitch = static_cast<std::uint32_t>(frameData.width * 2);
// Color-mapped depth
{
DepthFrameData& frameData = framePtr->colorMappedDepthFrame.emplace();
frameData.width = currentDepthMode.width;
frameData.height = currentDepthMode.height;

// Convert to R16
std::size_t memSize = frameData.width * frameData.height * 2;
frameData.memory.resize(memSize);
freenect_map_depth_to_rgb(m_device, reinterpret_cast<std::uint8_t*>(frameMem), reinterpret_cast<std::uint16_t*>(frameData.memory.data()));

frameData.ptr.reset(reinterpret_cast<std::uint16_t*>(frameData.memory.data()));
frameData.pitch = static_cast<std::uint32_t>(frameData.width * 2);
}
}

UpdateFrame(std::move(framePtr));
Expand Down
11 changes: 3 additions & 8 deletions xmake-repo/packages/l/libfreenect/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@ package("libfreenect")
set_description("Drivers and libraries for the Xbox Kinect device on Windows, Linux, and OS X")
set_license("GPL-2.0")

set_urls("https://github.com/OpenKinect/libfreenect/archive/refs/tags/$(version).tar.gz",
"https://github.com/OpenKinect/libfreenect.git")
add_versions("v0.6.2", "e135f5e60ae290bf1aa403556211f0a62856a9e34f12f12400ec593620a36bfa")

add_urls("https://github.com/OpenKinect/libfreenect.git")

add_versions("v0.2.1", "fd64c5d9b214df6f6a55b4419357e51083f15d93")
add_versions("v0.2.0", "v0.2.0")
set_urls("https://github.com/SirLynix/libfreenect.git")
add_versions("v0.1", "5e2cd8e18877fbe7ef4c863161e90873c06a9180")

add_deps("cmake >=3.12.4")
if is_plat("windows") then
Expand Down Expand Up @@ -40,4 +34,5 @@ package("libfreenect")

on_test(function (package)
assert(package:has_cfuncs("freenect_init", {includes = "libfreenect/libfreenect.h"}))
assert(package:has_cfuncs("freenect_map_depth_to_rgb", {includes = "libfreenect/libfreenect_registration.h"}))
end)

0 comments on commit 5e67696

Please sign in to comment.