Skip to content

Commit ae32bfb

Browse files
mcteomcteo
mcteo
authored andcommitted
A slightly more aggressive form of allowing resolution to be set through c_sync wrapper
Signed-off-by: Thomas Dunne <[email protected]>
1 parent a1233ca commit ae32bfb

File tree

4 files changed

+42
-35
lines changed

4 files changed

+42
-35
lines changed

examples/glpclview.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ void DrawGLScene()
117117
short *depth = 0;
118118
char *rgb = 0;
119119
uint32_t ts;
120-
if (freenect_sync_get_depth((void**)&depth, &ts, 0, FREENECT_DEPTH_11BIT) < 0)
120+
if (freenect_sync_get_depth((void**)&depth, &ts, 0, FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_11BIT) < 0)
121121
no_kinect_quit();
122-
if (freenect_sync_get_video((void**)&rgb, &ts, 0, FREENECT_VIDEO_RGB) < 0)
122+
if (freenect_sync_get_video((void**)&rgb, &ts, 0, FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_RGB) < 0)
123123
no_kinect_quit();
124124

125125
static unsigned int indices[480][640];

examples/regtest.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,31 +67,31 @@ int main(void)
6767
FILE *fp;
6868
int ret;
6969

70-
ret = freenect_sync_get_video((void**)&rgb, &ts, 0, FREENECT_VIDEO_RGB);
70+
ret = freenect_sync_get_video((void**)&rgb, &ts, 0, FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_RGB);
7171
if (ret < 0)
7272
no_kinect_quit();
7373

7474
fp = open_dump("registration_test_rgb.ppm");
7575
dump_rgb(fp, rgb, 640, 480);
7676
fclose(fp);
7777

78-
ret = freenect_sync_get_depth((void**)&depth, &ts, 0, FREENECT_DEPTH_11BIT);
78+
ret = freenect_sync_get_depth((void**)&depth, &ts, 0, FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_11BIT);
7979
if (ret < 0)
8080
no_kinect_quit();
8181

8282
fp = open_dump("registration_test_depth_raw.pgm");
8383
dump_depth(fp, depth, 640, 480);
8484
fclose(fp);
8585

86-
ret = freenect_sync_get_depth((void**)&depth, &ts, 0, FREENECT_DEPTH_REGISTERED);
86+
ret = freenect_sync_get_depth((void**)&depth, &ts, 0, FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_REGISTERED);
8787
if (ret < 0)
8888
no_kinect_quit();
8989

9090
fp = open_dump("registration_test_depth_registered.pgm");
9191
dump_depth(fp, depth, 640, 480);
9292
fclose(fp);
9393

94-
ret = freenect_sync_get_depth((void**)&depth, &ts, 0, FREENECT_DEPTH_MM);
94+
ret = freenect_sync_get_depth((void**)&depth, &ts, 0, FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_MM);
9595
if (ret < 0)
9696
no_kinect_quit();
9797

wrappers/c_sync/libfreenect_sync.c

+32-26
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ typedef struct buffer_ring {
3737
uint32_t timestamp;
3838
int valid; // True if middle buffer is valid
3939
int fmt;
40+
int res;
4041
} buffer_ring_t;
4142

4243
typedef struct sync_kinect {
@@ -56,8 +57,6 @@ static pthread_mutex_t runloop_lock = PTHREAD_MUTEX_INITIALIZER;
5657
static int pending_runloop_tasks = 0;
5758
static pthread_mutex_t pending_runloop_tasks_lock = PTHREAD_MUTEX_INITIALIZER;
5859
static pthread_cond_t pending_runloop_tasks_cond = PTHREAD_COND_INITIALIZER;
59-
static freenect_resolution m_video_resolution = FREENECT_RESOLUTION_MEDIUM;
60-
static freenect_resolution m_depth_resolution = FREENECT_RESOLUTION_MEDIUM;
6160

6261
/* Locking Convention
6362
Rules:
@@ -69,7 +68,7 @@ static freenect_resolution m_depth_resolution = FREENECT_RESOLUTION_MEDIUM;
6968
- runloop_lock, buffer_ring_t.lock (NOTE: You may only have one)
7069
*/
7170

72-
static int alloc_buffer_ring_video(freenect_video_format fmt, buffer_ring_t *buf)
71+
static int alloc_buffer_ring_video(freenect_resolution res, freenect_video_format fmt, buffer_ring_t *buf)
7372
{
7473
int sz, i;
7574
switch (fmt) {
@@ -78,7 +77,7 @@ static int alloc_buffer_ring_video(freenect_video_format fmt, buffer_ring_t *buf
7877
case FREENECT_VIDEO_IR_8BIT:
7978
case FREENECT_VIDEO_IR_10BIT:
8079
case FREENECT_VIDEO_IR_10BIT_PACKED:
81-
sz = freenect_find_video_mode(m_video_resolution, fmt).bytes;
80+
sz = freenect_find_video_mode(res, fmt).bytes;
8281
break;
8382
default:
8483
printf("Invalid video format %d\n", fmt);
@@ -89,10 +88,11 @@ static int alloc_buffer_ring_video(freenect_video_format fmt, buffer_ring_t *buf
8988
buf->timestamp = 0;
9089
buf->valid = 0;
9190
buf->fmt = fmt;
91+
buf->res = res;
9292
return 0;
9393
}
9494

95-
static int alloc_buffer_ring_depth(freenect_depth_format fmt, buffer_ring_t *buf)
95+
static int alloc_buffer_ring_depth(freenect_resolution res, freenect_depth_format fmt, buffer_ring_t *buf)
9696
{
9797
int sz, i;
9898
switch (fmt) {
@@ -102,7 +102,7 @@ static int alloc_buffer_ring_depth(freenect_depth_format fmt, buffer_ring_t *buf
102102
case FREENECT_DEPTH_10BIT_PACKED:
103103
case FREENECT_DEPTH_REGISTERED:
104104
case FREENECT_DEPTH_MM:
105-
sz = freenect_find_depth_mode(m_depth_resolution, fmt).bytes;
105+
sz = freenect_find_depth_mode(res, fmt).bytes;
106106
break;
107107
default:
108108
printf("Invalid depth format %d\n", fmt);
@@ -113,6 +113,7 @@ static int alloc_buffer_ring_depth(freenect_depth_format fmt, buffer_ring_t *buf
113113
buf->timestamp = 0;
114114
buf->valid = 0;
115115
buf->fmt = fmt;
116+
buf->res = res;
116117
return 0;
117118
}
118119

@@ -126,6 +127,7 @@ static void free_buffer_ring(buffer_ring_t *buf)
126127
buf->timestamp = 0;
127128
buf->valid = 0;
128129
buf->fmt = -1;
130+
buf->res = -1;
129131
}
130132

131133
static void producer_cb_inner(freenect_device *dev, void *data, uint32_t timestamp, buffer_ring_t *buf, set_buffer_t set_buffer)
@@ -219,29 +221,27 @@ static void init_thread(void)
219221
pthread_create(&thread, NULL, init, NULL);
220222
}
221223

222-
static int change_video_format(sync_kinect_t *kinect, freenect_video_format fmt, freenect_resolution requested_resolution)
224+
static int change_video_format(sync_kinect_t *kinect, freenect_resolution res, freenect_video_format fmt)
223225
{
224226
freenect_stop_video(kinect->dev);
225227
free_buffer_ring(&kinect->video);
226-
if (alloc_buffer_ring_video(fmt, &kinect->video))
228+
if (alloc_buffer_ring_video(res, fmt, &kinect->video))
227229
return -1;
228-
freenect_set_video_mode(kinect->dev, freenect_find_video_mode(requested_resolution, fmt));
230+
freenect_set_video_mode(kinect->dev, freenect_find_video_mode(res, fmt));
229231
freenect_set_video_buffer(kinect->dev, kinect->video.bufs[2]);
230232
freenect_start_video(kinect->dev);
231-
m_video_resolution = requested_resolution;
232233
return 0;
233234
}
234235

235-
static int change_depth_format(sync_kinect_t *kinect, freenect_depth_format fmt, freenect_resolution requested_resolution)
236+
static int change_depth_format(sync_kinect_t *kinect, freenect_resolution res, freenect_depth_format fmt)
236237
{
237238
freenect_stop_depth(kinect->dev);
238239
free_buffer_ring(&kinect->depth);
239-
if (alloc_buffer_ring_depth(fmt, &kinect->depth))
240+
if (alloc_buffer_ring_depth(res, fmt, &kinect->depth))
240241
return -1;
241-
freenect_set_depth_mode(kinect->dev, freenect_find_depth_mode(requested_resolution, fmt));
242+
freenect_set_depth_mode(kinect->dev, freenect_find_depth_mode(res, fmt));
242243
freenect_set_depth_buffer(kinect->dev, kinect->depth.bufs[2]);
243244
freenect_start_depth(kinect->dev);
244-
m_depth_resolution = requested_resolution;
245245
return 0;
246246
}
247247

@@ -258,7 +258,9 @@ static sync_kinect_t *alloc_kinect(int index)
258258
kinect->depth.bufs[i] = NULL;
259259
}
260260
kinect->video.fmt = -1;
261+
kinect->video.res = -1;
261262
kinect->depth.fmt = -1;
263+
kinect->depth.res = -1;
262264
freenect_set_video_callback(kinect->dev, video_producer_cb);
263265
freenect_set_depth_callback(kinect->dev, depth_producer_cb);
264266
pthread_mutex_init(&kinect->video.lock, NULL);
@@ -268,7 +270,7 @@ static sync_kinect_t *alloc_kinect(int index)
268270
return kinect;
269271
}
270272

271-
static int setup_kinect(int index, int fmt, int is_depth)
273+
static int setup_kinect(int index, int res, int fmt, int is_depth)
272274
{
273275
pending_runloop_tasks_inc();
274276
pthread_mutex_lock(&runloop_lock);
@@ -299,12 +301,12 @@ static int setup_kinect(int index, int fmt, int is_depth)
299301
else
300302
buf = &kinects[index]->video;
301303
pthread_mutex_lock(&buf->lock);
302-
if (buf->fmt != fmt) {
304+
if ((buf->fmt != fmt) || (buf->res != res)) {
303305
if (is_depth)
304-
change_depth_format(kinects[index], (freenect_depth_format)fmt, m_video_resolution);
306+
change_depth_format(kinects[index], (freenect_resolution)res, (freenect_depth_format)fmt);
305307
else
306-
change_video_format(kinects[index], (freenect_video_format)fmt, m_video_resolution);
307-
}
308+
change_video_format(kinects[index], (freenect_resolution)res, (freenect_video_format)fmt);
309+
}
308310
pthread_mutex_unlock(&buf->lock);
309311
pthread_mutex_unlock(&runloop_lock);
310312
pending_runloop_tasks_dec();
@@ -343,7 +345,7 @@ static int runloop_enter(int index)
343345
return -1;
344346
}
345347
if (!thread_running || !kinects[index])
346-
if (setup_kinect(index, FREENECT_DEPTH_11BIT, 1))
348+
if (setup_kinect(index, FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_11BIT, 1))
347349
return -1;
348350

349351
pending_runloop_tasks_inc();
@@ -357,27 +359,31 @@ static void runloop_exit()
357359
pending_runloop_tasks_dec();
358360
}
359361

360-
int freenect_sync_get_video(void **video, uint32_t *timestamp, int index, freenect_video_format fmt)
362+
int freenect_sync_get_video(void **video, uint32_t *timestamp, int index,
363+
freenect_resolution res, freenect_video_format fmt)
361364
{
362365
if (index < 0 || index >= MAX_KINECTS) {
363366
printf("Error: Invalid index [%d]\n", index);
364367
return -1;
365368
}
366-
if (!thread_running || !kinects[index] || kinects[index]->video.fmt != fmt)
367-
if (setup_kinect(index, fmt, 0))
369+
if (!thread_running || !kinects[index] || kinects[index]->video.fmt != fmt
370+
|| kinects[index]->video.res != res)
371+
if (setup_kinect(index, res, fmt, 0))
368372
return -1;
369373
sync_get(video, timestamp, &kinects[index]->video);
370374
return 0;
371375
}
372376

373-
int freenect_sync_get_depth(void **depth, uint32_t *timestamp, int index, freenect_depth_format fmt)
377+
int freenect_sync_get_depth(void **depth, uint32_t *timestamp, int index,
378+
freenect_resolution res, freenect_depth_format fmt)
374379
{
375380
if (index < 0 || index >= MAX_KINECTS) {
376381
printf("Error: Invalid index [%d]\n", index);
377382
return -1;
378383
}
379-
if (!thread_running || !kinects[index] || kinects[index]->depth.fmt != fmt)
380-
if (setup_kinect(index, fmt, 1))
384+
if (!thread_running || !kinects[index] || kinects[index]->depth.fmt != fmt
385+
|| kinects[index]->depth.res != res)
386+
if (setup_kinect(index, res, fmt, 1))
381387
return -1;
382388
sync_get(depth, timestamp, &kinects[index]->depth);
383389
return 0;

wrappers/c_sync/libfreenect_sync.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
extern "C" {
3434
#endif
3535

36-
int freenect_sync_get_video(void **video, uint32_t *timestamp, int index, freenect_video_format fmt);
36+
int freenect_sync_get_video(void **video, uint32_t *timestamp, int index,
37+
freenect_resolution res, freenect_video_format fmt);
3738
/* Synchronous video function, starts the runloop if it isn't running
3839
3940
The returned buffer is valid until this function is called again, after which the buffer must not
@@ -49,8 +50,8 @@ int freenect_sync_get_video(void **video, uint32_t *timestamp, int index, freene
4950
Nonzero on error.
5051
*/
5152

52-
53-
int freenect_sync_get_depth(void **depth, uint32_t *timestamp, int index, freenect_depth_format fmt);
53+
int freenect_sync_get_depth(void **depth, uint32_t *timestamp, int index,
54+
freenect_resolution res, freenect_depth_format fmt);
5455
/* Synchronous depth function, starts the runloop if it isn't running
5556
5657
The returned buffer is valid until this function is called again, after which the buffer must not

0 commit comments

Comments
 (0)