Files
kinect/core/protocol/streams.cpp
T
kinect-bot cb311381a2
ci / Linux C++ core (cmake + ctest) (push) Canceled after 0s
ci / Python tracker (pytest) (push) Canceled after 0s
feat(protocol): byte-exact Kinect v1 virtual USB device emitter (wave 5; todo 8 done)
2026-11-25 04:50:42 -05:00

196 lines
6.8 KiB
C++

#include <algorithm>
#include <cmath>
#include <cstddef>
#include <vector>
#include "protocol/protocol.h"
#include "protocol/wire.h"
#include "synth/synth.h"
namespace nui::protocol {
namespace {
constexpr uint32_t kDepthWidth = 640;
constexpr uint32_t kDepthHeight = 480;
constexpr uint32_t kColorWidth = 640;
constexpr uint32_t kColorHeight = 480;
constexpr uint32_t kInfraredWidth = 640;
constexpr uint32_t kInfraredHeight = 488;
constexpr std::size_t kDepthPayload = 1748; // DEPTH_PKTDSIZE, freenect_internal.h:131
constexpr std::size_t kVideoPayload = 1908; // VIDEO_PKTDSIZE, freenect_internal.h:133
constexpr uint16_t kRawNoValue = 2047; // FREENECT_DEPTH_RAW_NO_VALUE
// Inverse of libfreenect's freenect_raw_to_mm (registration.c:315-325) under
// the synthetic 1414 calibration (wire.h). The host rebuilds raw->mm from the
// same EEPROM constants this device reports, so this inverse must use them too
// or the reconstructed depth drifts. parameter_coefficient=4, shift_scale=10,
// pixel_size_factor=1, S2D_CONST_OFFSET=0.375 are constants in that source.
uint16_t millimeters_to_raw(uint16_t millimeters) {
if (millimeters == 0) {
return kRawNoValue;
}
const double scaled = millimeters / 10.0;
const double metric =
(scaled - wire::kReferenceDistance) * wire::kEmitterDistance / scaled;
const double fixed_ref_x = metric / wire::kPixelSize;
const double raw = 4.0 * fixed_ref_x + 1.5 + 4.0 * wire::kConstantShift;
const long rounded = std::lround(raw);
if (rounded < 0 || rounded >= kRawNoValue) {
return kRawNoValue;
}
return static_cast<uint16_t>(rounded);
}
// MSB-first bit packing: the exact inverse of convert_packed11_to_16bit
// (cameras.c:345-375) generalised to any width <= 16. Each value's high bits
// enter the byte stream first, so 8 eleven-bit samples occupy 11 bytes.
void pack_bits(const std::vector<uint16_t>& values, unsigned width, Bytes& out) {
const uint32_t mask = (1u << width) - 1u;
uint32_t buffer = 0;
unsigned bits = 0;
for (const uint16_t value : values) {
buffer = (buffer << width) | (value & mask);
bits += width;
while (bits >= 8) {
bits -= 8;
out.push_back(static_cast<uint8_t>((buffer >> bits) & 0xff));
}
}
if (bits > 0) {
out.push_back(static_cast<uint8_t>((buffer << (8 - bits)) & 0xff));
}
}
uint16_t sample_depth(const synth::DepthFrame& source, uint32_t x, uint32_t y) {
if (source.size.width == 0 || source.size.height == 0 ||
source.pixels.size() != static_cast<std::size_t>(source.size.width) *
source.size.height) {
return 0;
}
const uint32_t source_x = x * source.size.width / kDepthWidth;
const uint32_t source_y = y * source.size.height / kDepthHeight;
return source.pixels[static_cast<std::size_t>(source_y) * source.size.width +
source_x];
}
uint8_t bayer_grbg(const synth::BgraFrame& color, uint32_t x, uint32_t y) {
const std::size_t base =
(static_cast<std::size_t>(y) * kColorWidth + x) * 4;
const uint8_t blue = color.pixels[base];
const uint8_t green = color.pixels[base + 1];
const uint8_t red = color.pixels[base + 2];
const bool even_row = (y % 2) == 0;
const bool even_col = (x % 2) == 0;
if (even_row) {
return even_col ? green : red;
}
return even_col ? blue : green;
}
Bytes build_depth(const SynthFrame& frame) {
std::vector<uint16_t> raw(static_cast<std::size_t>(kDepthWidth) * kDepthHeight);
for (uint32_t y = 0; y < kDepthHeight; ++y) {
for (uint32_t x = 0; x < kDepthWidth; ++x) {
const uint16_t packed = sample_depth(frame.depth, x, y);
raw[static_cast<std::size_t>(y) * kDepthWidth + x] =
millimeters_to_raw(synth::NuiDepthPixelToDepth(packed));
}
}
Bytes out;
out.reserve(static_cast<std::size_t>(kDepthWidth) * kDepthHeight * 11 / 8);
pack_bits(raw, 11, out);
return out;
}
Bytes build_video(const SynthFrame& frame, bool infrared) {
if (infrared) {
const auto image = synth::synthesize_infrared(
frame.color, {kInfraredWidth, kInfraredHeight});
std::vector<uint16_t> values(image.pixels.size());
for (std::size_t i = 0; i < values.size(); ++i) {
values[i] = static_cast<uint16_t>(image.pixels[i] >> 6);
}
Bytes out;
out.reserve(static_cast<std::size_t>(kInfraredWidth) * kInfraredHeight * 10 / 8);
pack_bits(values, 10, out);
return out;
}
const auto color =
synth::passthrough_color(frame.color, {kColorWidth, kColorHeight});
Bytes out(static_cast<std::size_t>(kColorWidth) * kColorHeight);
for (uint32_t y = 0; y < kColorHeight; ++y) {
for (uint32_t x = 0; x < kColorWidth; ++x) {
out[static_cast<std::size_t>(y) * kColorWidth + x] = bayer_grbg(color, x, y);
}
}
return out;
}
} // namespace
TransferResult KinectV1Device::next_packet(UsbFunction function, uint8_t endpoint) {
if (function != UsbFunction::camera) {
return {TransferStatus::stall, {}};
}
Stream* stream = nullptr;
std::size_t payload_size = 0;
uint8_t base_flag = 0;
bool running = false;
bool infrared = false;
if (endpoint == 0x82) {
stream = &depth_;
payload_size = kDepthPayload;
base_flag = 0x70; // depth stream base flag, cameras.c:849
running = registers_.at(0x06) == 0x02;
} else if (endpoint == 0x81) {
stream = &video_;
payload_size = kVideoPayload;
base_flag = 0x80; // video stream base flag, cameras.c:913
infrared = registers_.at(0x05) == 0x03;
running = registers_.at(0x05) == 0x01 || registers_.at(0x05) == 0x03;
} else {
return {TransferStatus::invalid_frame, {}};
}
if (!running) {
*stream = {};
return {TransferStatus::idle, {}};
}
if (stream->bytes.empty()) {
const auto frame = source_();
if (!frame) {
return {TransferStatus::idle, {}};
}
stream->bytes = endpoint == 0x82 ? build_depth(*frame)
: build_video(*frame, infrared);
stream->offset = 0;
stream->timestamp += 1;
stream->frame_number = frame->metadata.frame_number;
}
const std::size_t remaining = stream->bytes.size() - stream->offset;
const std::size_t payload = std::min(payload_size, remaining);
const bool end_of_frame = payload == remaining;
const uint8_t flag =
static_cast<uint8_t>(base_flag | (end_of_frame ? 5 : (stream->offset == 0 ? 1 : 2)));
Bytes packet(12 + payload);
packet[0] = 0x52; // 'R'
packet[1] = 0x42; // 'B', the iso packet magic checked at cameras.c:102
packet[3] = flag;
packet[5] = stream->sequence;
wire::put32(packet, 8, stream->timestamp);
std::copy_n(stream->bytes.begin() + static_cast<std::ptrdiff_t>(stream->offset),
payload, packet.begin() + 12);
stream->offset += payload;
++stream->sequence;
if (end_of_frame) {
stream->bytes.clear();
stream->offset = 0;
}
return {TransferStatus::completed, std::move(packet)};
}
} // namespace nui::protocol