37 #define MAKE_RESERVED(res, fmt) (uint32_t)(((res & 0xff) << 8) | (((fmt & 0xff)))) 38 #define RESERVED_TO_RESOLUTION(reserved) (freenect_resolution)((reserved >> 8) & 0xff) 39 #define RESERVED_TO_FORMAT(reserved) ((reserved) & 0xff) 41 #define video_mode_count 12 44 {
MAKE_RESERVED(
FREENECT_RESOLUTION_HIGH,
FREENECT_VIDEO_RGB),
FREENECT_RESOLUTION_HIGH, {
FREENECT_VIDEO_RGB}, 1280*1024*3, 1280, 1024, 24, 0, 10, 1 },
45 {
MAKE_RESERVED(
FREENECT_RESOLUTION_MEDIUM,
FREENECT_VIDEO_RGB),
FREENECT_RESOLUTION_MEDIUM, {
FREENECT_VIDEO_RGB}, 640*480*3, 640, 480, 24, 0, 30, 1 },
47 {
MAKE_RESERVED(
FREENECT_RESOLUTION_HIGH,
FREENECT_VIDEO_BAYER),
FREENECT_RESOLUTION_HIGH, {
FREENECT_VIDEO_BAYER}, 1280*1024, 1280, 1024, 8, 0, 10, 1 },
48 {
MAKE_RESERVED(
FREENECT_RESOLUTION_MEDIUM,
FREENECT_VIDEO_BAYER),
FREENECT_RESOLUTION_MEDIUM, {
FREENECT_VIDEO_BAYER}, 640*480, 640, 480, 8, 0, 30, 1 },
50 {
MAKE_RESERVED(
FREENECT_RESOLUTION_HIGH,
FREENECT_VIDEO_IR_8BIT),
FREENECT_RESOLUTION_HIGH, {
FREENECT_VIDEO_IR_8BIT}, 1280*1024, 1280, 1024, 8, 0, 10, 1 },
51 {
MAKE_RESERVED(
FREENECT_RESOLUTION_MEDIUM,
FREENECT_VIDEO_IR_8BIT),
FREENECT_RESOLUTION_MEDIUM, {
FREENECT_VIDEO_IR_8BIT}, 640*488, 640, 488, 8, 0, 30, 1 },
53 {
MAKE_RESERVED(
FREENECT_RESOLUTION_HIGH,
FREENECT_VIDEO_IR_10BIT),
FREENECT_RESOLUTION_HIGH, {
FREENECT_VIDEO_IR_10BIT}, 1280*1024*2, 1280, 1024, 10, 6, 10, 1 },
54 {
MAKE_RESERVED(
FREENECT_RESOLUTION_MEDIUM,
FREENECT_VIDEO_IR_10BIT),
FREENECT_RESOLUTION_MEDIUM, {
FREENECT_VIDEO_IR_10BIT}, 640*488*2, 640, 488, 10, 6, 30, 1 },
56 {
MAKE_RESERVED(
FREENECT_RESOLUTION_HIGH,
FREENECT_VIDEO_IR_10BIT_PACKED),
FREENECT_RESOLUTION_HIGH, {
FREENECT_VIDEO_IR_10BIT_PACKED}, 1280*1024*10/8, 1280, 1024, 10, 0, 10, 1 },
57 {
MAKE_RESERVED(
FREENECT_RESOLUTION_MEDIUM,
FREENECT_VIDEO_IR_10BIT_PACKED),
FREENECT_RESOLUTION_MEDIUM, {
FREENECT_VIDEO_IR_10BIT_PACKED}, 640*488*10/8, 640, 488, 10, 0, 30, 1 },
59 {
MAKE_RESERVED(
FREENECT_RESOLUTION_MEDIUM,
FREENECT_VIDEO_YUV_RGB),
FREENECT_RESOLUTION_MEDIUM, {
FREENECT_VIDEO_YUV_RGB}, 640*480*3, 640, 480, 24, 0, 15, 1 },
61 {
MAKE_RESERVED(
FREENECT_RESOLUTION_MEDIUM,
FREENECT_VIDEO_YUV_RAW),
FREENECT_RESOLUTION_MEDIUM, {
FREENECT_VIDEO_YUV_RAW}, 640*480*2, 640, 480, 16, 0, 15, 1 },
64 #define depth_mode_count 6 67 {
MAKE_RESERVED(
FREENECT_RESOLUTION_MEDIUM,
FREENECT_DEPTH_11BIT),
FREENECT_RESOLUTION_MEDIUM, {
FREENECT_DEPTH_11BIT}, 640*480*2, 640, 480, 11, 5, 30, 1},
68 {
MAKE_RESERVED(
FREENECT_RESOLUTION_MEDIUM,
FREENECT_DEPTH_10BIT),
FREENECT_RESOLUTION_MEDIUM, {
FREENECT_DEPTH_10BIT}, 640*480*2, 640, 480, 10, 6, 30, 1},
69 {
MAKE_RESERVED(
FREENECT_RESOLUTION_MEDIUM,
FREENECT_DEPTH_11BIT_PACKED),
FREENECT_RESOLUTION_MEDIUM, {
FREENECT_DEPTH_11BIT_PACKED}, 640*480*11/8, 640, 480, 11, 0, 30, 1},
70 {
MAKE_RESERVED(
FREENECT_RESOLUTION_MEDIUM,
FREENECT_DEPTH_10BIT_PACKED),
FREENECT_RESOLUTION_MEDIUM, {
FREENECT_DEPTH_10BIT_PACKED}, 640*480*10/8, 640, 480, 10, 0, 30, 1},
71 {
MAKE_RESERVED(
FREENECT_RESOLUTION_MEDIUM,
FREENECT_DEPTH_REGISTERED),
FREENECT_RESOLUTION_MEDIUM, {
FREENECT_DEPTH_REGISTERED}, 640*480*2, 640, 480, 16, 0, 30, 1},
72 {
MAKE_RESERVED(
FREENECT_RESOLUTION_MEDIUM,
FREENECT_DEPTH_MM),
FREENECT_RESOLUTION_MEDIUM, {
FREENECT_DEPTH_MM}, 640*480*2, 640, 480, 16, 0, 30, 1},
93 uint8_t *data = pkt +
sizeof(*hdr);
94 int datalen = len -
sizeof(*hdr);
100 l_info = l_notice = l_warning =
LL_SPEW;
102 if (hdr->magic[0] !=
'R' || hdr->magic[1] !=
'B') {
103 FN_LOG(l_notice,
"[Stream %02x] Invalid magic %02x%02x\n",
104 strm->
flag, hdr->magic[0], hdr->magic[1]);
108 FN_FLOOD(
"[Stream %02x] Packet with flag: %02x\n", strm->
flag, hdr->flag);
116 if (hdr->flag != sof) {
117 FN_SPEW(
"[Stream %02x] Not synced yet...\n", strm->
flag);
121 strm->
seq = hdr->seq;
127 int got_frame_size = 0;
130 if (strm->
seq != hdr->seq) {
133 FN_LOG(l_info,
"[Stream %02x] Lost %d packets\n", strm->
flag, lost);
135 FN_DEBUG(
"[Stream %02x] Lost %d total packets in %d frames (%f lppf)\n",
139 FN_LOG(l_notice,
"[Stream %02x] Lost too many packets, resyncing...\n", strm->
flag);
143 strm->
seq = hdr->seq;
161 if (!(strm->
pkt_num == 0 && hdr->flag == sof) &&
164 FN_LOG(l_notice,
"[Stream %02x] Inconsistent flag %02x with %d packets in buf (%d total), resyncing...\n",
167 return got_frame_size;
170 if (datalen > expected_pkt_size) {
171 FN_LOG(l_warning,
"[Stream %02x] Expected max %d data bytes, but got %d. Dropping...\n",
172 strm->
flag, expected_pkt_size, datalen);
173 return got_frame_size;
175 if (datalen < expected_pkt_size)
176 FN_LOG(l_warning,
"[Stream %02x] Expected %d data bytes, but got %d\n",
177 strm->
flag, expected_pkt_size, datalen);
180 if (!(strm->
pkt_num == 0 && hdr->flag == sof) &&
182 FN_LOG(l_notice,
"[Stream %02x] Inconsistent flag %02x with %d packets in buf (%d total), resyncing...\n",
185 return got_frame_size;
188 if (datalen > expected_pkt_size) {
189 FN_LOG(l_warning,
"[Stream %02x] Expected max %d data bytes, but got %d. Resyncng...\n",
190 strm->
flag, expected_pkt_size, datalen);
192 return got_frame_size;
194 if (datalen < expected_pkt_size && hdr->
flag != eof) {
195 FN_LOG(l_warning,
"[Stream %02x] Expected %d data bytes, but got %d. Resyncing...\n",
196 strm->
flag, expected_pkt_size, datalen);
198 return got_frame_size;
207 memcpy(dbuf, data, datalen);
216 if (hdr->flag == eof) {
218 got_frame_size = (dbuf - strm->
raw_buf) + datalen;
228 return got_frame_size;
279 FN_ERROR(
"Attempted to set buffer to NULL but stream was started with no internal buffer\n");
306 unsigned int mask = (1 << vw) - 1;
310 while (bitsIn < vw) {
311 buffer = (buffer << 8) | *(src++);
315 *(dest++) = (buffer >> bitsIn) & mask;
335 while (bitsIn < vw) {
336 buffer = (buffer << 8) | *(src++);
340 *(dest++) = buffer >> (bitsIn + vw - 8);
362 frame[0] = (r0<<3) | (r1>>5);
363 frame[1] = ((r1<<6) | (r2>>2) ) & baseMask;
364 frame[2] = ((r2<<9) | (r3<<1) | (r4>>7) ) & baseMask;
365 frame[3] = ((r4<<4) | (r5>>4) ) & baseMask;
366 frame[4] = ((r5<<7) | (r6>>1) ) & baseMask;
367 frame[5] = ((r6<<10) | (r7<<2) | (r8>>6) ) & baseMask;
368 frame[6] = ((r8<<5) | (r9>>3) ) & baseMask;
369 frame[7] = ((r9<<8) | (r10) ) & baseMask;
392 FN_SPEW(
"Got depth frame of size %d/%d, %d/%d packets arrived, TS %08x\n", got_frame_size,
412 FN_ERROR(
"depth_process() was called, but an invalid depth_format is set\n");
419 #define CLAMP(x) if (x < 0) {x = 0;} if (x > 255) {x = 255;} 423 for(y = 0; y < frame_mode.
height; ++y) {
424 for(x = 0; x < frame_mode.
width; x+=2) {
425 int i = (frame_mode.
width * y + x);
426 int u = raw_buf[2*i];
427 int y1 = raw_buf[2*i+1];
428 int v = raw_buf[2*i+2];
429 int y2 = raw_buf[2*i+3];
430 int r1 = (y1-16)*1164/1000 + (v-128)*1596/1000;
431 int g1 = (y1-16)*1164/1000 - (v-128)*813/1000 - (u-128)*391/1000;
432 int b1 = (y1-16)*1164/1000 + (u-128)*2018/1000;
433 int r2 = (y2-16)*1164/1000 + (v-128)*1596/1000;
434 int g2 = (y2-16)*1164/1000 - (v-128)*813/1000 - (u-128)*391/1000;
435 int b2 = (y2-16)*1164/1000 + (u-128)*2018/1000;
523 nextLine = curLine + frame_mode.
width;
524 for (y = 0; y < frame_mode.
height; ++y) {
526 if ((y > 0) && (y < frame_mode.
height-1))
527 prevLine = curLine - frame_mode.
width;
534 hVals = (*(curLine++) << 8);
536 hVals |= (*curLine << 16);
538 vSums = ((*(prevLine++) + *(nextLine++)) << 7) & 0xFF00;
540 vSums |= ((*prevLine + *nextLine) << 15) & 0xFF0000;
546 for (x = 0; x < frame_mode.
width-1; ++x) {
548 hVals |= *(curLine++);
549 vSums |= (*(prevLine++) + *(nextLine++)) >> 1;
559 *(dst++) = hVals >> 8;
560 *(dst++) = vSums >> 8;
563 *(dst++) = hVals >> 8;
564 *(dst++) = (hSum + (
uint8_t)(vSums >> 8)) >> 1;
571 *(dst++) = (hSum + (
uint8_t)(vSums >> 8)) >> 1;
572 *(dst++) = hVals >> 8;
575 *(dst++) = vSums >> 8;
576 *(dst++) = hVals >> 8;
586 hVals |= (
uint8_t)(hVals >> 16);
587 vSums |= (
uint8_t)(vSums >> 16);
595 *(dst++) = hVals >> 8;
596 *(dst++) = vSums >> 8;
598 *(dst++) = hVals >> 8;
599 *(dst++) = (hSum + (
uint8_t)(vSums >> 8)) >> 1;
605 *(dst++) = (hSum + (
uint8_t)(vSums >> 8)) >> 1;
606 *(dst++) = hVals >> 8;
608 *(dst++) = vSums >> 8;
609 *(dst++) = hVals >> 8;
632 FN_SPEW(
"Got video frame of size %d/%d, %d/%d packets arrived, TS %08x\n", got_frame_size,
656 FN_ERROR(
"video_process() was called, but an invalid video_format is set\n");
677 res =
send_cmd(dev, 0x16, cmd, 10, reply, 118);
679 FN_ERROR(
"freenect_fetch_reg_info: send_cmd read %d bytes (expected 118)\n", res);
754 res =
send_cmd(dev, 0x16, cmd, 10, reply, 8);
756 FN_ERROR(
"freenect_fetch_reg_pad_info: send_cmd read %d bytes (expected 8)\n", res);
781 res =
send_cmd(dev, 0x16, cmd, 10, reply, 4);
783 FN_ERROR(
"freenect_fetch_reg_const_shift: send_cmd read %d bytes (expected 8)\n", res);
787 memcpy(&shift, reply+2,
sizeof(shift));
804 FN_ERROR(
"freenect_fetch_zero_plane_info: send_cmd read %d bytes (expected %d)\n", res,ctx->
zero_plane_res);
814 conversion_union.ui =
fn_le32(conversion_union.ui);
818 conversion_union.ui =
fn_le32(conversion_union.ui);
822 conversion_union.ui =
fn_le32(conversion_union.ui);
826 conversion_union.ui =
fn_le32(conversion_union.ui);
930 FN_ERROR(
"freenect_start_video(): called with invalid format/resolution combination\n");
943 FN_ERROR(
"freenect_start_video(): cannot stream high-resolution IR at same time as depth stream\n");
963 FN_ERROR(
"freenect_start_video(): called with invalid format/resolution combination\n");
978 FN_ERROR(
"freenect_start_video(): called with invalid format/resolution combination\n");
1061 FN_ERROR(
"Failed to stop depth isochronous stream: %d\n", res);
1083 FN_ERROR(
"Failed to stop RGB isochronous stream: %d\n", res);
1120 return supported_video_modes[mode_num];
1136 if (supported_video_modes[i].reserved == unique_id)
1137 return supported_video_modes[i];
1148 FN_ERROR(
"Tried to set video mode while stream is active\n");
1155 if (supported_video_modes[i].reserved == mode.
reserved) {
1161 FN_ERROR(
"freenect_set_video_mode: freenect_frame_mode provided is invalid\n");
1183 return supported_depth_modes[mode_num];
1199 if (supported_depth_modes[i].reserved == unique_id)
1200 return supported_depth_modes[i];
1211 FN_ERROR(
"Tried to set depth mode while stream is active\n");
1218 if (supported_depth_modes[i].reserved == mode.
reserved) {
1224 FN_ERROR(
"freenect_set_depth_mode: freenect_frame_mode provided is invalid\n");
1249 FN_ERROR(
"freenect_camera_init(): Failed to fetch registration pad info for device\n");
1254 FN_ERROR(
"freenect_camera_init(): Failed to fetch zero plane info for device\n");
1261 FN_ERROR(
"freenect_camera_init(): Failed to fetch const shift for device\n");
1274 FN_ERROR(
"freenect_camera_teardown(): Failed to stop depth camera\n");
1281 FN_ERROR(
"freenect_camera_teardown(): Failed to stop video camera\n");
freenect_chunk_cb depth_chunk_cb
void freenect_set_video_chunk_callback(freenect_device *dev, freenect_chunk_cb cb)
FREENECTAPI int freenect_destroy_registration(freenect_registration *reg)
freenect_video_format video_format
int freenect_set_depth_buffer(freenect_device *dev, void *buf)
FN_INTERNAL int freenect_init_registration(freenect_device *dev)
freenect_context * parent
FN_INTERNAL int fnusb_start_iso(fnusb_dev *dev, fnusb_isoc_stream *strm, fnusb_iso_cb cb, int ep, int xfers, int pkts, int len)
freenect_zero_plane_info zero_plane_info
int freenect_get_video_mode_count()
static void stream_init(freenect_context *ctx, packet_stream *strm, int rlen, int plen)
freenect_registration registration
void freenect_set_video_callback(freenect_device *dev, freenect_video_cb cb)
static void convert_bayer_to_rgb(uint8_t *raw_buf, uint8_t *proc_buf, freenect_frame_mode frame_mode)
int freenect_start_video(freenect_device *dev)
freenect_depth_format depth_format
freenect_reg_info reg_info
static int freenect_fetch_reg_const_shift(freenect_device *dev)
FN_INTERNAL int fnusb_stop_iso(fnusb_dev *dev, fnusb_isoc_stream *strm)
freenect_frame_mode freenect_get_depth_mode(int mode_num)
static void video_process(freenect_device *dev, uint8_t *pkt, int len)
#define FN_LOG(level,...)
int freenect_stop_video(freenect_device *dev)
static int stream_setbuf(freenect_context *ctx, packet_stream *strm, void *pbuf)
fnusb_isoc_stream video_isoc
static int freenect_fetch_reg_pad_info(freenect_device *dev)
void freenect_set_depth_chunk_callback(freenect_device *dev, freenect_chunk_cb cb)
freenect_resolution depth_resolution
void(* freenect_depth_cb)(freenect_device *dev, void *depth, uint32_t timestamp)
Typedef for depth image received event callbacks.
freenect_depth_cb depth_cb
int freenect_set_depth_mode(freenect_device *dev, const freenect_frame_mode mode)
static int freenect_fetch_reg_info(freenect_device *dev)
int freenect_set_video_mode(freenect_device *dev, const freenect_frame_mode mode)
freenect_frame_mode freenect_find_video_mode(freenect_resolution res, freenect_video_format fmt)
freenect_loglevel
Enumeration of message logging levels.
static int stream_process(freenect_context *ctx, packet_stream *strm, uint8_t *pkt, int len, freenect_chunk_cb cb, void *user_data)
static void convert_packed_to_8bit(uint8_t *src, uint8_t *dest, int vw, int n)
int freenect_set_video_buffer(freenect_device *dev, void *buf)
static void convert_uyvy_to_rgb(uint8_t *raw_buf, uint8_t *proc_buf, freenect_frame_mode frame_mode)
freenect_frame_mode freenect_get_video_mode(int mode_num)
freenect_frame_mode freenect_find_depth_mode(freenect_resolution res, freenect_depth_format fmt)
static void stream_freebufs(freenect_context *ctx, packet_stream *strm)
static void depth_process(freenect_device *dev, uint8_t *pkt, int len)
static freenect_frame_mode supported_video_modes[video_mode_count]
static const freenect_frame_mode invalid_mode
static freenect_context * ctx
int freenect_start_depth(freenect_device *dev)
FN_INTERNAL int write_register(freenect_device *dev, uint16_t reg, uint16_t data)
#define MAKE_RESERVED(res, fmt)
static freenect_frame_mode supported_depth_modes[depth_mode_count]
static void convert_packed11_to_16bit(uint8_t *raw, uint16_t *frame, int n)
freenect_resolution video_resolution
FN_INTERNAL int send_cmd(freenect_device *dev, uint16_t cmd, void *cmdbuf, unsigned int cmd_len, void *replybuf, int reply_len)
freenect_reg_pad_info reg_pad_info
FN_INTERNAL int freenect_camera_teardown(freenect_device *dev)
int freenect_stop_depth(freenect_device *dev)
freenect_frame_mode freenect_get_current_depth_mode(freenect_device *dev)
fnusb_isoc_stream depth_isoc
static int freenect_fetch_zero_plane_info(freenect_device *dev)
float reference_pixel_size
FN_INTERNAL int freenect_apply_depth_to_mm(freenect_device *dev, uint8_t *input_packed, uint16_t *output_mm)
freenect_resolution resolution
void(* freenect_chunk_cb)(void *buffer, void *pkt_data, int pkt_num, int datalen, void *user_data)
Typedef for stream chunk processing callbacks.
freenect_chunk_cb video_chunk_cb
void(* freenect_video_cb)(freenect_device *dev, void *video, uint32_t timestamp)
Typedef for video image received event callbacks.
static void convert_packed_to_16bit(uint8_t *src, uint16_t *dest, int vw, int n)
FN_INTERNAL int freenect_camera_init(freenect_device *dev)
int freenect_get_depth_mode_count()
freenect_video_cb video_cb
void freenect_set_depth_callback(freenect_device *dev, freenect_depth_cb cb)
freenect_frame_mode freenect_get_current_video_mode(freenect_device *dev)
#define RESERVED_TO_RESOLUTION(reserved)
#define RESERVED_TO_FORMAT(reserved)
FN_INTERNAL int freenect_apply_registration(freenect_device *dev, uint8_t *input_packed, uint16_t *output_mm)