Skip to content

Commit

Permalink
update libfreenect to fix ofxKinect on raspberry pi (#6123)
Browse files Browse the repository at this point in the history
  • Loading branch information
npisanti authored and arturoc committed Sep 8, 2018
1 parent 12c9912 commit 1b71506
Show file tree
Hide file tree
Showing 11 changed files with 445 additions and 433 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#ifndef _SSIZE_T_DEFINED
#define _SSIZE_T_DEFINED
#ifdef _WIN64
typedef __int64 ssize_t;
typedef __int64 ssize_t;
#else
typedef int ssize_t;
#endif
Expand Down
2 changes: 1 addition & 1 deletion addons/ofxKinect/libs/libfreenect/src/cameras.c
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ static void depth_process(freenect_device *dev, uint8_t *pkt, int len)
convert_packed11_to_16bit(dev->depth.raw_buf, (uint16_t*)dev->depth.proc_buf, 640*480);
break;
case FREENECT_DEPTH_REGISTERED:
freenect_apply_registration(dev, dev->depth.raw_buf, (uint16_t*)dev->depth.proc_buf );
freenect_apply_registration(dev, dev->depth.raw_buf, (uint16_t*)dev->depth.proc_buf, false);
break;
case FREENECT_DEPTH_MM:
freenect_apply_depth_to_mm(dev, dev->depth.raw_buf, (uint16_t*)dev->depth.proc_buf );
Expand Down
8 changes: 4 additions & 4 deletions addons/ofxKinect/libs/libfreenect/src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ FREENECTAPI int freenect_init(freenect_context **ctx, freenect_usb_context *usb_

memset(*ctx, 0, sizeof(freenect_context));

(*ctx)->log_level = LL_WARNING;
(*ctx)->log_level = LL_NOTICE;
(*ctx)->enabled_subdevices = (freenect_device_flags)(FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA);
res = fnusb_init(&(*ctx)->usb, usb_ctx);
if (res < 0) {
Expand Down Expand Up @@ -101,12 +101,12 @@ FREENECTAPI int freenect_process_events_timeout(freenect_context *ctx, struct ti

FREENECTAPI int freenect_num_devices(freenect_context *ctx)
{
return fnusb_num_devices(&ctx->usb);
return fnusb_num_devices(ctx);
}

FREENECTAPI int freenect_list_device_attributes(freenect_context *ctx, struct freenect_device_attributes **attribute_list)
{
return fnusb_list_device_attributes(&ctx->usb, attribute_list);
return fnusb_list_device_attributes(ctx, attribute_list);
}

FREENECTAPI void freenect_free_device_attributes(struct freenect_device_attributes *attribute_list)
Expand Down Expand Up @@ -185,7 +185,7 @@ FREENECTAPI int freenect_open_device_by_camera_serial(freenect_context *ctx, fre
// freenect_open_device with that index.
struct freenect_device_attributes* attrlist;
struct freenect_device_attributes* item;
int count = fnusb_list_device_attributes(&ctx->usb, &attrlist);
int count = fnusb_list_device_attributes(ctx, &attrlist);
if (count < 0) {
FN_ERROR("freenect_open_device_by_camera_serial: Couldn't enumerate serial numbers\n");
return count;
Expand Down
1 change: 1 addition & 0 deletions addons/ofxKinect/libs/libfreenect/src/flags.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ FN_INTERNAL int send_cmd(freenect_device *dev, uint16_t cmd, void *cmdbuf, unsig
do {
actual_len = fnusb_control(&dev->usb_cam, 0xc0, 0, 0, 0, ibuf, 0x200);
FN_FLOOD("send_cmd: actual length = %d\n", actual_len);
usleep(1);
} while ((actual_len == 0) || (actual_len == 0x200));
FN_SPEW("Control reply: %d\n", res);
if (actual_len < (int)sizeof(*rhdr)) {
Expand Down
1 change: 1 addition & 0 deletions addons/ofxKinect/libs/libfreenect/src/freenect_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ static inline int32_t fn_le32s(int32_t s)
#define PID_NUI_CAMERA 0x02ae
#define PID_NUI_MOTOR 0x02b0
#define PID_K4W_CAMERA 0x02bf
#define PID_KV2_CAMERA 0x02d9 // This identifies the Kinect v2 (XBox One). Use https://github.com/OpenKinect/libfreenect2 instead.

// For K4W: first pid is what it starts out as,
// second is how it appears with lastest firmware from SDK,
Expand Down
Empty file modified addons/ofxKinect/libs/libfreenect/src/libfreenect.pc.in
100755 → 100644
Empty file.
41 changes: 32 additions & 9 deletions addons/ofxKinect/libs/libfreenect/src/registration.c
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static inline void unpack_8_pixels(uint8_t *raw, uint16_t *frame)
}

// apply registration data to a single packed frame
FN_INTERNAL int freenect_apply_registration(freenect_device* dev, uint8_t* input_packed, uint16_t* output_mm)
FN_INTERNAL int freenect_apply_registration(freenect_device* dev, uint8_t* input, uint16_t* output_mm, bool unpacked)
{
freenect_registration* reg = &(dev->registration);
// set output buffer to zero using pointer-sized memory access (~ 30-40% faster than memset)
Expand All @@ -116,15 +116,22 @@ FN_INTERNAL int freenect_apply_registration(freenect_device* dev, uint8_t* input
for (y = 0; y < DEPTH_Y_RES; y++) {
for (x = 0; x < DEPTH_X_RES; x++) {

// get 8 pixels from the packed frame
if (source_index == 8) {
unpack_8_pixels( input_packed, unpack );
source_index = 0;
input_packed += 11;
}
uint16_t metric_depth;

// get the value at the current depth pixel, convert to millimeters
uint16_t metric_depth = reg->raw_to_mm_shift[ unpack[source_index++] ];
if (unpacked) {
uint32_t buf_index = y * DEPTH_X_RES + x;
metric_depth = reg->raw_to_mm_shift[((uint16_t *)input)[buf_index]];
} else {
// get 8 pixels from the packed frame
if (source_index == 8) {
unpack_8_pixels( input, unpack );
source_index = 0;
input += 11;
}

// get the value at the current depth pixel, convert to millimeters
metric_depth = reg->raw_to_mm_shift[ unpack[source_index++] ];
}

// so long as the current pixel has a depth value
if (metric_depth == DEPTH_NO_MM_VALUE) continue;
Expand Down Expand Up @@ -190,6 +197,22 @@ FN_INTERNAL int freenect_apply_depth_to_mm(freenect_device* dev, uint8_t* input_
return 0;
}

// Same as freenect_apply_depth_to_mm, but don't need to unpack 11 bit depth values
FN_INTERNAL int freenect_apply_depth_unpacked_to_mm(freenect_device* dev, uint16_t* input, uint16_t* output_mm)
{
freenect_registration* reg = &(dev->registration);
uint32_t x,y;
for (y = 0; y < DEPTH_Y_RES; y++) {
for (x = 0; x < DEPTH_X_RES; x++) {
// get the value at the current depth pixel, convert to millimeters
uint32_t buf_index = y * DEPTH_X_RES + x;
uint16_t metric_depth = reg->raw_to_mm_shift[input[buf_index]];
output_mm[buf_index] = metric_depth < DEPTH_MAX_METRIC_VALUE ? metric_depth : DEPTH_MAX_METRIC_VALUE;
}
}
return 0;
}

// create temporary x/y shift tables
static void freenect_create_dxdy_tables(double* reg_x_table, double* reg_y_table, int32_t resolution_x, int32_t resolution_y, freenect_reg_info* regdata )
{
Expand Down
4 changes: 3 additions & 1 deletion addons/ofxKinect/libs/libfreenect/src/registration.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@

#pragma once

#include <stdbool.h>
#include "libfreenect.h"

// Internal function declarations relating to registration
int freenect_init_registration(freenect_device* dev);
int freenect_apply_registration(freenect_device* dev, uint8_t* input_packed, uint16_t* output_mm);
int freenect_apply_registration(freenect_device* dev, uint8_t* input, uint16_t* output_mm, bool unpacked);
int freenect_apply_depth_to_mm(freenect_device* dev, uint8_t* input_packed, uint16_t* output_mm);
int freenect_apply_depth_unpacked_to_mm(freenect_device* dev, uint16_t* input, uint16_t* output_mm);
6 changes: 3 additions & 3 deletions addons/ofxKinect/libs/libfreenect/src/tilt.c
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ int get_reply(libusb_device_handle* dev, freenect_context *ctx){
int res = 0;
res = libusb_bulk_transfer(dev, 0x81, buffer, 512, &transferred, 100);
if (res != 0) {
FN_ERROR("get_reply(): libusb_bulk_transfer failed: %d (transferred = %d)\n", res, transferred);
FN_ERROR("get_reply(): libusb_bulk_transfer failed: %s (transferred = %d)\n", libusb_error_name(res), transferred);
} else if (transferred != 12) {
FN_ERROR("get_reply(): weird - got %d bytes (expected 12)\n", transferred);
} else {
Expand Down Expand Up @@ -214,7 +214,7 @@ int freenect_set_tilt_degs_alt(freenect_device *dev, int tilt_degrees)

res = libusb_bulk_transfer(dev->usb_audio.dev, 0x01, buffer, 20, &transferred, 250);
if (res != 0) {
FN_ERROR("freenect_set_tilt_alt(): libusb_bulk_transfer failed: %d (transferred = %d)\n", res, transferred);
FN_ERROR("freenect_set_tilt_alt(): libusb_bulk_transfer failed: %s (transferred = %d)\n", libusb_error_name(res), transferred);
return res;
}

Expand Down Expand Up @@ -281,7 +281,7 @@ FN_INTERNAL int fnusb_set_led_alt(libusb_device_handle * dev, freenect_context *
int transferred = 0;
int res = libusb_bulk_transfer(dev, 0x01, buffer, 20, &transferred, 100);
if (res != 0) {
FN_WARNING("fnusb_set_led_alt(): libusb_bulk_transfer failed: %d (transferred = %d)\n", res, transferred);
FN_WARNING("fnusb_set_led_alt(): libusb_bulk_transfer failed: %s (transferred = %d)\n", libusb_error_name(res), transferred);
return res;
}
return get_reply(dev, ctx);
Expand Down
Loading

0 comments on commit 1b71506

Please sign in to comment.