00001
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005
00006
00007
00008
00009
00010
00011
00012 #include "vidl2_dc1394_istream.h"
00013 #include "vidl2_iidc1394_params.h"
00014 #include "vidl2_pixel_format.h"
00015 #include "vidl2_frame.h"
00016
00017 #include <dc1394/control.h>
00018 #include <dc1394/utils.h>
00019
00020
00021
00022 namespace {
00023
00024 vidl2_iidc1394_params::feature_options
00025 dc1394_feature_to_vidl2(const dc1394feature_info_t& f_old)
00026 {
00027 vidl2_iidc1394_params::feature_options f;
00028 f.id = static_cast<vidl2_iidc1394_params::feature_t>(f_old.id - DC1394_FEATURE_MIN);
00029 f.active_mode = static_cast<vidl2_iidc1394_params::feature_mode_t>
00030 (f_old.current_mode - DC1394_FEATURE_MODE_MIN);
00031
00032 for(unsigned int i=0; i<f_old.modes.num; ++i){
00033 f.available_modes.push_back(static_cast<vidl2_iidc1394_params::feature_mode_t>
00034 (f_old.modes.modes[i] - DC1394_FEATURE_MODE_MIN) );
00035 }
00036
00037 f.available = f_old.available;
00038 f.absolute_capable = f_old.absolute_capable;
00039 f.readout_capable = f_old.readout_capable;
00040 f.on_off_capable = f_old.on_off_capable;
00041 f.polarity_capable = f_old.polarity_capable;
00042 f.is_on = f_old.is_on;
00043
00044 f.min = f_old.min;
00045 f.max = f_old.max;
00046 f.value = f_old.value;
00047 f.BU_value = f_old.BU_value;
00048 f.RV_value = f_old.RV_value;
00049 f.B_value = f_old.B_value;
00050 f.R_value = f_old.R_value;
00051 f.G_value = f_old.G_value;
00052 f.target_value = f_old.target_value;
00053
00054 f.abs_control = f_old.abs_control;
00055 f.abs_value = f_old.abs_value;
00056 f.abs_max = f_old.abs_max;
00057 f.abs_min = f_old.abs_min;
00058
00059 return f;
00060 }
00061
00062 dc1394feature_info_t
00063 vidl2_feature_to_dc1394(const vidl2_iidc1394_params::feature_options& f_old)
00064 {
00065 dc1394feature_info_t f;
00066 f.id = static_cast<dc1394feature_t>(f_old.id + DC1394_FEATURE_MIN);
00067 f.current_mode = static_cast<dc1394feature_mode_t>
00068 (f_old.active_mode + DC1394_FEATURE_MODE_MIN);
00069
00070 for(unsigned int i=0; i<f_old.available_modes.size(); ++i){
00071 f.modes.modes[i] = static_cast<dc1394feature_mode_t>
00072 (f_old.available_modes[i] + DC1394_FEATURE_MODE_MIN);
00073 }
00074
00075 f.available = f_old.available?DC1394_TRUE:DC1394_FALSE;
00076 f.absolute_capable = f_old.absolute_capable?DC1394_TRUE:DC1394_FALSE;
00077 f.readout_capable = f_old.readout_capable?DC1394_TRUE:DC1394_FALSE;
00078 f.on_off_capable = f_old.on_off_capable?DC1394_TRUE:DC1394_FALSE;
00079 f.polarity_capable = f_old.polarity_capable?DC1394_TRUE:DC1394_FALSE;
00080 f.is_on = f_old.is_on?DC1394_ON:DC1394_OFF;
00081
00082 f.min = f_old.min;
00083 f.max = f_old.max;
00084 f.value = f_old.value;
00085 f.BU_value = f_old.BU_value;
00086 f.RV_value = f_old.RV_value;
00087 f.B_value = f_old.B_value;
00088 f.R_value = f_old.R_value;
00089 f.G_value = f_old.G_value;
00090 f.target_value = f_old.target_value;
00091
00092 f.abs_control = f_old.abs_control?DC1394_ON:DC1394_OFF;
00093 f.abs_value = f_old.abs_value;
00094 f.abs_max = f_old.abs_max;
00095 f.abs_min = f_old.abs_min;
00096
00097 return f;
00098 }
00099
00100 };
00101
00102
00103 struct vidl2_dc1394_istream::pimpl
00104 {
00105 pimpl()
00106 : vid_index_( unsigned(-1) ),
00107 dc1394_data_(NULL),
00108 camera_info_(NULL),
00109 max_speed_(DC1394_ISO_SPEED_400),
00110 b_mode_(false),
00111 pixel_format_(VIDL2_PIXEL_FORMAT_UNKNOWN),
00112 width_(0),
00113 height_(0),
00114 framerate_(0.0),
00115 cur_frame_(NULL),
00116 dc1394frame_(NULL),
00117 cur_frame_valid_(false)
00118 {
00119 }
00120
00121 unsigned int vid_index_;
00122
00123 dc1394_t * dc1394_data_;
00124
00125 dc1394camera_t* camera_info_;
00126
00127 int max_speed_;
00128
00129 bool b_mode_;
00130
00131 vidl2_pixel_format pixel_format_;
00132
00133 unsigned int width_;
00134
00135 unsigned int height_;
00136
00137 double framerate_;
00138
00139
00140 mutable vidl2_frame_sptr cur_frame_;
00141
00142 dc1394video_frame_t *dc1394frame_;
00143
00144 bool cur_frame_valid_;
00145 };
00146
00147
00148
00149
00150
00151 vidl2_dc1394_istream::
00152 vidl2_dc1394_istream()
00153 : is_( new vidl2_dc1394_istream::pimpl )
00154 {
00155 is_->dc1394_data_ = dc1394_new();
00156 }
00157
00158
00159
00160 vidl2_dc1394_istream::
00161 ~vidl2_dc1394_istream()
00162 {
00163 close();
00164 dc1394_free(is_->dc1394_data_);
00165 delete is_;
00166 }
00167
00168
00169 bool
00170 vidl2_dc1394_istream::
00171 open(unsigned int num_dma_buffers,
00172 bool drop_frames,
00173 const vidl2_iidc1394_params& params)
00174 {
00175
00176 close();
00177
00178
00179
00180
00181
00182
00183 is_->camera_info_ = dc1394_camera_new (is_->dc1394_data_, params.guid_);
00184 if(!is_->camera_info_){
00185 vcl_cerr << "Warning, failed to initialize camera with guid " << vcl_hex << params.guid_ << vcl_endl;
00186 return false;
00187 }
00188
00189
00190 dc1394operation_mode_t op_mode = params.b_mode_ ? DC1394_OPERATION_MODE_1394B : DC1394_OPERATION_MODE_LEGACY;
00191 if ( dc1394_video_set_operation_mode(is_->camera_info_, op_mode) != DC1394_SUCCESS){
00192 vcl_cerr << "Failed to set camera in b mode\n";
00193 close();
00194 return false;
00195 }
00196
00197 if (dc1394_video_set_iso_speed(is_->camera_info_, dc1394speed_t(params.speed_)) != DC1394_SUCCESS) {
00198 vcl_cerr << "Failed to set iso channel and speed.\n";
00199 close();
00200 return false;
00201 }
00202
00203 if (dc1394_video_set_mode(is_->camera_info_, dc1394video_mode_t(params.video_mode_)) != DC1394_SUCCESS) {
00204 vcl_cerr << "Failed to set video mode.\n";
00205 close();
00206 return false;
00207 }
00208
00209 if (dc1394_video_set_framerate(is_->camera_info_, dc1394framerate_t(params.frame_rate_)) != DC1394_SUCCESS) {
00210 vcl_cerr << "Failed to set frame rate.\n";
00211 close();
00212 return false;
00213 }
00214
00215
00216
00217 for (unsigned int i=0; i<params.features_.size(); ++i)
00218 {
00219 dc1394feature_info_t f = vidl2_feature_to_dc1394(params.features_[i]);
00220
00221 if ( dc1394_feature_set_power(is_->camera_info_, f.id, f.is_on?DC1394_ON:DC1394_OFF) != DC1394_SUCCESS) {
00222 vcl_cerr << "Failed to "<< (f.is_on?"enable":"disable") <<" feature \""
00223 << vidl2_iidc1394_params::feature_string(params.features_[i].id)
00224 << '\n';
00225 return false;
00226 }
00227
00228
00229 dc1394feature_mode_t old_mode;
00230 if ( dc1394_feature_get_mode(is_->camera_info_, f.id, &old_mode) == DC1394_SUCCESS &&
00231 old_mode != f.current_mode ) {
00232 if ( dc1394_feature_set_mode(is_->camera_info_, f.id, f.current_mode) != DC1394_SUCCESS) {
00233 vcl_cerr << "Failed to set mode of feature \""
00234 << vidl2_iidc1394_params::feature_string(params.features_[i].id)
00235 << "\" to " << vidl2_iidc1394_params::feature_mode_string(params.features_[i].active_mode) << '\n';
00236 return false;
00237 }
00238 }
00239
00240
00241
00242 switch (params.features_[i].id)
00243 {
00244 case vidl2_iidc1394_params::FEATURE_WHITE_BALANCE:
00245 if ( dc1394_feature_whitebalance_set_value(is_->camera_info_, f.BU_value, f.RV_value) != DC1394_SUCCESS) {
00246 vcl_cerr << "Failed to set feature \"White Balance\" to "<< f.BU_value<<", "<<f.RV_value <<'\n';
00247 close();
00248 return false;
00249 }
00250 break;
00251 case vidl2_iidc1394_params::FEATURE_TEMPERATURE:
00252 if ( dc1394_feature_temperature_set_value(is_->camera_info_, f.target_value) != DC1394_SUCCESS) {
00253 vcl_cerr << "Failed to set feature \"Temperature\" to "<< f.target_value<<'\n';
00254 close();
00255 return false;
00256 }
00257 break;
00258 default:
00259 if ( f.abs_control ){
00260 if ( dc1394_feature_set_absolute_value(is_->camera_info_, f.id, f.abs_value) != DC1394_SUCCESS) {
00261 vcl_cerr << "Failed to set feature \""
00262 << vidl2_iidc1394_params::feature_string(params.features_[i].id)
00263 << "\" to absolute value "<< f.value <<'\n';
00264 close();
00265 return false;
00266 }
00267 }
00268 else if ( dc1394_feature_set_value(is_->camera_info_, f.id, f.value) != DC1394_SUCCESS) {
00269 vcl_cerr << "Failed to set feature \""
00270 << vidl2_iidc1394_params::feature_string(params.features_[i].id)
00271 << "\" to "<< f.value <<'\n';
00272 close();
00273 return false;
00274 }
00275 }
00276 }
00277
00278
00279 if (dc1394_capture_setup(is_->camera_info_, num_dma_buffers,
00280 DC1394_CAPTURE_FLAGS_DEFAULT) != DC1394_SUCCESS) {
00281 vcl_cerr << "Failed to setup DMA capture.\n";
00282 return false;
00283 }
00284
00285
00286 is_->pixel_format_ = vidl2_iidc1394_params::pixel_format(params.video_mode_);
00287 vidl2_iidc1394_params::resolution(params.video_mode_,is_->width_,is_->height_);
00288 is_->framerate_ = vidl2_iidc1394_params::frame_rate_val(params.frame_rate_);
00289
00290
00291 dc1394switch_t pwr;
00292 if (dc1394_video_get_transmission(is_->camera_info_, &pwr) == DC1394_SUCCESS) {
00293 if (pwr == DC1394_ON ) {
00294 dc1394_video_set_transmission(is_->camera_info_, DC1394_OFF);
00295 vcl_cerr << "power already on\n";
00296 }
00297 if (dc1394_video_set_transmission(is_->camera_info_, DC1394_ON) == DC1394_SUCCESS) {
00298 vcl_cerr << "power turned on\n";
00299 }
00300 else {
00301 vcl_cerr << "unable to power on\n";
00302 return false;
00303 }
00304 return true;
00305 }
00306 else {
00307 vcl_cerr << "unable to start camera iso transmission\n";
00308 close();
00309 return false;
00310 }
00311
00312 return true;
00313 }
00314
00315
00316
00317 void
00318 vidl2_dc1394_istream::
00319 close()
00320 {
00321 if (is_->camera_info_) {
00322
00323 dc1394switch_t pwr;
00324 if (dc1394_video_get_transmission(is_->camera_info_, &pwr) == DC1394_SUCCESS &&
00325 pwr == DC1394_ON) {
00326 dc1394_video_set_transmission(is_->camera_info_, DC1394_OFF);
00327 }
00328
00329 dc1394_capture_stop(is_->camera_info_);
00330 dc1394_camera_free(is_->camera_info_);
00331 is_->camera_info_ = NULL;
00332 }
00333 is_->vid_index_ = unsigned(-1);
00334 is_->pixel_format_ = VIDL2_PIXEL_FORMAT_UNKNOWN;
00335 is_->width_ = 0;
00336 is_->height_ = 0;
00337 is_->framerate_ = 0.0;
00338 }
00339
00340
00341
00342 bool
00343 vidl2_dc1394_istream::
00344 valid_params(vidl2_iidc1394_params::valid_options& options)
00345 {
00346 dc1394_t * d = dc1394_new();
00347 dc1394camera_list_t * list;
00348
00349
00350 dc1394error_t err = dc1394_camera_enumerate(d, &list);
00351
00352 if (err) {
00353 vcl_cerr << "error finding cameras: "
00354 << dc1394_error_get_string(err) << vcl_endl;
00355 dc1394_camera_free_list (list);
00356 dc1394_free(d);
00357 return false;
00358 }
00359
00360
00361 if (list->num == 0) {
00362 options.cameras.clear();
00363 dc1394_camera_free_list (list);
00364 dc1394_free(d);
00365 return true;
00366 }
00367
00368 options.cameras.resize(list->num);
00369
00370
00371 for (unsigned int i=0; i<list->num; ++i) {
00372 dc1394camera_t *camera = dc1394_camera_new (d, list->ids[i].guid);
00373 if(!camera){
00374 vcl_cerr << "Warning, failed to initialize camera with guid " << vcl_hex << list->ids[i].guid << vcl_endl;
00375 continue;
00376 }
00377
00378 options.cameras[i].guid = camera->guid;
00379 options.cameras[i].vendor = camera->vendor;
00380 options.cameras[i].model = camera->model;
00381
00382 dc1394video_mode_t video_mode;
00383 if( dc1394_video_get_mode(camera,&video_mode) == DC1394_SUCCESS )
00384 options.cameras[i].curr_mode = static_cast<vidl2_iidc1394_params::video_mode_t>(video_mode);
00385
00386 dc1394framerate_t framerate;
00387 if( dc1394_video_get_framerate(camera,&framerate) == DC1394_SUCCESS )
00388 options.cameras[i].curr_frame_rate = static_cast<vidl2_iidc1394_params::frame_rate_t>(framerate);
00389
00390 dc1394operation_mode_t op_mode;
00391 if ( dc1394_video_get_operation_mode(camera, &op_mode) == DC1394_SUCCESS)
00392 options.cameras[i].b_mode = (op_mode == DC1394_OPERATION_MODE_1394B);
00393
00394 dc1394speed_t iso_speed;
00395 if ( dc1394_video_get_iso_speed(camera, &iso_speed) == DC1394_SUCCESS)
00396 options.cameras[i].speed = static_cast<vidl2_iidc1394_params::speed_t>(iso_speed);
00397
00398 dc1394video_modes_t modes;
00399 if ( dc1394_video_get_supported_modes(camera, &modes ) <0 ) {
00400 vcl_cerr << "Could not find any supported video modes\n";
00401 dc1394_camera_free_list (list);
00402 dc1394_free(d);
00403 return false;
00404 }
00405
00406 options.cameras[i].modes.resize(modes.num);
00407 for (unsigned int j=0; j<modes.num; ++j)
00408 {
00409 options.cameras[i].modes[j].mode = (vidl2_iidc1394_params::video_mode_t) modes.modes[j];
00410 unsigned int format = vidl2_iidc1394_params::video_format_val(options.cameras[i].modes[j].mode);
00411 if (format > 5)
00412 continue;
00413 dc1394framerates_t framerates;
00414 dc1394_video_get_supported_framerates(camera, modes.modes[j], &framerates);
00415 options.cameras[i].modes[j].frame_rates.resize(framerates.num);
00416 for (unsigned int k=0; k<framerates.num; ++k) {
00417 options.cameras[i].modes[j].frame_rates[k] = (vidl2_iidc1394_params::frame_rate_t)framerates.framerates[k];
00418 }
00419 }
00420 dc1394featureset_t features;
00421 if (dc1394_feature_get_all(camera, &features) < 0){
00422 vcl_cerr << "Could not find any camera control features\n";
00423 dc1394_camera_free_list (list);
00424 dc1394_free(d);
00425 return false;
00426 }
00427 for (unsigned int k= DC1394_FEATURE_MIN, j= 0; k <= DC1394_FEATURE_MAX; k++, j++) {
00428 const dc1394feature_info_t& f = features.feature[j];
00429 if (!f.available)
00430 continue;
00431 options.cameras[i].features.push_back(dc1394_feature_to_vidl2(f));
00432 vcl_cout << "feature: "<< dc1394_feature_get_string(f.id) << vcl_endl;
00433 }
00434 dc1394_feature_print_all(&features, stdout);
00435
00436 dc1394_camera_free(camera);
00437 }
00438
00439
00440 dc1394_camera_free_list (list);
00441 dc1394_free(d);
00442
00443 return true;
00444 }
00445
00446
00447
00448 bool
00449 vidl2_dc1394_istream::
00450 is_open() const
00451 {
00452 return bool(is_->camera_info_);
00453 }
00454
00455
00456
00457 bool
00458 vidl2_dc1394_istream::
00459 is_valid() const
00460 {
00461 return is_open() && bool(is_->dc1394frame_);
00462 }
00463
00464
00465
00466 bool
00467 vidl2_dc1394_istream::
00468 is_seekable() const
00469 {
00470 return false;
00471 }
00472
00473
00474
00475
00476 int
00477 vidl2_dc1394_istream::
00478 num_frames() const
00479 {
00480 return -1;
00481 }
00482
00483
00484
00485 unsigned int
00486 vidl2_dc1394_istream::
00487 frame_number() const
00488 {
00489 return is_->vid_index_;
00490 }
00491
00492
00493
00494 unsigned int
00495 vidl2_dc1394_istream::
00496 width() const
00497 {
00498 return is_->width_;
00499 }
00500
00501
00502
00503 unsigned int
00504 vidl2_dc1394_istream::
00505 height() const
00506 {
00507 return is_->height_;
00508 }
00509
00510
00511
00512 vidl2_pixel_format
00513 vidl2_dc1394_istream::
00514 format() const
00515 {
00516 return is_->pixel_format_;
00517 }
00518
00519
00520
00521 double
00522 vidl2_dc1394_istream::
00523 frame_rate() const
00524 {
00525 return is_->framerate_;
00526 }
00527
00528
00529
00530 bool
00531 vidl2_dc1394_istream::
00532 advance()
00533 {
00534 ++is_->vid_index_;
00535 is_->cur_frame_valid_ = false;
00536
00537 if (is_->dc1394frame_)
00538 dc1394_capture_enqueue(is_->camera_info_, is_->dc1394frame_);
00539
00540 if (dc1394_capture_dequeue(is_->camera_info_, DC1394_CAPTURE_POLICY_WAIT, &(is_->dc1394frame_))!=DC1394_SUCCESS) {
00541 vcl_cerr << "capture failed\n";
00542 return false;
00543 }
00544 return true;
00545 }
00546
00547
00548
00549 vidl2_frame_sptr
00550 vidl2_dc1394_istream::read_frame()
00551 {
00552 if (advance())
00553 return current_frame();
00554 return NULL;
00555 }
00556
00557
00558
00559 vidl2_frame_sptr
00560 vidl2_dc1394_istream::current_frame()
00561 {
00562
00563 if ( !is_valid() ) {
00564 return NULL;
00565 }
00566
00567
00568 if (!is_->cur_frame_valid_) {
00569 if (is_->cur_frame_)
00570 is_->cur_frame_->invalidate();
00571
00572 is_->cur_frame_ = new vidl2_shared_frame(is_->dc1394frame_->image,
00573 is_->dc1394frame_->size[0],
00574 is_->dc1394frame_->size[1],
00575 is_->pixel_format_);
00576
00577 is_->cur_frame_valid_ = true;
00578 }
00579
00580 return is_->cur_frame_;
00581 }
00582
00583
00584
00585
00586 bool
00587 vidl2_dc1394_istream::
00588 seek_frame(unsigned int frame)
00589 {
00590 return false;
00591 }
00592