Skip to content

Commit 752e2f2

Browse files
committed
API rework, round 2.
Bindings still probably broken: Python, Java, AS3, C#, Matlab, Ruby Signed-off-by: Drew Fisher <drew.m.fisher@gmail.com> (zarvox)
1 parent 9b533d4 commit 752e2f2

File tree

11 files changed

+302
-244
lines changed

11 files changed

+302
-244
lines changed

examples/glview.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,8 @@ void *freenect_threadfunc(void *arg)
338338
freenect_set_led(f_dev,LED_RED);
339339
freenect_set_depth_callback(f_dev, depth_cb);
340340
freenect_set_video_callback(f_dev, rgb_cb);
341-
freenect_set_video_format(f_dev, current_format);
342-
freenect_set_video_resolution(f_dev, FREENECT_RESOLUTION_MEDIUM);
343-
freenect_set_depth_format(f_dev, FREENECT_DEPTH_11BIT);
341+
freenect_set_video_mode(f_dev, freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, current_format));
342+
freenect_set_depth_mode(f_dev, freenect_find_depth_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_11BIT));
344343
freenect_set_video_buffer(f_dev, rgb_back);
345344

346345
freenect_start_depth(f_dev);
@@ -364,7 +363,7 @@ void *freenect_threadfunc(void *arg)
364363

365364
if (requested_format != current_format) {
366365
freenect_stop_video(f_dev);
367-
freenect_set_video_format(f_dev, requested_format);
366+
freenect_set_video_mode(f_dev, freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, requested_format));
368367
freenect_start_video(f_dev);
369368
current_format = requested_format;
370369
}

examples/hiview.c

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ int freenect_led;
7272

7373
freenect_video_format requested_format = FREENECT_VIDEO_RGB;
7474
freenect_video_format current_format = FREENECT_VIDEO_RGB;
75-
freenect_video_resolution requested_resolution = FREENECT_RESOLUTION_HIGH;
76-
freenect_video_resolution current_resolution = FREENECT_RESOLUTION_HIGH;
75+
freenect_resolution requested_resolution = FREENECT_RESOLUTION_HIGH;
76+
freenect_resolution current_resolution = FREENECT_RESOLUTION_HIGH;
7777

7878
pthread_cond_t gl_frame_cond = PTHREAD_COND_INITIALIZER;
7979
int got_rgb = 0;
@@ -133,7 +133,7 @@ void DrawVideoScene()
133133

134134
pthread_mutex_lock(&video_mutex);
135135

136-
freenect_frame_size frame_size = freenect_get_video_frame_size(current_format, current_resolution);
136+
freenect_frame_mode frame_mode = freenect_get_current_video_mode(f_dev);
137137

138138
if (got_rgb) {
139139
uint8_t *tmp = rgb_front;
@@ -151,16 +151,16 @@ void DrawVideoScene()
151151

152152
glBindTexture(GL_TEXTURE_2D, gl_rgb_tex);
153153
if (current_format == FREENECT_VIDEO_RGB || current_format == FREENECT_VIDEO_YUV_RGB) {
154-
glTexImage2D(GL_TEXTURE_2D, 0, 3, frame_size.width, frame_size.height, 0, GL_RGB, GL_UNSIGNED_BYTE, rgb_front);
154+
glTexImage2D(GL_TEXTURE_2D, 0, 3, frame_mode.width, frame_mode.height, 0, GL_RGB, GL_UNSIGNED_BYTE, rgb_front);
155155
} else {
156-
glTexImage2D(GL_TEXTURE_2D, 0, 1, frame_size.width, frame_size.height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, rgb_front);
156+
glTexImage2D(GL_TEXTURE_2D, 0, 1, frame_mode.width, frame_mode.height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, rgb_front);
157157
}
158158
glBegin(GL_TRIANGLE_FAN);
159159
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
160160
glTexCoord2f(0, 0); glVertex3f(0,0,0);
161-
glTexCoord2f(1, 0); glVertex3f(frame_size.width,0,0);
162-
glTexCoord2f(1, 1); glVertex3f(frame_size.width,frame_size.height,0);
163-
glTexCoord2f(0, 1); glVertex3f(0,frame_size.height,0);
161+
glTexCoord2f(1, 0); glVertex3f(frame_mode.width,0,0);
162+
glTexCoord2f(1, 1); glVertex3f(frame_mode.width,frame_mode.height,0);
163+
glTexCoord2f(0, 1); glVertex3f(0,frame_mode.height,0);
164164
glEnd();
165165

166166
glutSwapBuffers();
@@ -211,7 +211,7 @@ void keyPressed(unsigned char key, int x, int y)
211211
}
212212
}
213213
glutSetWindow(video_window);
214-
freenect_frame_size s = freenect_get_video_frame_size(requested_format, requested_resolution);
214+
freenect_frame_mode s = freenect_find_video_mode(requested_resolution, requested_format);
215215
glutReshapeWindow(s.width, s.height);
216216
}
217217
if (key == 'd') { // Toggle depth camera.
@@ -272,9 +272,9 @@ void *gl_threadfunc(void *arg)
272272
glutKeyboardFunc(&keyPressed);
273273
InitGL(640, 480);
274274

275-
freenect_frame_size frame = freenect_get_video_frame_size(current_format, current_resolution);
275+
freenect_frame_mode mode = freenect_find_video_mode(current_resolution, current_format);
276276
glutInitWindowPosition(640,0);
277-
glutInitWindowSize(frame.width, frame.height);
277+
glutInitWindowSize(mode.width, mode.height);
278278
video_window = glutCreateWindow("Video");
279279

280280
glutDisplayFunc(&DrawVideoScene);
@@ -283,7 +283,7 @@ void *gl_threadfunc(void *arg)
283283
glutKeyboardFunc(&keyPressed);
284284

285285
InitGL(640, 480);
286-
ReSizeGLScene(frame.width, frame.height);
286+
ReSizeGLScene(mode.width, mode.height);
287287

288288
glutMainLoop();
289289

@@ -362,12 +362,11 @@ void *freenect_threadfunc(void *arg)
362362
freenect_set_led(f_dev,LED_RED);
363363
freenect_set_depth_callback(f_dev, depth_cb);
364364
freenect_set_video_callback(f_dev, video_cb);
365-
freenect_set_video_format(f_dev, current_format);
366-
freenect_set_video_resolution(f_dev, current_resolution);
367-
freenect_set_depth_format(f_dev, FREENECT_DEPTH_11BIT);
368-
rgb_back = (uint8_t*)malloc(freenect_get_video_frame_size(current_format, current_resolution).bytes);
369-
rgb_mid = (uint8_t*)malloc(freenect_get_video_frame_size(current_format, current_resolution).bytes);
370-
rgb_front = (uint8_t*)malloc(freenect_get_video_frame_size(current_format, current_resolution).bytes);
365+
freenect_set_video_mode(f_dev, freenect_find_video_mode(current_resolution, current_format));
366+
freenect_set_depth_mode(f_dev, freenect_find_depth_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_11BIT));
367+
rgb_back = (uint8_t*)malloc(freenect_find_video_mode(current_resolution, current_format).bytes);
368+
rgb_mid = (uint8_t*)malloc(freenect_find_video_mode(current_resolution, current_format).bytes);
369+
rgb_front = (uint8_t*)malloc(freenect_find_video_mode(current_resolution, current_format).bytes);
371370
freenect_set_video_buffer(f_dev, rgb_back);
372371

373372
freenect_start_depth(f_dev);
@@ -378,15 +377,14 @@ void *freenect_threadfunc(void *arg)
378377
status = freenect_process_events(f_ctx);
379378
if (requested_format != current_format || requested_resolution != current_resolution) {
380379
freenect_stop_video(f_dev);
381-
freenect_set_video_format(f_dev, requested_format);
382-
freenect_set_video_resolution(f_dev, requested_resolution);
380+
freenect_set_video_mode(f_dev, freenect_find_video_mode(requested_resolution, requested_format));
383381
pthread_mutex_lock(&video_mutex);
384382
free(rgb_back);
385383
free(rgb_mid);
386384
free(rgb_front);
387-
rgb_back = (uint8_t*)malloc(freenect_get_video_frame_size(requested_format, requested_resolution).bytes);
388-
rgb_mid = (uint8_t*)malloc(freenect_get_video_frame_size(requested_format, requested_resolution).bytes);
389-
rgb_front = (uint8_t*)malloc(freenect_get_video_frame_size(requested_format, requested_resolution).bytes);
385+
rgb_back = (uint8_t*)malloc(freenect_find_video_mode(requested_resolution, requested_format).bytes);
386+
rgb_mid = (uint8_t*)malloc(freenect_find_video_mode(requested_resolution, requested_format).bytes);
387+
rgb_front = (uint8_t*)malloc(freenect_find_video_mode(requested_resolution, requested_format).bytes);
390388
current_format = requested_format;
391389
current_resolution = requested_resolution;
392390
pthread_mutex_unlock(&video_mutex);

fakenect/fakenect.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ int freenect_process_events(freenect_context *ctx)
194194
if (cur_rgb_cb && rgb_running) {
195195
void *cur_rgb = skip_line(data);
196196
if (rgb_buffer) {
197-
memcpy(rgb_buffer, cur_rgb, freenect_get_video_frame_size(FREENECT_VIDEO_RGB, FREENECT_RESOLUTION_MEDIUM).bytes);
197+
memcpy(rgb_buffer, cur_rgb, freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_RGB).bytes);
198198
cur_rgb = rgb_buffer;
199199
}
200200
cur_rgb_cb(fake_dev, cur_rgb, timestamp);

fakenect/record.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ void depth_cb(freenect_device *dev, void *depth, uint32_t timestamp)
119119

120120
void rgb_cb(freenect_device *dev, void *rgb, uint32_t timestamp)
121121
{
122-
dump('r', timestamp, rgb, freenect_get_current_video_frame_size(dev).bytes);
122+
dump('r', timestamp, rgb, freenect_get_current_video_mode(dev).bytes);
123123
}
124124

125125
void init()
@@ -135,10 +135,9 @@ void init()
135135
printf("Error: Cannot get device\n");
136136
return;
137137
}
138-
freenect_set_depth_format(dev, FREENECT_DEPTH_11BIT);
138+
freenect_set_depth_mode(dev, freenect_find_depth_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_11BIT));
139139
freenect_start_depth(dev);
140-
freenect_set_video_format(dev, FREENECT_VIDEO_RGB);
141-
freenect_set_video_resolution(dev, FREENECT_RESOLUTION_MEDIUM);
140+
freenect_set_video_mode(dev, freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_RGB));
142141
freenect_start_video(dev);
143142
freenect_set_depth_callback(dev, depth_cb);
144143
freenect_set_video_callback(dev, rgb_cb);

include/libfreenect.h

Lines changed: 101 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,29 @@ extern "C" {
4040

4141
#define FREENECT_COUNTS_PER_G 819 /**< Ticks per G for accelerometer as set per http://www.kionix.com/Product%20Sheets/KXSD9%20Product%20Brief.pdf */
4242

43-
/// Structure to give information about the width and height of a
44-
/// frame, as well as the total number of bytes needed to hold a
45-
/// single frame. Negative values for width and height indicate
46-
/// an invalid video format and resolution combination, as does
47-
/// a value of 0 for bytes.
43+
/// Structure to give information about the width, height, bitrate,
44+
/// framerate, and buffer size of a frame in a particular mode, as
45+
/// well as the total number of bytes needed to hold a single frame.
4846
typedef struct {
49-
int width; /**< Width of this frame, in pixels */
50-
int height; /**< Height of this frame, in pixels */
51-
int bytes; /**< Total number of bytes needed to hold a single frame at the given resolution and pixel format */
52-
} freenect_frame_size;
47+
uint32_t reserved; /**< unique ID used internally. The meaning of values may change without notice. Don't touch or depend on the contents of this field. We mean it. */
48+
int is_valid; /**< If 0, this freenect_frame_mode is invalid and does not describe a supported mode. Otherwise, the frame_mode is valid. */
49+
int bytes; /**< Total buffer size in bytes to hold a single frame of data. Should be equivalent to width * height * (data_bits_per_pixel+padding_bits_per_pixel) / 8 */
50+
int width; /**< Width of the frame, in pixels */
51+
int height; /**< Height of the frame, in pixels */
52+
int data_bits_per_pixel; /**< Number of bits of information needed for each pixel */
53+
int padding_bits_per_pixel; /**< Number of bits of padding for alignment used for each pixel */
54+
int framerate; /**< Approximate expected frame rate, in Hz */
55+
} freenect_frame_mode ;
5356

5457
/// Enumeration of available resolutions.
5558
/// Not all available resolutions are actually supported for all video formats.
59+
/// Frame modes may not perfectly match resolutions. For instance,
60+
/// FREENECT_RESOLUTION_MEDIUM is 640x488 for the IR camera.
5661
typedef enum {
5762
FREENECT_RESOLUTION_LOW = 0, /**< QVGA - 320x240 */
5863
FREENECT_RESOLUTION_MEDIUM = 1, /**< VGA - 640x480 */
5964
FREENECT_RESOLUTION_HIGH = 2, /**< SXGA - 1280x1024 */
60-
} freenect_video_resolution;
65+
} freenect_resolution;
6166

6267
/// Enumeration of video frame information states.
6368
/// See http://openkinect.org/wiki/Protocol_Documentation#RGB_Camera for more information.
@@ -271,26 +276,6 @@ FREENECTAPI void freenect_set_depth_callback(freenect_device *dev, freenect_dept
271276
*/
272277
FREENECTAPI void freenect_set_video_callback(freenect_device *dev, freenect_video_cb cb);
273278

274-
/**
275-
* Set the format for depth information
276-
*
277-
* @param dev Device to set depth information format for
278-
* @param fmt Format of depth information. See freenect_depth_format enum.
279-
*
280-
* @return 0 on success, < 0 on error
281-
*/
282-
FREENECTAPI int freenect_set_depth_format(freenect_device *dev, freenect_depth_format fmt);
283-
284-
/**
285-
* Set the format for video information
286-
*
287-
* @param dev Device to set video information format for
288-
* @param fmt Format of video information. See freenect_video_format enum.
289-
*
290-
* @return 0 on success, < 0 on error
291-
*/
292-
FREENECTAPI int freenect_set_video_format(freenect_device *dev, freenect_video_format fmt);
293-
294279
/**
295280
* Set the buffer to store depth information to. Size of buffer is
296281
* dependant on depth format. See FREENECT_DEPTH_*_SIZE defines for
@@ -429,40 +414,105 @@ FREENECTAPI int freenect_set_led(freenect_device *dev, freenect_led_options opti
429414
FREENECTAPI void freenect_get_mks_accel(freenect_raw_tilt_state *state, double* x, double* y, double* z);
430415

431416
/**
432-
* Return the resolution implied and buffer size needed for the given
433-
* pixel format and resolution enum values.
417+
* Get the number of video camera modes supported by the driver. This includes both RGB and IR modes.
434418
*
435-
* @param fmt Video format (pixel format) to return information about
436-
* @param res Resolution enum value to return information about
419+
* @return Number of video modes supported by the driver
420+
*/
421+
FREENECTAPI int freenect_get_video_mode_count();
422+
423+
/**
424+
* Get the frame descriptor of the nth supported video mode for the
425+
* video camera.
437426
*
438-
* @return Frame information structure, containing width, height, and
439-
* bytes-per-frame. If width, height, or bytes is 0, the requested
440-
* format/resolution combination is unsupported and can be considered
441-
* invalid.
427+
* @param n Which of the supported modes to return information about
428+
*
429+
* @return A freenect_frame_mode describing the nth video mode
442430
*/
443-
FREENECTAPI freenect_frame_size freenect_get_video_frame_size(freenect_video_format fmt, freenect_video_resolution res);
431+
FREENECTAPI const freenect_frame_mode freenect_get_video_mode(int mode_num);
444432

445433
/**
446-
* Convenience function to return the resolution and buffer size for
447-
* the current pixel format and resolution of a given freenect device.
434+
* Get the frame descriptor of the current video mode for the specified
435+
* freenect device.
448436
*
449-
* @param dev Device to return current frame format information about
437+
* @param dev Which device to return the currently-set video mode for
450438
*
451-
* @return Frame information structure. As in freenect_get_video_frame_size(),
452-
* if width, height, or bytes is 0, the requested format/resolution
453-
* combination is unsupported.
439+
* @return A freenect_frame_mode describing the current video mode of the specified device
454440
*/
455-
FREENECTAPI freenect_frame_size freenect_get_current_video_frame_size(freenect_device *dev);
441+
FREENECTAPI const freenect_frame_mode freenect_get_current_video_mode(freenect_device *dev);
456442

457443
/**
458-
* Set which resolution the video stream should run at for a given freenect device.
444+
* Convenience function to return a mode descriptor matching the
445+
* specified resolution and video camera pixel format, if one exists.
459446
*
460-
* @param dev Device for which to set video stream resolution
461-
* @param res Desired stream resolution
447+
* @param res Resolution desired
448+
* @param fmt Pixel format desired
462449
*
463-
* @return 0 on success, < 0 on error.
450+
* @return A freenect_frame_mode that matches the arguments specified, if such a valid mode exists; otherwise, an invalid freenect_frame_mode.
451+
*/
452+
FREENECTAPI const freenect_frame_mode freenect_find_video_mode(freenect_resolution res, freenect_video_format fmt);
453+
454+
/**
455+
* Sets the current video mode for the specified device. If the
456+
* freenect_frame_mode specified is not one provided by the driver
457+
* e.g. from freenect_get_video_mode() or freenect_find_video_mode()
458+
* then behavior is undefined. The current video mode cannot be
459+
* changed while streaming is active.
460+
*
461+
* @param dev Device for which to set the video mode
462+
* @param mode Frame mode to set
463+
*
464+
* @return 0 on success, < 0 if error
465+
*/
466+
FREENECTAPI int freenect_set_video_mode(freenect_device* dev, const freenect_frame_mode mode);
467+
468+
/**
469+
* Get the number of depth camera modes supported by the driver. This includes both RGB and IR modes.
470+
*
471+
* @return Number of depth modes supported by the driver
472+
*/
473+
FREENECTAPI int freenect_get_depth_mode_count();
474+
475+
/**
476+
* Get the frame descriptor of the nth supported depth mode for the
477+
* depth camera.
478+
*
479+
* @param n Which of the supported modes to return information about
480+
*
481+
* @return A freenect_frame_mode describing the nth depth mode
482+
*/
483+
FREENECTAPI const freenect_frame_mode freenect_get_depth_mode(int mode_num);
484+
485+
/**
486+
* Get the frame descriptor of the current depth mode for the specified
487+
* freenect device.
488+
*
489+
* @param dev Which device to return the currently-set depth mode for
490+
*
491+
* @return A freenect_frame_mode describing the current depth mode of the specified device
492+
*/
493+
FREENECTAPI const freenect_frame_mode freenect_get_current_depth_mode(freenect_device *dev);
494+
495+
/**
496+
* Convenience function to return a mode descriptor matching the
497+
* specified resolution and depth camera pixel format, if one exists.
498+
*
499+
* @param res Resolution desired
500+
* @param fmt Pixel format desired
501+
*
502+
* @return A freenect_frame_mode that matches the arguments specified, if such a valid mode exists; otherwise, an invalid freenect_frame_mode.
503+
*/
504+
FREENECTAPI const freenect_frame_mode freenect_find_depth_mode(freenect_resolution res, freenect_depth_format fmt);
505+
506+
/**
507+
* Sets the current depth mode for the specified device. The mode
508+
* cannot be changed while streaming is active.
509+
*
510+
* @param dev Device for which to set the depth mode
511+
* @param mode Frame mode to set
512+
*
513+
* @return 0 on success, < 0 if error
464514
*/
465-
FREENECTAPI int freenect_set_video_resolution(freenect_device *dev, freenect_video_resolution res);
515+
FREENECTAPI int freenect_set_depth_mode(freenect_device* dev, const freenect_frame_mode mode);
466516

467517
#ifdef __cplusplus
468518
}

0 commit comments

Comments
 (0)