Debian Codegraph @

cvd_src/Linux/dvbuffer3_dc1394v2.cc

raw symbols SCIP
1 // -*- c++ -*-
2
3 #include <cvd/Linux/dvbuffer3.h>
4 #include <cvd/byte.h>
5 #include <cvd/timer.h>
6
7 #include <dc1394/dc1394.h>
8 #ifndef __APPLE__
9 #include <libraw1394/raw1394.h>
10 #endif
11 #include <algorithm>
12 #include <iomanip>
13 #include <iostream>
14 #include <string>
15 #include <vector>
16
17 using namespace CVD;
18 using namespace std;
19 using namespace DV3;
20 using CVD::Exceptions::DVBuffer3::All;
21
22 namespace CVD
23 {
24
25 namespace DV3
26 {
27
28 class VPrint_
29 {
30 public:
31 VPrint_(bool _p)
32 : p(_p)
33 {
34 }
35
36 template <class C>
37 VPrint_& operator<<(const C& c)
38 {
39 if(p)
40 cerr << c;
41
42 return *this;
43 }
44
45 bool p;
46 };
47
48 class VPrint : public VPrint_
49 {
50 public:
51 VPrint(bool _b)
52 : VPrint_(_b)
53 {
54 }
55
56 template <class C>
57 VPrint_& operator<<(const C& c)
58 {
59 if(p)
60 cerr << "RawDVBuffer3: " << c;
61
62 return *this;
63 }
64 };
65
66 string coding(dc1394color_coding_t ii)
67 {
68 int i = (int)ii;
69 static vector<string> c;
70
71 if(c.empty())
72 {
73 c.push_back("MONO8");
74 c.push_back("YUV411");
75 c.push_back("YUV422");
76 c.push_back("YUV444");
77 c.push_back("RGB8");
78 c.push_back("MONO16");
79 c.push_back("RGB16");
80 c.push_back("MONO16S");
81 c.push_back("RGB16S");
82 c.push_back("RAW8");
83 c.push_back("RAW16");
84 }
85
86 i -= 352;
87 if(i < 0 || i >= (int)c.size())
88 return "error";
89 else
90 return c[i];
91 }
92
93 string filter(dc1394color_filter_t f)
94 {
95 switch(f)
96 {
97 case DC1394_COLOR_FILTER_RGGB:
98 return "RGGB";
99 case DC1394_COLOR_FILTER_BGGR:
100 return "BGGR";
101 case DC1394_COLOR_FILTER_GRBG:
102 return "GRBG";
103 case DC1394_COLOR_FILTER_GBRG:
104 return "GBRG";
105 }
106
107 return "error";
108 }
109
110 static DV3ColourFilter DV3_from_DC_ColourFilter(dc1394color_filter_t f, uint32_t /*vendor*/, uint32_t /*model*/, uint64_t guid)
111 {
112 // some cameras report incorrect bayer patterns
113 if(guid == 0x814436200006075ULL)
114 {
115 return GBRG;
116 }
117 return static_cast<DV3ColourFilter>(f - DC1394_COLOR_FILTER_MIN);
118 }
119
120 struct LibDCParams
121 {
122 dc1394_t* pDC1394;
123 dc1394camera_t* pCamera;
124 LibDCParams()
125 {
126 pDC1394 = NULL;
127 pCamera = NULL;
128 }
129 };
130
131 struct DV3Frame : public VideoFrame<byte>
132 {
133 DV3Frame(dc1394video_frame_t* pDC1394Frame)
134 : VideoFrame<byte>(const_cast<const cvd_timer&>(timer).conv_ntime(static_cast<double>(pDC1394Frame->timestamp) / 1000000.0),
135 pDC1394Frame->image,
136 ImageRef(pDC1394Frame->size[0], pDC1394Frame->size[1]))
137 {
138 mpDC1394Frame = pDC1394Frame;
139 }
140
141 protected:
142 dc1394video_frame_t* mpDC1394Frame;
143 friend class RawDVBuffer3;
144 };
145
146 static dc1394color_coding_t DC_from_DV3_ColourSpace(DV3ColourSpace s, uint32_t /*vendor*/, uint32_t /*model*/, uint64_t guid)
147 {
148 // some cameras report their raw bayer mode as being mono and do not
149 // have a mono mode at all...
150 if(guid == 0x814436200006075ULL)
151 {
152 //vendor==0x81443 model==0x0 ?
153 switch(s)
154 {
155 case MONO8:
156 return DC1394_COLOR_CODING_RAW8;
157 case MONO16:
158 return DC1394_COLOR_CODING_RAW16;
159 case RAW8:
160 return DC1394_COLOR_CODING_MONO8;
161 case RAW16:
162 return DC1394_COLOR_CODING_MONO16;
163 default:
164 break;
165 }
166 }
167 switch(s)
168 {
169 case MONO8:
170 return DC1394_COLOR_CODING_MONO8;
171 case MONO16:
172 return DC1394_COLOR_CODING_MONO16;
173 case MONO16S:
174 return DC1394_COLOR_CODING_MONO16S;
175 case RGB8:
176 return DC1394_COLOR_CODING_RGB8;
177 case RGB16:
178 return DC1394_COLOR_CODING_RGB16;
179 case RGB16S:
180 return DC1394_COLOR_CODING_RGB16S;
181 case YUV411:
182 return DC1394_COLOR_CODING_YUV411;
183 case YUV422:
184 return DC1394_COLOR_CODING_YUV422;
185 case YUV444:
186 return DC1394_COLOR_CODING_YUV444;
187 case RAW8:
188 return DC1394_COLOR_CODING_RAW8;
189 case RAW16:
190 return DC1394_COLOR_CODING_RAW16;
191 }
192 throw(All("DC_from_DV3: Invalid colourspace"));
193 }
194
195 static dc1394feature_t DC_from_DV3_Feature(DV3Feature f)
196 {
197 switch(f)
198 {
199 case BRIGHTNESS:
200 return DC1394_FEATURE_BRIGHTNESS;
201 case EXPOSURE:
202 return DC1394_FEATURE_EXPOSURE;
203 case SHARPNESS:
204 return DC1394_FEATURE_SHARPNESS;
205 case WHITE_BALANCE:
206 return DC1394_FEATURE_WHITE_BALANCE;
207 case HUE:
208 return DC1394_FEATURE_HUE;
209 case SATURATION:
210 return DC1394_FEATURE_SATURATION;
211 case GAMMA:
212 return DC1394_FEATURE_GAMMA;
213 case SHUTTER:
214 return DC1394_FEATURE_SHUTTER;
215 case GAIN:
216 return DC1394_FEATURE_GAIN;
217 case IRIS:
218 return DC1394_FEATURE_IRIS;
219 case FOCUS:
220 return DC1394_FEATURE_FOCUS;
221 case ZOOM:
222 return DC1394_FEATURE_ZOOM;
223 case PAN:
224 return DC1394_FEATURE_PAN;
225 case TILT:
226 return DC1394_FEATURE_TILT;
227 case FRAME_RATE:
228 return DC1394_FEATURE_FRAME_RATE;
229 }
230 throw(All("DC_from_DV3: Invalid feature"));
231 }
232
233 RawDVBuffer3::RawDVBuffer3(DV3ColourSpace colourspace,
234 int nCamNumber,
235 uint64_t cam_guid,
236 int cam_unit,
237 bool verbose,
238 bool bus_reset,
239 ImageRef irSize,
240 float fFrameRate,
241 ImageRef irOffset,
242 int format7_mode)
243 {
244 VPrint log(verbose);
245
246 mpLDCP = new LibDCParams;
247
248 // Create a libDC1394 context.
249 mpLDCP->pDC1394 = dc1394_new();
250
251 // A variable to record error values..
252 dc1394error_t error;
253
254 // Enumerate the cameras connected to the system.
255 dc1394camera_list_t* pCameraList = NULL;
256
257 error = dc1394_camera_enumerate(mpLDCP->pDC1394, &pCameraList);
258 if(error)
259 throw(All("Camera enumerate"));
260
261 log << "Requesting: \n";
262 log << " colourspace: " << colourspace << "\n";
263 log << " camera number: " << nCamNumber << "\n";
264 log << " camera guid: " << cam_guid << "\n";
265 log << " camera unit: " << cam_unit << "\n";
266 log << " size: " << irSize << "\n";
267 log << " framerate: " << fFrameRate << "\n";
268 log << " offset: " << irOffset << "\n";
269 log << " format7 mode: " << format7_mode << "\n";
270
271 if(pCameraList->num == 0)
272 {
273 dc1394_camera_free_list(pCameraList);
274 throw(All("No cameras found."));
275 }
276 else
277 {
278 log << "List of cameras:\n";
279 for(unsigned int i = 0; i < pCameraList->num; i++)
280 {
281 log << " Camera: " << i << ": unit=" << pCameraList->ids[i].unit << " guid=" << hex << pCameraList->ids[i].guid << dec << "\n";
282
283 if(nCamNumber == -1 && cam_guid == pCameraList->ids[i].guid)
284 {
285 if(cam_unit == -1 || cam_unit == pCameraList->ids[i].unit)
286 nCamNumber = i;
287 }
288 }
289 }
290
291 if(nCamNumber + 1 > (int)pCameraList->num)
292 {
293 dc1394_camera_free_list(pCameraList);
294 throw(All("Selected camera out of range"));
295 }
296
297 // Allocate a camera struct...
298 mpLDCP->pCamera = dc1394_camera_new(mpLDCP->pDC1394,
299 pCameraList->ids[nCamNumber].guid);
300 dc1394_camera_free_list(pCameraList);
301
302 if(!mpLDCP->pCamera)
303 throw(All("Failed on dc1394_camera_new"));
304
305 if(bus_reset)
306 {
307 dc1394switch_t is_iso_on;
308 if(dc1394_video_get_transmission(mpLDCP->pCamera, &is_iso_on) != DC1394_SUCCESS)
309 is_iso_on = DC1394_OFF;
310 if(is_iso_on == DC1394_ON)
311 {
312 dc1394_video_set_transmission(mpLDCP->pCamera, DC1394_OFF);
313 }
314 }
315
316 log << "Selected camera: " << hex << mpLDCP->pCamera->vendor_id << ":" << mpLDCP->pCamera->model_id << "(guid: " << mpLDCP->pCamera->guid << dec << ")\n";
317
318 // What mode to use?
319 dc1394color_coding_t nTargetColourCoding = DC_from_DV3_ColourSpace(colourspace, mpLDCP->pCamera->vendor_id, mpLDCP->pCamera->model_id, mpLDCP->pCamera->guid);
320
321 dc1394_camera_reset(mpLDCP->pCamera);
322
323 log << "Target colour coding: " << nTargetColourCoding << " (" << coding(nTargetColourCoding) << ")\n";
324 bool foundAStandardMode = false;
325 dc1394video_mode_t nMode;
326 mColourfilter = UNDEFINED;
327 if(irOffset.x == -1 && format7_mode == -1)
328 {
329 try
330 {
331 // First, get a list of the modes which the camera supports.
332 dc1394video_modes_t modes;
333 error = dc1394_video_get_supported_modes(mpLDCP->pCamera, &modes);
334 if(error)
335 throw(All("Could not get modelist"));
336
337 // Second: Build up a list of the modes which are the right colour-space
338 std::vector<dc1394video_mode_t> vModes;
339 for(unsigned int i = 0; i < modes.num; i++)
340 {
341 dc1394video_mode_t nMode = modes.modes[i];
342 // ignore format 7 for now
343 if(nMode >= DC1394_VIDEO_MODE_FORMAT7_0)
344 continue;
345 dc1394color_coding_t nColourCoding;
346 error = dc1394_get_color_coding_from_video_mode(mpLDCP->pCamera, nMode, &nColourCoding);
347 if(error)
348 throw(All("Error in get_color_coding_from_video_mode"));
349 if(nColourCoding == nTargetColourCoding)
350 vModes.push_back(nMode);
351 }
352 if(vModes.size() == 0)
353 throw(-1);
354
355 // Third: Select mode according to size
356 bool bModeFound = false;
357 dc1394video_mode_t nMode;
358 if(irSize.x != -1) // Has the user specified a target size? Choose mode according to that..
359 for(size_t i = 0; i < vModes.size(); i++)
360 {
361 uint32_t x, y;
362 dc1394_get_image_size_from_video_mode(mpLDCP->pCamera, vModes[i], &x, &y);
363 if(x == (uint32_t)irSize.x && y == (uint32_t)irSize.y)
364 {
365 bModeFound = true;
366 nMode = vModes[i];
367 break;
368 }
369 }
370 else // If the user didn't specify a target size, choose the one with the
371 { // highest resolution.
372 sort(vModes.begin(), vModes.end());
373 bModeFound = true;
374 nMode = vModes.back();
375 }
376 if(!bModeFound)
377 throw(-1);
378
379 // Store the size of the selected mode..
380 uint32_t x, y;
381 dc1394_get_image_size_from_video_mode(mpLDCP->pCamera, nMode, &x, &y);
382 mirSize.x = x;
383 mirSize.y = y;
384 mirOffset = irOffset;
385
386 // Got mode, now decide on frame-rate. Similar thing: first get list, then choose from list.
387 dc1394framerates_t framerates;
388 dc1394framerate_t nChosenFramerate = DC1394_FRAMERATE_MIN;
389 mdFramerate = -1.0;
390 error = dc1394_video_get_supported_framerates(mpLDCP->pCamera, nMode, &framerates);
391 if(error)
392 throw(All("Could not query supported framerates"));
393
394 if(fFrameRate > 0) // User wants a specific frame-rate?
395 {
396 for(unsigned int i = 0; i < framerates.num; i++)
397 {
398 float f_rate;
399 dc1394_framerate_as_float(framerates.framerates[i], &f_rate);
400 if(f_rate == fFrameRate)
401 {
402 nChosenFramerate = framerates.framerates[i];
403 mdFramerate = f_rate;
404 break;
405 }
406 }
407 }
408 else
409 { // Just pick the highest frame-rate the camera can do.
410 for(unsigned int i = 0; i < framerates.num; i++)
411 {
412 float f_rate;
413 dc1394_framerate_as_float(framerates.framerates[i], &f_rate);
414 if(f_rate > mdFramerate)
415 {
416 nChosenFramerate = framerates.framerates[i];
417 mdFramerate = f_rate;
418 }
419 }
420 }
421 if(mdFramerate == -1.0)
422 throw(-1);
423
424 // Selected mode and frame-rate; Now tell the camera to use these.
425 // At the moment, hard-code the channel to speed 400. This is maybe something to fix in future?
426 error = dc1394_video_set_iso_speed(mpLDCP->pCamera, DC1394_ISO_SPEED_400);
427 if(error)
428 throw(All("Could not set ISO speed."));
429
430 error = dc1394_video_set_iso_channel(mpLDCP->pCamera, nCamNumber);
431 if(error)
432 throw(All("Could not set ISO channel."));
433
434 error = dc1394_video_set_mode(mpLDCP->pCamera, nMode);
435 if(error)
436 throw(All("Could not set video mode"));
437
438 error = dc1394_video_set_framerate(mpLDCP->pCamera, nChosenFramerate);
439 if(error)
440 throw(All("Could not set frame-rate"));
441 // no need to check Format 7 modes
442 foundAStandardMode = true;
443 }
444 catch(int e)
445 {
446 //cout << "DCBuffer is checking format 7 modes as well" << endl;
447 foundAStandardMode = false;
448 }
449 }
450 if(!foundAStandardMode)
451 {
452 log << "Failed to find a standard mode. Using FORMAT_7\n";
453
454 dc1394format7modeset_t modeset;
455 error = dc1394_format7_get_modeset(mpLDCP->pCamera, &modeset);
456 if(error)
457 throw(All("Could not get Format 7 modes."));
458 int index = 0;
459 if(irOffset.x == -1)
460 mirOffset = ImageRef(0, 0);
461 else
462 mirOffset = irOffset;
463 for(; index < DC1394_VIDEO_MODE_FORMAT7_NUM; ++index)
464 {
465 const dc1394format7mode_t& mode = modeset.mode[index];
466
467 log << " FORMAT_7 mode index " << index << "\n";
468 log << " present: " << mode.present << "\n";
469 if(!mode.present)
470 continue;
471 log << " size: " << mode.size_x << "x" << mode.size_y << "\n";
472 log << " max size: " << mode.max_size_x << "x" << mode.max_size_y << "\n";
473 log << " position?: " << mode.pos_x << "x" << mode.pos_y << "\n";
474 log << " unit size: " << mode.unit_size_x << "x" << mode.unit_size_y << "\n";
475 log << " unit pos: " << mode.unit_pos_x << "x" << mode.unit_pos_y << "\n";
476 log << " color codings: " << mode.color_codings.num << "\n";
477
478 for(unsigned int i = 0; i < mode.color_codings.num && i < DC1394_COLOR_CODING_NUM; i++)
479 log << " color: " << mode.color_codings.codings[i] << "(" << coding(mode.color_codings.codings[i]) << ")\n";
480 log << " color: " << mode.color_coding << "(" << coding(mode.color_coding) << ")\n";
481
482 log << " pixnum: " << mode.pixnum << "\n";
483 log << " packet size: " << mode.packet_size << "\n";
484 log << " unit pkt size: " << mode.unit_packet_size << "\n";
485 log << " max pkt size: " << mode.max_packet_size << "\n";
486 log << " total bytes: " << mode.total_bytes << "\n";
487 log << " color filter: " << mode.color_filter << "(" << filter(mode.color_filter) << ")\n";
488
489 if(format7_mode != -1 && format7_mode != index)
490 continue;
491
492 // does the mode exist ?
493 // does it support the colour format we need ?
494 unsigned int i;
495 for(i = 0; i < mode.color_codings.num && i < DC1394_COLOR_CODING_NUM; ++i)
496 {
497 if(mode.color_codings.codings[i] == nTargetColourCoding)
498 break;
499 }
500
501 if(i == mode.color_codings.num)
502 {
503 log << " No matching mode\n";
504 continue;
505 }
506 if(irSize.x != -1)
507 {
508 // can it support the size ?
509 if((irSize.x + mirOffset.x) > (int)mode.max_size_x || (irSize.y + mirOffset.y) > (int)mode.max_size_y || irSize.x % mode.unit_size_x != 0 || irSize.y % mode.unit_size_y != 0)
510 {
511 log << " Cannot support size/offset combination\n";
512 continue;
513 }
514 }
515 else
516 {
517 irSize.x = mode.max_size_x;
518 irSize.y = mode.max_size_y;
519 }
520 // found one
521 break;
522 }
523 if(index == DC1394_VIDEO_MODE_FORMAT7_NUM)
524 throw(All("Could not find any usable format!"));
525 const dc1394format7mode_t& mode = modeset.mode[index];
526 nMode = static_cast<dc1394video_mode_t>(DC1394_VIDEO_MODE_FORMAT7_0 + index);
527
528 // At the moment, hard-code the channel to speed 400. This is maybe something to fix in future?
529 error = dc1394_video_set_iso_speed(mpLDCP->pCamera, DC1394_ISO_SPEED_400);
530 if(error)
531 throw(All("Could not set ISO speed."));
532
533 error = dc1394_video_set_iso_channel(mpLDCP->pCamera, nCamNumber);
534 if(error)
535 throw(All("Could not set ISO channel."));
536
537 error = dc1394_video_set_mode(mpLDCP->pCamera, nMode);
538 if(error)
539 throw(All("Could not set video mode."));
540
541 // frame rate calculations
542 int num_packets = (int)(8000.0 / fFrameRate + 0.5);
543 log << "Number of packets: " << num_packets << "\n";
544 int packet_size = (irSize.x * irSize.y * 8 + num_packets * 8 - 1) / (num_packets * 8);
545 mdFramerate = fFrameRate;
546 // offset calculations
547 if(irOffset.x == -1)
548 {
549 mirOffset.x = (mode.max_size_x - irSize.x) / 2;
550 mirOffset.y = (mode.max_size_y - irSize.y) / 2;
551 }
552 else
553 {
554 mirOffset = irOffset;
555 }
556 mirSize = irSize;
557 log << "Requesting:\n";
558 log << " packet size: " << packet_size << "\n";
559 log << " left: " << mirOffset.x << "\n";
560 log << " right: " << mirOffset.y << "\n";
561 log << " width: " << mirSize.x << "\n";
562 log << " height: " << mirSize.y << "\n";
563 error = dc1394_format7_set_roi(mpLDCP->pCamera, nMode,
564 nTargetColourCoding,
565 packet_size,
566 mirOffset.x, // left
567 mirOffset.y, // top
568 mirSize.x, // width
569 mirSize.y); // height
570
571 uint32_t pkt_size, offx, offy, posx, posy;
572 error = dc1394_format7_get_roi(mpLDCP->pCamera, nMode,
573 &nTargetColourCoding,
574 &pkt_size,
575 &offx, // left
576 &offy, // top
577 &posx, // width
578 &posy); // height
579 log << "Got:\n";
580 log << " packet size: " << pkt_size << "\n";
581 log << " left: " << offx << "\n";
582 log << " right: " << offy << "\n";
583 log << " width: " << posx << "\n";
584 log << " height: " << posy << "\n";
585 log << error << "\n";
586 dc1394color_filter_t filterType;
587 error = dc1394_format7_get_color_filter(mpLDCP->pCamera, nMode, &filterType);
588 mColourfilter = DV3_from_DC_ColourFilter(filterType, mpLDCP->pCamera->vendor_id, mpLDCP->pCamera->model_id, mpLDCP->pCamera->guid);
589 }
590
591 // Hack Alert: If someone requested raw bayer output but we have not
592 // yet determined the bayer filter type, then try harder to find it.
593 // This happens, if a default mode announced as MONO8 is actually a
594 // raw bayer mode instead, which is a common quirk for many cameras.
595 // We will try to query the format 7 bayer patterns, which will
596 // probably be the same as for all other modes...
597 if((mColourfilter == UNDEFINED) && ((colourspace == RAW8) || (colourspace == RAW16)))
598 {
599 dc1394format7modeset_t modeset;
600 error = dc1394_format7_get_modeset(mpLDCP->pCamera, &modeset);
601 if(error)
602 throw(All("Could not get Format 7 modes."));
603
604 for(int index = 0; index < DC1394_VIDEO_MODE_FORMAT7_NUM; ++index)
605 {
606 dc1394color_filter_t filterType;
607 dc1394video_mode_t nMode2 = static_cast<dc1394video_mode_t>(DC1394_VIDEO_MODE_FORMAT7_0 + index);
608 error = dc1394_format7_get_color_filter(mpLDCP->pCamera, nMode2, &filterType);
609 if(error)
610 continue;
611 mColourfilter = DV3_from_DC_ColourFilter(filterType, mpLDCP->pCamera->vendor_id, mpLDCP->pCamera->model_id, mpLDCP->pCamera->guid);
612
613 if(mColourfilter != UNDEFINED)
614 break;
615 }
616 }
617
618 // Hack Alert: The code below sets the iso channel without this
619 // having been properly allocated! Likewise we never allocate
620 // bandwidth. Both of these could be allocated if the following
621 // two lines were erased, and the `0' parameter to
622 // dc1394_capture_setup were changed to
623 // DC1394_CAPTURE_FLAGS_DEFAULT; but this causes problems when
624 // the program crashes, as the resources are not deallocated
625 // properly.
626 // This hack emulates what dvbuffer.cc does using libdc1394v1.
627
628 // Hack to disable resource allocation -- see above
629 // error = dc1394_capture_setup(mpLDCP->pCamera, 4, DC1394_CAPTURE_FLAGS_DEFAULT);
630 error = dc1394_capture_setup(mpLDCP->pCamera, 4, 0);
631 if(error)
632 throw(All("Could not setup capture."));
633
634 error = dc1394_video_set_transmission(mpLDCP->pCamera, DC1394_ON);
635 if(error)
636 throw(All("Could not start ISO transmission."));
637 }
638
639 RawDVBuffer3::~RawDVBuffer3()
640 {
641 if(mpLDCP->pCamera)
642 {
643 dc1394_video_set_transmission(mpLDCP->pCamera, DC1394_OFF);
644 dc1394_capture_stop(mpLDCP->pCamera);
645 dc1394_camera_set_power(mpLDCP->pCamera, DC1394_OFF);
646 dc1394_camera_free(mpLDCP->pCamera);
647 }
648
649 if(mpLDCP->pDC1394)
650 dc1394_free(mpLDCP->pDC1394);
651
652 delete mpLDCP;
653 }
654
655 #ifndef __APPLE__
656 void RawDVBuffer3::stopAllTransmissions(void)
657 {
658 raw1394handle_t rawhandle = raw1394_new_handle();
659 int numPorts = raw1394_get_port_info(rawhandle, NULL, 0);
660 for(int i = 0; i < numPorts; i++)
661 {
662 raw1394handle_t port = raw1394_new_handle_on_port(i);
663 if(port == NULL)
664 continue;
665 raw1394_iso_stop(port);
666 raw1394_iso_shutdown(port);
667 raw1394_reset_bus(port);
668 raw1394_destroy_handle(port);
669 }
670 raw1394_destroy_handle(rawhandle);
671 }
672 #endif //__APPLE__
673
674 bool RawDVBuffer3::frame_pending()
675 {
676 return true;
677 }
678
679 VideoFrame<byte>* RawDVBuffer3::get_frame()
680 {
681 dc1394error_t error;
682 dc1394video_frame_t* pDC1394Frame;
683
684 error = dc1394_capture_dequeue(mpLDCP->pCamera,
685 DC1394_CAPTURE_POLICY_WAIT,
686 &pDC1394Frame);
687
688 if(error)
689 throw(All("Failed on deque"));
690
691 DV3::DV3Frame* pDV3Frame = new DV3::DV3Frame(pDC1394Frame);
692 return pDV3Frame;
693 }
694
695 void RawDVBuffer3::put_frame(VideoFrame<byte>* pVidFrame)
696 {
697 dc1394error_t error;
698 DV3::DV3Frame* pDV3Frame = dynamic_cast<DV3::DV3Frame*>(pVidFrame);
699 if(!pVidFrame)
700 throw(All("put_frame got passed an alien frame"));
701
702 error = dc1394_capture_enqueue(mpLDCP->pCamera, pDV3Frame->mpDC1394Frame);
703
704 if(error != DC1394_SUCCESS)
705 {
706 //FIXME we really need to clean up exceptions here!
707 throw(All("failed to reenqueue frame"));
708 }
709
710 delete pDV3Frame;
711 }
712
713 unsigned int RawDVBuffer3::get_feature_value(DV3Feature nFeature)
714 {
715 if(!mpLDCP || !mpLDCP->pCamera)
716 return 0;
717 dc1394error_t error;
718 unsigned int nValue = 0;
719 error = dc1394_feature_get_value(mpLDCP->pCamera, DC_from_DV3_Feature(nFeature), &nValue);
720 if(error)
721 return 0;
722 else
723 return nValue;
724 }
725
726 void RawDVBuffer3::set_feature_value(DV3Feature nFeature, unsigned int nValue)
727 {
728 if(!mpLDCP || !mpLDCP->pCamera)
729 return;
730 dc1394_feature_set_value(mpLDCP->pCamera, DC_from_DV3_Feature(nFeature), nValue);
731 }
732
733 std::pair<unsigned int, unsigned int> RawDVBuffer3::get_feature_min_max(DV3Feature nFeature)
734 {
735 std::pair<unsigned int, unsigned int> res;
736 res.first = res.second = 0;
737 if(!mpLDCP || !mpLDCP->pCamera)
738 return res;
739 dc1394error_t error;
740 unsigned int nMin;
741 unsigned int nMax;
742 error = dc1394_feature_get_boundaries(mpLDCP->pCamera,
743 DC_from_DV3_Feature(nFeature),
744 &nMin,
745 &nMax);
746 if(error)
747 return res;
748 res.first = nMin;
749 res.second = nMax;
750 return res;
751 }
752
753 void RawDVBuffer3::auto_on_off(DV3Feature nFeature, bool bValue)
754 {
755 if(!mpLDCP || !mpLDCP->pCamera)
756 return;
757 dc1394_feature_set_mode(mpLDCP->pCamera, DC_from_DV3_Feature(nFeature),
758 bValue ? DC1394_FEATURE_MODE_AUTO : DC1394_FEATURE_MODE_MANUAL);
759 }
760
761 void RawDVBuffer3::power_on_off(DV3Feature nFeature, bool bValue)
762 {
763 if(!mpLDCP || !mpLDCP->pCamera)
764 return;
765 dc1394_feature_set_power(mpLDCP->pCamera, DC_from_DV3_Feature(nFeature),
766 bValue ? DC1394_ON : DC1394_OFF);
767 }
768 }
769
770 }