| 1 | /************************************************************************** |
| 2 | ** Title: grab one gray image using libdc1394 |
| 3 | ** $RCSfile: dvbuffer.cc,v $ |
| 4 | ** $Revision: 1.16 $$Name: $ |
| 5 | ** $Date: 2009/06/04 17:10:20 $ |
| 6 | ** Copyright: LGPL $Author: edrosten $ |
| 7 | ** Description: |
| 8 | ** |
| 9 | ** Get one gray image using libdc1394 and store it as portable gray map |
| 10 | ** (pgm). Based on 'samplegrab' from Chris Urmson |
| 11 | ** |
| 12 | **------------------------------------------------------------------------- |
| 13 | ** |
| 14 | ** $Log: dvbuffer.cc,v $ |
| 15 | ** Revision 1.16 2009/06/04 17:10:20 edrosten |
| 16 | ** Fix some warnings. |
| 17 | ** |
| 18 | ** Revision 1.15 2008/11/25 12:08:57 edrosten |
| 19 | ** Fixed mad return. |
| 20 | ** |
| 21 | ** Revision 1.14 2008/01/29 14:43:20 georgklein |
| 22 | ** Replaced some more DVBuffer constructor exit()s with exceptions |
| 23 | ** |
| 24 | ** Revision 1.13 2006/05/25 12:33:24 georgklein |
| 25 | ** Highly dubious fix without which my DVBuffer doesn't work (??) |
| 26 | ** |
| 27 | ** Revision 1.12 2006/03/31 16:58:04 edrosten |
| 28 | ** Added some proper error handling. |
| 29 | ** |
| 30 | ** Revision 1.11 2005/11/07 11:52:14 georgklein |
| 31 | ** Added auto_on_off |
| 32 | ** |
| 33 | ** Revision 1.10 2005/11/01 12:55:12 georgklein |
| 34 | ** *** empty log message *** |
| 35 | ** |
| 36 | ** Revision 1.9 2005/10/26 10:42:04 georgklein |
| 37 | ** Add sharpness control |
| 38 | ** |
| 39 | ** Revision 1.8 2005/09/18 15:46:22 edrosten |
| 40 | ** Fix for gcc40 |
| 41 | ** |
| 42 | ** Revision 1.7 2005/09/16 10:00:13 edrosten |
| 43 | ** Re-fixed instation of cam_type<...>::fps. |
| 44 | ** |
| 45 | ** Revision 1.6 2005/05/09 11:55:00 er258 |
| 46 | ** Put in correct address for the FSF |
| 47 | ** |
| 48 | ** Revision 1.5 2005/05/05 19:22:19 er258 |
| 49 | ** Added LGPL license boilerplate to the start of each source and header file. |
| 50 | ** |
| 51 | ** Revision 1.4 2005/05/05 18:48:21 er258 |
| 52 | ** Removed dependence on GPL'd kernel code. kernel-video1394.h is a rewrite |
| 53 | ** from scratch of the internal kernel headers needed for firewire digital |
| 54 | ** cameras. |
| 55 | ** |
| 56 | ** Revision 1.3 2005/04/28 18:03:53 er258 |
| 57 | ** Changed build system to use autoconf |
| 58 | ** |
| 59 | ** Revision 1.2 2005/04/12 12:50:55 er258 |
| 60 | ** All videobuffers now have a virtual frame_rate() |
| 61 | ** |
| 62 | ** Revision 1.1.1.1 2005/01/26 16:17:45 er258 |
| 63 | ** Entering in to CVS |
| 64 | ** |
| 65 | ** Revision 1.1.1.1 2005/01/25 18:59:22 er258 |
| 66 | ** Entering libCVD in to CVS |
| 67 | ** |
| 68 | ** Revision 1.3 2001/10/16 09:14:14 ronneber |
| 69 | ** - added more meaningful error message, when no raw1394 handle could be get |
| 70 | ** - does not exit anymore, when RawDCVideo has no trigger |
| 71 | ** |
| 72 | ** Revision 1.2 2001/09/14 08:10:41 ronneber |
| 73 | ** - some cosmetic changes |
| 74 | ** |
| 75 | ** Revision 1.1 2001/07/24 13:50:59 ronneber |
| 76 | ** - simple test programs to demonstrate the use of libdc1394 (based |
| 77 | ** on 'samplegrab' of Chris Urmson |
| 78 | ** |
| 79 | ** |
| 80 | **************************************************************************/ |
| 81 | |
| 82 | #include <libdc1394/dc1394_control.h> |
| 83 | #include <libraw1394/raw1394.h> |
| 84 | #include <stdio.h> |
| 85 | #include <stdlib.h> |
| 86 | |
| 87 | #include <errno.h> |
| 88 | #include <string.h> |
| 89 | |
| 90 | #include "cvd_src/Linux/kernel-video1394.h" |
| 91 | |
| 92 | #include <iostream> |
| 93 | #include <sstream> |
| 94 | #include <sys/ioctl.h> |
| 95 | #include <unistd.h> |
| 96 | |
| 97 | #include <fcntl.h> |
| 98 | #include <sys/mman.h> |
| 99 | #include <sys/stat.h> |
| 100 | #include <sys/types.h> |
| 101 | |
| 102 | #include <math.h> |
| 103 | |
| 104 | //#include <videodisplay.h> |
| 105 | #include "cvd/Linux/dvbuffer.h" |
| 106 | #include "cvd/Linux/dvframe.h" |
| 107 | #include "cvd/image.h" |
| 108 | |
| 109 | using namespace std; |
| 110 | namespace CVD |
| 111 | { |
| 112 | Exceptions::DVBuffer::DeviceOpen::DeviceOpen(string name) |
| 113 | : All("DVBuffer2 couldn't open " + name + ": " + strerror(errno)) |
| 114 | { |
| 115 | } |
| 116 | |
| 117 | Exceptions::DVBuffer::Raw1394Setup::Raw1394Setup(string action) |
| 118 | : All("DVBuffer2 (in Raw1394 setup): " + action + ": " + strerror(errno)) |
| 119 | { |
| 120 | } |
| 121 | |
| 122 | Exceptions::DVBuffer::DC1394Setup::DC1394Setup(string action) |
| 123 | : All("DVBuffer2 (in camera setup): " + action) |
| 124 | { |
| 125 | } |
| 126 | |
| 127 | Exceptions::DVBuffer::BadCameraSelection::BadCameraSelection(int nc, int cn) |
| 128 | : All([=]() { |
| 129 | ostringstream o; |
| 130 | o << "DVBuffer2: Camera number " << cn << " requested, but there are only " << nc << " cameras plugged in."; |
| 131 | return o.str(); |
| 132 | }) |
| 133 | { |
| 134 | } |
| 135 | |
| 136 | Exceptions::DVBuffer::BusReset::BusReset() |
| 137 | : All("Sorry, your RawDCVideo is the highest numbered node\n" |
| 138 | "of the bus, and has therefore become the root node.\n" |
| 139 | "The root node is responsible for maintaining \n" |
| 140 | "the timing of isochronous transactions on the IEEE \n" |
| 141 | "1394 bus. However, if the root node is not cycle master \n" |
| 142 | "capable (it doesn't have to be), then isochronous \n" |
| 143 | "transactions will not work. The host controller card is \n" |
| 144 | "cycle master capable, however, most cameras are not.\n" |
| 145 | "\n" |
| 146 | "The quick solution is to add the parameter \n" |
| 147 | "attempt_root=1 when loading the OHCI driver as a \n" |
| 148 | "module. So please do (as root):\n" |
| 149 | "\n" |
| 150 | " rmmod ohci1394\n" |
| 151 | " insmod ohci1394 attempt_root=1\n" |
| 152 | "\n" |
| 153 | "A quicker solution is to unplug the camera and plug it back in again.\n" |
| 154 | "\n") |
| 155 | { |
| 156 | } |
| 157 | |
| 158 | Exceptions::DVBuffer::DeviceSetup::DeviceSetup(string action) |
| 159 | : All("DVBuffer2 (in setup): Failed on " + action + ": " + strerror(errno)) |
| 160 | { |
| 161 | } |
| 162 | |
| 163 | namespace DC |
| 164 | { |
| 165 | const double cam_type<yuv411>::fps = 30; |
| 166 | const double cam_type<byte>::fps = 30; |
| 167 | const double cam_type<Rgb<byte>>::fps = 15; |
| 168 | } |
| 169 | |
| 170 | int get_closest_framerate(double fps, double& ret) |
| 171 | { |
| 172 | double fpss[8] = { 1.875, 1.875, 3.75, 7.5, 15, 30, 60 }; |
| 173 | int names[] = { |
| 174 | FRAMERATE_1_875, |
| 175 | FRAMERATE_1_875, |
| 176 | FRAMERATE_3_75, |
| 177 | FRAMERATE_7_5, |
| 178 | FRAMERATE_15, |
| 179 | FRAMERATE_30, |
| 180 | FRAMERATE_60, |
| 181 | }; |
| 182 | int i; |
| 183 | |
| 184 | for(i = 1; i < 7; i++) |
| 185 | if(fps < (fpss[i - 1] + fpss[i]) / 2) |
| 186 | break; |
| 187 | |
| 188 | ret = fpss[i - 1]; |
| 189 | return names[i - 1]; |
| 190 | } |
| 191 | |
| 192 | #define MAX_NUM_PORTS 8 |
| 193 | |
| 194 | /* |
| 195 | These arrays define how many image quadlets there |
| 196 | are in a packet given a mode and a frame rate |
| 197 | This is defined in the 1394 digital RawDCVideo spec |
| 198 | */ |
| 199 | const int quadlets_per_packet_format_0[42] = { |
| 200 | -1, -1, 15, 30, 60, -1, |
| 201 | -1, 20, 40, 80, 160, -1, |
| 202 | -1, 60, 120, 240, 480, -1, |
| 203 | -1, 80, 160, 320, 640, -1, |
| 204 | -1, 120, 240, 480, 960, -1, |
| 205 | -1, 40, 80, 160, 320, 640, |
| 206 | -1, 80, 160, 320, 640, -1 |
| 207 | }; |
| 208 | |
| 209 | const int quadlets_per_packet_format_1[48] = { |
| 210 | -1, 125, 250, 500, 1000, -1, |
| 211 | -1, -1, 375, 750, -1, -1, |
| 212 | -1, -1, 125, 250, 500, 1000, |
| 213 | 96, 192, 384, 768, -1, -1, |
| 214 | 144, 288, 576, -1, -1, -1, |
| 215 | 48, 96, 192, 384, 768, -1, |
| 216 | -1, 125, 250, 500, 1000, -1, |
| 217 | 96, 192, 384, 768, -1, -1 |
| 218 | }; |
| 219 | |
| 220 | const int quadlets_per_packet_format_2[48] = { |
| 221 | 160, 320, 640, -1, -1, -1, |
| 222 | 240, 480, 960, -1, -1, -1, |
| 223 | 80, 160, 320, 640, -1, -1, |
| 224 | 250, 500, 1000, -1, -1, -1, |
| 225 | 375, 750, -1, -1, -1, -1, |
| 226 | 125, 250, 500, 1000, -1, -1, |
| 227 | 160, 320, 640, -1, -1, -1, |
| 228 | 250, 500, 1000, -1, -1, -1 |
| 229 | }; |
| 230 | |
| 231 | /**********************/ |
| 232 | /* Internal functions */ |
| 233 | /**********************/ |
| 234 | |
| 235 | static int _dc1394_get_wh_from_format(int format, int mode, int* w, int* h) |
| 236 | { |
| 237 | |
| 238 | switch(format) |
| 239 | { |
| 240 | case FORMAT_VGA_NONCOMPRESSED: |
| 241 | switch(mode) |
| 242 | { |
| 243 | case MODE_160x120_YUV444: |
| 244 | *w = 160; |
| 245 | *h = 120; |
| 246 | return DC1394_SUCCESS; |
| 247 | case MODE_320x240_YUV422: |
| 248 | *w = 320; |
| 249 | *h = 240; |
| 250 | return DC1394_SUCCESS; |
| 251 | case MODE_640x480_YUV411: |
| 252 | case MODE_640x480_YUV422: |
| 253 | case MODE_640x480_RGB: |
| 254 | case MODE_640x480_MONO: |
| 255 | case MODE_640x480_MONO16: |
| 256 | *w = 640; |
| 257 | *h = 480; |
| 258 | return DC1394_SUCCESS; |
| 259 | default: |
| 260 | return DC1394_FAILURE; |
| 261 | } |
| 262 | case FORMAT_SVGA_NONCOMPRESSED_1: |
| 263 | switch(mode) |
| 264 | { |
| 265 | case MODE_800x600_YUV422: |
| 266 | case MODE_800x600_RGB: |
| 267 | case MODE_800x600_MONO: |
| 268 | case MODE_800x600_MONO16: |
| 269 | *w = 800; |
| 270 | *h = 600; |
| 271 | return DC1394_SUCCESS; |
| 272 | case MODE_1024x768_YUV422: |
| 273 | case MODE_1024x768_RGB: |
| 274 | case MODE_1024x768_MONO: |
| 275 | case MODE_1024x768_MONO16: |
| 276 | *w = 1024; |
| 277 | *h = 768; |
| 278 | return DC1394_SUCCESS; |
| 279 | default: |
| 280 | return DC1394_FAILURE; |
| 281 | } |
| 282 | case FORMAT_SVGA_NONCOMPRESSED_2: |
| 283 | switch(mode) |
| 284 | { |
| 285 | case MODE_1280x960_YUV422: |
| 286 | case MODE_1280x960_RGB: |
| 287 | case MODE_1280x960_MONO: |
| 288 | case MODE_1280x960_MONO16: |
| 289 | *w = 1280; |
| 290 | *h = 960; |
| 291 | return DC1394_SUCCESS; |
| 292 | case MODE_1600x1200_YUV422: |
| 293 | case MODE_1600x1200_RGB: |
| 294 | case MODE_1600x1200_MONO: |
| 295 | case MODE_1600x1200_MONO16: |
| 296 | *w = 1600; |
| 297 | *h = 1200; |
| 298 | return DC1394_SUCCESS; |
| 299 | default: |
| 300 | return DC1394_FAILURE; |
| 301 | } |
| 302 | default: |
| 303 | return DC1394_FAILURE; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | /********************************************************** |
| 308 | _dc1394_quadlets_from_format |
| 309 | |
| 310 | This routine reports the number of quadlets that make up a |
| 311 | frame given the format and mode |
| 312 | ***********************************************************/ |
| 313 | static int _dc1394_quadlets_from_format(int format, int mode) |
| 314 | { |
| 315 | |
| 316 | switch(format) |
| 317 | { |
| 318 | case FORMAT_VGA_NONCOMPRESSED: |
| 319 | |
| 320 | switch(mode) |
| 321 | { |
| 322 | case MODE_160x120_YUV444: |
| 323 | return 14400; //160x120*3/4 |
| 324 | case MODE_320x240_YUV422: |
| 325 | return 38400; //320x240/2 |
| 326 | case MODE_640x480_YUV411: |
| 327 | return 115200; //640x480x12/32 |
| 328 | case MODE_640x480_YUV422: |
| 329 | return 153600; //640x480/2 |
| 330 | case MODE_640x480_RGB: |
| 331 | return 230400; //640x480x3/4 |
| 332 | case MODE_640x480_MONO: |
| 333 | return 76800; //640x480/4 |
| 334 | case MODE_640x480_MONO16: |
| 335 | return 153600; //640x480/2 |
| 336 | default: |
| 337 | fprintf(stderr, "(%s) Improper mode specified: %d\n", __FILE__, mode); |
| 338 | break; |
| 339 | } |
| 340 | |
| 341 | break; |
| 342 | case FORMAT_SVGA_NONCOMPRESSED_1: |
| 343 | |
| 344 | switch(mode) |
| 345 | { |
| 346 | case MODE_800x600_YUV422: |
| 347 | return 240000; //800x600/2 |
| 348 | case MODE_800x600_RGB: |
| 349 | return 360000; //800x600x3/4 |
| 350 | case MODE_800x600_MONO: |
| 351 | return 120000; //800x600/4 |
| 352 | case MODE_1024x768_YUV422: |
| 353 | return 393216; //1024x768/2 |
| 354 | case MODE_1024x768_RGB: |
| 355 | return 589824; //1024x768x3/4 |
| 356 | case MODE_1024x768_MONO: |
| 357 | return 196608; //1024x768/4 |
| 358 | case MODE_800x600_MONO16: |
| 359 | return 240000; //800x600/2 |
| 360 | case MODE_1024x768_MONO16: |
| 361 | return 393216; //1024x768/2 |
| 362 | default: |
| 363 | fprintf(stderr, "(%s) Improper mode specified: %d\n", __FILE__, mode); |
| 364 | break; |
| 365 | } |
| 366 | |
| 367 | break; |
| 368 | case FORMAT_SVGA_NONCOMPRESSED_2: |
| 369 | |
| 370 | switch(mode) |
| 371 | { |
| 372 | case MODE_1280x960_YUV422: |
| 373 | return 614400; //1280x960/2 |
| 374 | case MODE_1280x960_RGB: |
| 375 | return 921600; //1280x960x3/4 |
| 376 | case MODE_1280x960_MONO: |
| 377 | return 307200; //1280x960/4 |
| 378 | case MODE_1600x1200_YUV422: |
| 379 | return 960000; //1600x1200/2 |
| 380 | case MODE_1600x1200_RGB: |
| 381 | return 1440000; //1600x1200x3/4 |
| 382 | case MODE_1600x1200_MONO: |
| 383 | return 480000; //1600x1200/4 |
| 384 | case MODE_1280x960_MONO16: |
| 385 | return 614400; //1280x960/2 |
| 386 | case MODE_1600x1200_MONO16: |
| 387 | return 960000; //1600x1200/2 |
| 388 | default: |
| 389 | fprintf(stderr, "(%s) Improper mode specified: %d\n", __FILE__, mode); |
| 390 | break; |
| 391 | } |
| 392 | |
| 393 | break; |
| 394 | case FORMAT_STILL_IMAGE: |
| 395 | fprintf(stderr, "(%s) Don't know how many quadlets per frame for " |
| 396 | "FORMAT_STILL_IMAGE mode:%d\n", |
| 397 | __FILE__, mode); |
| 398 | break; |
| 399 | case FORMAT_SCALABLE_IMAGE_SIZE: |
| 400 | fprintf(stderr, "(%s) Don't know how many quadlets per frame for " |
| 401 | "FORMAT_SCALABLE_IMAGE mode:%d\n", |
| 402 | __FILE__, mode); |
| 403 | break; |
| 404 | default: |
| 405 | fprintf(stderr, "(%s) Improper format specified: %d\n", __FILE__, format); |
| 406 | break; |
| 407 | } |
| 408 | |
| 409 | return -1; |
| 410 | } |
| 411 | |
| 412 | /***************************************************** |
| 413 | dc1394_dma_release_camera |
| 414 | |
| 415 | This releases memory that was mapped by |
| 416 | dc1394_dma_setup_camera |
| 417 | *****************************************************/ |
| 418 | void tom_dc1394_dma_release_camera(raw1394handle_t, const unsigned char* ring_buffer, int buffer_size, int dma_fd) |
| 419 | { |
| 420 | if(ring_buffer) |
| 421 | { |
| 422 | munmap((void*)ring_buffer, buffer_size); |
| 423 | } |
| 424 | |
| 425 | while(close(dma_fd) != 0) |
| 426 | { |
| 427 | cerr << "waiting for dma_fd to close" << endl; |
| 428 | sleep(1); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | double DC::RawDCVideo::frame_rate() |
| 433 | { |
| 434 | return true_fps; |
| 435 | } |
| 436 | |
| 437 | class handleholder |
| 438 | { |
| 439 | raw1394handle_t handle; |
| 440 | |
| 441 | public: |
| 442 | handleholder(raw1394handle_t h) |
| 443 | : handle(h) |
| 444 | { |
| 445 | } |
| 446 | |
| 447 | void clear() |
| 448 | { |
| 449 | handle = 0; |
| 450 | } |
| 451 | |
| 452 | ~handleholder() |
| 453 | { |
| 454 | if(handle) |
| 455 | raw1394_destroy_handle(handle); |
| 456 | } |
| 457 | }; |
| 458 | |
| 459 | DC::RawDCVideo::RawDCVideo(int camera_no, int num_dma_buffers, int bright, int exposure, int mode, double fps) |
| 460 | { |
| 461 | |
| 462 | int channel = camera_no; |
| 463 | int format = FORMAT_VGA_NONCOMPRESSED; |
| 464 | //int mode = MODE_640x480_MONO; |
| 465 | int speed = SPEED_400; |
| 466 | const char* dma_device_file = "/dev/video1394/0"; |
| 467 | |
| 468 | int camera_quadlets_per_frame; // construction variable |
| 469 | int numNodes; // local variable |
| 470 | int numCameras; // local variable |
| 471 | int port = 0; |
| 472 | int frame_rate; |
| 473 | |
| 474 | frame_rate = get_closest_framerate(fps, true_fps); |
| 475 | |
| 476 | my_frame_sequence.resize(num_dma_buffers); |
| 477 | for(int i = 0; i < num_dma_buffers - 1; i++) |
| 478 | { |
| 479 | my_frame_sequence[i] = i + 1; |
| 480 | } |
| 481 | my_frame_sequence[num_dma_buffers - 1] = -1; |
| 482 | |
| 483 | my_next_frame = 0; |
| 484 | my_last_in_sequence = num_dma_buffers - 1; |
| 485 | |
| 486 | /*----------------------------------------------------------------------- |
| 487 | * Open ohci and asign handle to it |
| 488 | *-----------------------------------------------------------------------*/ |
| 489 | if(!(my_handle = raw1394_new_handle())) |
| 490 | { |
| 491 | throw Exceptions::DVBuffer::Raw1394Setup("Couldn't get raw1394 handle (check for raw1394 and ohci1394 modules, and permissions on /dev/raw1394)"); |
| 492 | } |
| 493 | |
| 494 | handleholder holder(my_handle); |
| 495 | |
| 496 | if(raw1394_set_port(my_handle, port) < 0) |
| 497 | throw Exceptions::DVBuffer::Raw1394Setup("Couldn't perform raw1394_set_port"); |
| 498 | |
| 499 | // Without the following two lines dvbuffer can't set_iso_channel_and_speed |
| 500 | // on my system for some reason (GK) |
| 501 | int* strange = new int; |
| 502 | raw1394_set_userdata(my_handle, strange); |
| 503 | |
| 504 | /*----------------------------------------------------------------------- |
| 505 | * get the RawDCVideo nodes and describe them as we find them |
| 506 | *-----------------------------------------------------------------------*/ |
| 507 | numNodes = raw1394_get_nodecount(my_handle); |
| 508 | my_camera_nodes = dc1394_get_camera_nodes(my_handle, &numCameras, 1); // keep this one too |
| 509 | |
| 510 | if(numCameras <= camera_no) |
| 511 | throw Exceptions::DVBuffer::BadCameraSelection(numCameras, camera_no); |
| 512 | |
| 513 | /*----------------------------------------------------------------------- |
| 514 | * to prevent the iso-transfer bug from raw1394 system, check if |
| 515 | * RawDCVideo is highest node. |
| 516 | *-----------------------------------------------------------------------*/ |
| 517 | if(my_camera_nodes[camera_no] == numNodes - 1) |
| 518 | throw Exceptions::DVBuffer::BusReset(); |
| 519 | |
| 520 | /*----------------------------------------------------------------------- |
| 521 | * setup capture |
| 522 | *-----------------------------------------------------------------------*/ |
| 523 | |
| 524 | my_node = my_camera_nodes[camera_no]; |
| 525 | |
| 526 | if(dc1394_set_iso_channel_and_speed(my_handle, my_node, channel, speed) != DC1394_SUCCESS) |
| 527 | throw Exceptions::DVBuffer::DC1394Setup("dc1394_set_iso_channel_and_speed"); |
| 528 | |
| 529 | if(dc1394_set_video_format(my_handle, my_node, format) != DC1394_SUCCESS) |
| 530 | throw Exceptions::DVBuffer::DC1394Setup("dc1394_set_video_format failed"); |
| 531 | |
| 532 | if(dc1394_set_video_mode(my_handle, my_node, mode) != DC1394_SUCCESS) |
| 533 | throw Exceptions::DVBuffer::DC1394Setup("dc1394_set_video_mode failed"); |
| 534 | |
| 535 | if(dc1394_set_video_framerate(my_handle, my_node, frame_rate) != DC1394_SUCCESS) |
| 536 | throw Exceptions::DVBuffer::DC1394Setup("dc1394_set_video_framerate failed"); |
| 537 | |
| 538 | my_channel = channel; |
| 539 | |
| 540 | if((camera_quadlets_per_frame = _dc1394_quadlets_from_format(format, mode)) < 0) |
| 541 | throw Exceptions::DVBuffer::DC1394Setup("_dc1394_quadlets_from_format failed"); |
| 542 | |
| 543 | if(_dc1394_get_wh_from_format(format, mode, &(my_size.x), &(my_size.y)) == DC1394_FAILURE) |
| 544 | throw Exceptions::DVBuffer::DC1394Setup("_dc1394_get_wh_from_format failed"); |
| 545 | |
| 546 | // dma setup |
| 547 | struct cvd_video1394_mmap vmmap; |
| 548 | struct cvd_video1394_wait vwait; |
| 549 | |
| 550 | if((my_fd = open(dma_device_file, O_RDONLY)) < 0) |
| 551 | throw Exceptions::DVBuffer::DeviceOpen(dma_device_file); |
| 552 | |
| 553 | vmmap.syncronization_tag = 1; |
| 554 | vmmap.num_buffers = num_dma_buffers; |
| 555 | vmmap.capture_flags = SYNC_FRAMES; |
| 556 | vmmap.buffer_size = camera_quadlets_per_frame * 4; //number of bytes needed |
| 557 | vmmap.channel_number = channel; |
| 558 | |
| 559 | /* tell the video1394 system that we want to listen to the given channel */ |
| 560 | if(ioctl(my_fd, LISTEN_CHANNEL, &vmmap) < 0) |
| 561 | { |
| 562 | tom_dc1394_dma_release_camera(my_handle, my_ring_buffer, my_frame_size * my_num_buffers, my_fd); |
| 563 | throw Exceptions::DVBuffer::DeviceSetup("LISTEN_CHANNEL ioctl"); |
| 564 | } |
| 565 | |
| 566 | my_frame_size = vmmap.buffer_size; |
| 567 | my_num_buffers = vmmap.num_buffers; |
| 568 | vwait.channel_number = channel; |
| 569 | |
| 570 | /* QUEUE the buffers */ |
| 571 | for(unsigned int i = 0; i < vmmap.num_buffers; i++) |
| 572 | { |
| 573 | vwait.buffer = i; |
| 574 | |
| 575 | if(ioctl(my_fd, LISTEN_QUEUE_BUFFER, &vwait) < 0) |
| 576 | { |
| 577 | ioctl(my_fd, UNLISTEN_CHANNEL, &(vwait.channel_number)); |
| 578 | tom_dc1394_dma_release_camera(my_handle, my_ring_buffer, my_frame_size * my_num_buffers, my_fd); |
| 579 | throw Exceptions::DVBuffer::DeviceSetup("LISTEN_QUEUE_BUFFER ioctl"); |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | my_ring_buffer = (unsigned char*)mmap(0, vmmap.num_buffers * vmmap.buffer_size, |
| 584 | PROT_READ, MAP_SHARED, my_fd, 0); |
| 585 | |
| 586 | // make sure the ring buffer was allocated |
| 587 | if(my_ring_buffer == (unsigned char*)(-1)) |
| 588 | { |
| 589 | ioctl(my_fd, UNLISTEN_CHANNEL, &vmmap.channel_number); |
| 590 | tom_dc1394_dma_release_camera(my_handle, my_ring_buffer, my_frame_size * my_num_buffers, my_fd); |
| 591 | throw Exceptions::DVBuffer::DeviceSetup("mmap"); |
| 592 | } |
| 593 | |
| 594 | // camera_dma_buffer_size= vmmap.buffer_size * vmmap.num_buffers; |
| 595 | |
| 596 | // set trigger mode |
| 597 | if(dc1394_set_trigger_mode(my_handle, my_node, TRIGGER_MODE_0) |
| 598 | != DC1394_SUCCESS) |
| 599 | { |
| 600 | fprintf(stderr, "unable to set RawDCVideo trigger mode\n"); |
| 601 | } |
| 602 | |
| 603 | // auto brightness and exposure |
| 604 | if(bright == -1) |
| 605 | { |
| 606 | dc1394_auto_on_off(my_handle, my_node, FEATURE_BRIGHTNESS, 1); |
| 607 | } |
| 608 | else |
| 609 | { |
| 610 | dc1394_auto_on_off(my_handle, my_node, FEATURE_BRIGHTNESS, 0); |
| 611 | //dc1394_set_brightness(bright); |
| 612 | } |
| 613 | |
| 614 | if(exposure == -1) |
| 615 | { |
| 616 | dc1394_auto_on_off(my_handle, my_node, FEATURE_EXPOSURE, 1); |
| 617 | } |
| 618 | else |
| 619 | { |
| 620 | dc1394_auto_on_off(my_handle, my_node, FEATURE_EXPOSURE, 0); |
| 621 | //dc1394_set_exposure(exposure); |
| 622 | } |
| 623 | |
| 624 | //dc1394_auto_on_off(my_handle,my_node,FEATURE_EXPOSURE,0); |
| 625 | //dc1394_set_exposure(my_handle,my_node,200); |
| 626 | |
| 627 | /*----------------------------------------------------------------------- |
| 628 | * have the RawDCVideo start sending us data |
| 629 | *-----------------------------------------------------------------------*/ |
| 630 | |
| 631 | // this call is OK |
| 632 | if(dc1394_start_iso_transmission(my_handle, my_node) != DC1394_SUCCESS) |
| 633 | { |
| 634 | tom_dc1394_dma_release_camera(my_handle, my_ring_buffer, my_frame_size * my_num_buffers, my_fd); |
| 635 | throw Exceptions::DVBuffer::DC1394Setup("DC1394_start_iso_transmission"); |
| 636 | } |
| 637 | else |
| 638 | { |
| 639 | fprintf(stderr, "started iso transmisssion\n"); |
| 640 | } |
| 641 | |
| 642 | holder.clear(); |
| 643 | } |
| 644 | |
| 645 | VideoFrame<byte>* DC::RawDCVideo::get_frame() |
| 646 | { |
| 647 | struct cvd_video1394_wait vwait; |
| 648 | |
| 649 | vwait.channel_number = my_channel; |
| 650 | |
| 651 | // check that there actually is a free slot! |
| 652 | if(my_next_frame == -1) |
| 653 | { |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | // get the first frame |
| 658 | vwait.buffer = my_next_frame; |
| 659 | |
| 660 | if(int retval = ioctl(my_fd, LISTEN_WAIT_BUFFER, &vwait) != 0) |
| 661 | { |
| 662 | cerr << " LISTEN_WAIT_BUFFER ioctl failed with value" << retval << endl; |
| 663 | } |
| 664 | |
| 665 | // find the most recent frame |
| 666 | for(;;) |
| 667 | { |
| 668 | int next_frame = my_frame_sequence[my_next_frame]; |
| 669 | |
| 670 | // we'd better not run off the end of the sequence |
| 671 | if(next_frame == -1) |
| 672 | { |
| 673 | break; |
| 674 | } |
| 675 | |
| 676 | vwait.buffer = next_frame; |
| 677 | if(ioctl(my_fd, LISTEN_POLL_BUFFER, &vwait) != 0) |
| 678 | { |
| 679 | break; |
| 680 | } |
| 681 | |
| 682 | vwait.buffer = my_next_frame; |
| 683 | if(ioctl(my_fd, LISTEN_QUEUE_BUFFER, &vwait) < 0) |
| 684 | { |
| 685 | cerr << " LISTEN_QUEUE_BUFFER failed" << endl; |
| 686 | } |
| 687 | my_frame_sequence[my_last_in_sequence] = my_next_frame; |
| 688 | my_frame_sequence[my_next_frame] = -1; |
| 689 | my_last_in_sequence = my_next_frame; |
| 690 | my_next_frame = next_frame; |
| 691 | } |
| 692 | |
| 693 | DVFrame* frame = new DVFrame(my_size, vwait.time, my_next_frame, |
| 694 | my_ring_buffer + my_next_frame * my_frame_size); |
| 695 | |
| 696 | my_next_frame = my_frame_sequence[my_next_frame]; |
| 697 | |
| 698 | return frame; |
| 699 | } |
| 700 | |
| 701 | void DC::RawDCVideo::put_frame(VideoFrame<byte>* f) |
| 702 | { |
| 703 | DVFrame* frame = (DVFrame*)f; |
| 704 | |
| 705 | struct cvd_video1394_wait vwait; |
| 706 | |
| 707 | // requeue the last buffer |
| 708 | vwait.channel_number = my_channel; |
| 709 | vwait.buffer = frame->my_buffer; |
| 710 | if(ioctl(my_fd, LISTEN_QUEUE_BUFFER, &vwait) < 0) |
| 711 | { |
| 712 | cerr << " LISTEN_QUEUE_BUFFER failed to queue buffer " << vwait.buffer << endl; |
| 713 | } |
| 714 | my_frame_sequence[my_last_in_sequence] = frame->my_buffer; |
| 715 | my_frame_sequence[frame->my_buffer] = -1; |
| 716 | my_last_in_sequence = frame->my_buffer; |
| 717 | |
| 718 | if(my_next_frame == -1) |
| 719 | { |
| 720 | my_next_frame = my_last_in_sequence; |
| 721 | } |
| 722 | |
| 723 | delete frame; |
| 724 | } |
| 725 | |
| 726 | DC::RawDCVideo::~RawDCVideo() |
| 727 | { |
| 728 | /*----------------------------------------------------------------------- |
| 729 | * Stop data transmission |
| 730 | *-----------------------------------------------------------------------*/ |
| 731 | if(dc1394_stop_iso_transmission(my_handle, my_node) != DC1394_SUCCESS) |
| 732 | { |
| 733 | fprintf(stderr, "couldn't stop the RawDCVideo?\n"); |
| 734 | } |
| 735 | else |
| 736 | { |
| 737 | fprintf(stderr, "stopped the RawDCVideo\n"); |
| 738 | } |
| 739 | |
| 740 | /*----------------------------------------------------------------------- |
| 741 | * Close RawDCVideo |
| 742 | *-----------------------------------------------------------------------*/ |
| 743 | tom_dc1394_dma_release_camera(my_handle, my_ring_buffer, my_frame_size * my_num_buffers, my_fd); |
| 744 | dc1394_camera_off(my_handle, my_node); // GKMOD: Turn camera off |
| 745 | raw1394_destroy_handle(my_handle); |
| 746 | } |
| 747 | |
| 748 | ImageRef DC::RawDCVideo::size() { return my_size; } |
| 749 | bool DC::RawDCVideo::frame_pending() { return true; } |
| 750 | //void DC::RawDCVideo::seek_to(unsigned long long int t){} |
| 751 | |
| 752 | void DC::RawDCVideo::set_shutter(unsigned int s) |
| 753 | { |
| 754 | if(dc1394_set_shutter(my_handle, my_node, s) != DC1394_SUCCESS) |
| 755 | fprintf(stderr, "couldn't set shutter.\n"); |
| 756 | } |
| 757 | |
| 758 | unsigned int DC::RawDCVideo::get_shutter() |
| 759 | { |
| 760 | unsigned int s; |
| 761 | if(dc1394_get_shutter(my_handle, my_node, &s) != DC1394_SUCCESS) |
| 762 | fprintf(stderr, "couldn't get shutter.\n"); |
| 763 | return s; |
| 764 | } |
| 765 | |
| 766 | void DC::RawDCVideo::set_iris(unsigned int s) |
| 767 | { |
| 768 | if(dc1394_set_iris(my_handle, my_node, s) != DC1394_SUCCESS) |
| 769 | fprintf(stderr, "couldn't set iris.\n"); |
| 770 | } |
| 771 | |
| 772 | unsigned int DC::RawDCVideo::get_iris() |
| 773 | { |
| 774 | unsigned int s; |
| 775 | if(dc1394_get_iris(my_handle, my_node, &s) != DC1394_SUCCESS) |
| 776 | fprintf(stderr, "couldn't get iris.\n"); |
| 777 | return s; |
| 778 | } |
| 779 | |
| 780 | void DC::RawDCVideo::set_sharpness(unsigned int s) |
| 781 | { |
| 782 | if(dc1394_set_sharpness(my_handle, my_node, s) != DC1394_SUCCESS) |
| 783 | fprintf(stderr, "couldn't set sharpness.\n"); |
| 784 | } |
| 785 | |
| 786 | unsigned int DC::RawDCVideo::get_sharpness() |
| 787 | { |
| 788 | unsigned int s; |
| 789 | if(dc1394_get_sharpness(my_handle, my_node, &s) != DC1394_SUCCESS) |
| 790 | fprintf(stderr, "couldn't get sharpness.\n"); |
| 791 | return s; |
| 792 | } |
| 793 | |
| 794 | void DC::RawDCVideo::set_gain(unsigned int s) |
| 795 | { |
| 796 | if(dc1394_set_gain(my_handle, my_node, s) != DC1394_SUCCESS) |
| 797 | fprintf(stderr, "couldn't set gain.\n"); |
| 798 | } |
| 799 | |
| 800 | unsigned int DC::RawDCVideo::get_gain() |
| 801 | { |
| 802 | unsigned int s; |
| 803 | if(dc1394_get_gain(my_handle, my_node, &s) != DC1394_SUCCESS) |
| 804 | fprintf(stderr, "couldn't get gain.\n"); |
| 805 | return s; |
| 806 | } |
| 807 | |
| 808 | void DC::RawDCVideo::set_exposure(unsigned int s) |
| 809 | { |
| 810 | if(dc1394_set_exposure(my_handle, my_node, s) != DC1394_SUCCESS) |
| 811 | fprintf(stderr, "couldn't set exposure.\n"); |
| 812 | } |
| 813 | |
| 814 | unsigned int DC::RawDCVideo::get_exposure() |
| 815 | { |
| 816 | unsigned int s; |
| 817 | if(dc1394_get_exposure(my_handle, my_node, &s) != DC1394_SUCCESS) |
| 818 | fprintf(stderr, "couldn't get exposure.\n"); |
| 819 | return s; |
| 820 | } |
| 821 | |
| 822 | void DC::RawDCVideo::set_brightness(unsigned int s) |
| 823 | { |
| 824 | if(dc1394_set_brightness(my_handle, my_node, s) != DC1394_SUCCESS) |
| 825 | fprintf(stderr, "couldn't set brightness.\n"); |
| 826 | } |
| 827 | |
| 828 | unsigned int DC::RawDCVideo::get_brightness() |
| 829 | { |
| 830 | unsigned int s; |
| 831 | if(dc1394_get_brightness(my_handle, my_node, &s) != DC1394_SUCCESS) |
| 832 | fprintf(stderr, "couldn't get brightness.\n"); |
| 833 | return s; |
| 834 | } |
| 835 | |
| 836 | void DC::RawDCVideo::set_feature_value(unsigned int feature, unsigned int value) |
| 837 | { |
| 838 | if(dc1394_set_feature_value(my_handle, my_node, feature, value) != DC1394_SUCCESS) |
| 839 | fprintf(stderr, "couldn't set feature.\n"); |
| 840 | } |
| 841 | |
| 842 | unsigned int DC::RawDCVideo::get_feature_value(unsigned int feature) |
| 843 | { |
| 844 | unsigned int value = -1; |
| 845 | if(dc1394_get_feature_value(my_handle, my_node, feature, &value) != DC1394_SUCCESS) |
| 846 | fprintf(stderr, "couldn't get feature value.\n"); |
| 847 | return value; |
| 848 | } |
| 849 | |
| 850 | std::pair<unsigned int, unsigned int> DC::RawDCVideo::get_feature_min_max(unsigned int feature) |
| 851 | { |
| 852 | unsigned int min = 0; |
| 853 | unsigned int max = 0; |
| 854 | if(dc1394_get_min_value(my_handle, my_node, feature, &min) != DC1394_SUCCESS) |
| 855 | fprintf(stderr, "couldn't get feature min value.\n"); |
| 856 | if(dc1394_get_max_value(my_handle, my_node, feature, &max) != DC1394_SUCCESS) |
| 857 | fprintf(stderr, "couldn't get feature max value.\n"); |
| 858 | return std::pair<unsigned int, unsigned int>(min, max); |
| 859 | } |
| 860 | |
| 861 | void DC::RawDCVideo::auto_on_off(unsigned int feature, unsigned int auto_value) |
| 862 | { |
| 863 | if(dc1394_auto_on_off(my_handle, my_node, feature, auto_value) != DC1394_SUCCESS) |
| 864 | fprintf(stderr, "couldn't set auto on/off.\n"); |
| 865 | } |
| 866 | |
| 867 | raw1394handle_t& DC::RawDCVideo::handle() |
| 868 | { |
| 869 | return my_handle; |
| 870 | } |
| 871 | |
| 872 | nodeid_t& DC::RawDCVideo::node() |
| 873 | { |
| 874 | return my_node; |
| 875 | } |
| 876 | |
| 877 | } |