Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

vidl2_dc1394_istream.cxx

Go to the documentation of this file.
00001 // This is brl/bbas/vidl2/vidl2_dc1394_istream.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author Matt Leotta
00008 // \date   6 Jan 2005
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 // Anonymous namespace
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 }; // anonymous namespace
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     cur_frame_(NULL),
00113     dc1394frame_(NULL),
00114     cur_frame_valid_(false)
00115   {
00116   }
00117 
00118   unsigned int vid_index_;
00119 
00120   dc1394_t * dc1394_data_;
00121 
00122   dc1394camera_t* camera_info_;
00123 
00124   int max_speed_;
00125 
00126   bool b_mode_;
00127 
00128   vidl2_pixel_format pixel_format_;
00129 
00130   //: The last successfully decoded frame.
00131   mutable vidl2_frame_sptr cur_frame_;
00132 
00133   dc1394video_frame_t *dc1394frame_;
00134 
00135   bool cur_frame_valid_;
00136 };
00137 
00138 
00139 //--------------------------------------------------------------------------------
00140 
00141 //: Constructor
00142 vidl2_dc1394_istream::
00143 vidl2_dc1394_istream()
00144   : is_( new vidl2_dc1394_istream::pimpl )
00145 {
00146   is_->dc1394_data_ = dc1394_new();
00147 }
00148 
00149 
00150 //: Destructor
00151 vidl2_dc1394_istream::
00152 ~vidl2_dc1394_istream()
00153 {
00154   close();
00155   dc1394_free(is_->dc1394_data_);
00156   delete is_;
00157 }
00158 
00159 //: Open a new stream using a filename
00160 bool
00161 vidl2_dc1394_istream::
00162 open(unsigned int num_dma_buffers,
00163      bool drop_frames,
00164      const vidl2_iidc1394_params& params)
00165 {
00166   // Close any currently opened file
00167   close();
00168 
00169   // FIXME - where is this used in the new API?
00170   //dc1394ring_buffer_policy_t rb_policy = drop_frames? DC1394_RING_BUFFER_LAST: DC1394_RING_BUFFER_NEXT;
00171 
00172 
00173 
00174   is_->camera_info_ = dc1394_camera_new (is_->dc1394_data_, params.guid_);
00175   if(!is_->camera_info_){
00176     vcl_cerr << "Warning, failed to initialize camera with guid " << vcl_hex << params.guid_ << vcl_endl;
00177     return false;
00178   }
00179 
00180 
00181   dc1394operation_mode_t op_mode = params.b_mode_ ? DC1394_OPERATION_MODE_1394B : DC1394_OPERATION_MODE_LEGACY;
00182   if ( dc1394_video_set_operation_mode(is_->camera_info_, op_mode) != DC1394_SUCCESS){
00183     vcl_cerr << "Failed to set camera in b mode\n";
00184     close();
00185     return false;
00186   }
00187 
00188   if (dc1394_video_set_iso_speed(is_->camera_info_, dc1394speed_t(params.speed_)) != DC1394_SUCCESS) {
00189     vcl_cerr << "Failed to set iso channel and speed.\n";
00190     close();
00191     return false;
00192   }
00193 
00194   if (dc1394_video_set_mode(is_->camera_info_, dc1394video_mode_t(params.video_mode_)) != DC1394_SUCCESS) {
00195     vcl_cerr << "Failed to set video mode.\n";
00196     close();
00197     return false;
00198   }
00199 
00200   if (dc1394_video_set_framerate(is_->camera_info_, dc1394framerate_t(params.frame_rate_)) != DC1394_SUCCESS) {
00201     vcl_cerr << "Failed to set frame rate.\n";
00202     close();
00203     return false;
00204   }
00205   //else
00206   //  is_->camera_info_->framerate = dc1394framerate_t(params.frame_rate_);
00207 
00208   for (unsigned int i=0; i<params.features_.size(); ++i)
00209   {
00210     dc1394feature_info_t f = vidl2_feature_to_dc1394(params.features_[i]);
00211     // Enable/Disable a feature
00212     if ( dc1394_feature_set_power(is_->camera_info_, f.id, f.is_on?DC1394_ON:DC1394_OFF) != DC1394_SUCCESS) {
00213       vcl_cerr << "Failed to "<< (f.is_on?"enable":"disable") <<" feature \""
00214                 << vidl2_iidc1394_params::feature_string(params.features_[i].id)
00215                 << '\n';
00216       return false;
00217     }
00218 
00219     // Set the feature mode
00220     dc1394feature_mode_t old_mode;
00221     if ( dc1394_feature_get_mode(is_->camera_info_, f.id, &old_mode) == DC1394_SUCCESS &&
00222          old_mode != f.current_mode ) {
00223       if ( dc1394_feature_set_mode(is_->camera_info_, f.id, f.current_mode) != DC1394_SUCCESS) {
00224         vcl_cerr << "Failed to set mode of feature \""
00225                   << vidl2_iidc1394_params::feature_string(params.features_[i].id)
00226                   << "\" to " << vidl2_iidc1394_params::feature_mode_string(params.features_[i].active_mode) << '\n';
00227         return false;
00228       }
00229     }
00230 
00231 
00232     // Set the reature value(s)
00233     switch (params.features_[i].id)
00234     {
00235      case vidl2_iidc1394_params::FEATURE_WHITE_BALANCE:
00236       if ( dc1394_feature_whitebalance_set_value(is_->camera_info_, f.BU_value, f.RV_value) != DC1394_SUCCESS) {
00237         vcl_cerr << "Failed to set feature \"White Balance\" to "<< f.BU_value<<", "<<f.RV_value <<'\n';
00238         close();
00239         return false;
00240       }
00241       break;
00242     case vidl2_iidc1394_params::FEATURE_TEMPERATURE:
00243       if ( dc1394_feature_temperature_set_value(is_->camera_info_, f.target_value) != DC1394_SUCCESS) {
00244         vcl_cerr << "Failed to set feature \"Temperature\" to "<< f.target_value<<'\n';
00245         close();
00246         return false;
00247       }
00248       break;
00249     default:
00250       if ( f.abs_control ){
00251         if ( dc1394_feature_set_absolute_value(is_->camera_info_, f.id, f.abs_value) != DC1394_SUCCESS) {
00252           vcl_cerr << "Failed to set feature \""
00253                   << vidl2_iidc1394_params::feature_string(params.features_[i].id)
00254                   << "\" to absolute value "<< f.value <<'\n';
00255           close();
00256           return false;
00257         }
00258       }
00259       else if ( dc1394_feature_set_value(is_->camera_info_, f.id, f.value) != DC1394_SUCCESS) {
00260         vcl_cerr << "Failed to set feature \""
00261                  << vidl2_iidc1394_params::feature_string(params.features_[i].id)
00262                  << "\" to "<< f.value <<'\n';
00263         close();
00264         return false;
00265       }
00266     }
00267   }
00268 
00269 
00270   if (dc1394_capture_setup(is_->camera_info_, num_dma_buffers,
00271                            DC1394_CAPTURE_FLAGS_DEFAULT) != DC1394_SUCCESS) {
00272     vcl_cerr << "Failed to setup DMA capture.\n";
00273     return false;
00274   }
00275 
00276 
00277   is_->pixel_format_ = vidl2_iidc1394_params::pixel_format(params.video_mode_);
00278 
00279 
00280   // turn on the camera power
00281   dc1394switch_t pwr;
00282   if (dc1394_video_get_transmission(is_->camera_info_, &pwr) == DC1394_SUCCESS) {
00283     if (pwr == DC1394_ON ) {
00284       dc1394_video_set_transmission(is_->camera_info_, DC1394_OFF);
00285       vcl_cerr << "power already on\n";
00286     }
00287     if (dc1394_video_set_transmission(is_->camera_info_, DC1394_ON) == DC1394_SUCCESS) {
00288       vcl_cerr << "power turned on\n";
00289     }
00290     else {
00291       vcl_cerr << "unable to power on\n";
00292       return false;
00293     }
00294     return true;
00295   }
00296   else {
00297     vcl_cerr << "unable to start camera iso transmission\n";
00298     close();
00299     return false;
00300   }
00301 
00302   return true;
00303 }
00304 
00305 
00306 //: Close the stream
00307 void
00308 vidl2_dc1394_istream::
00309 close()
00310 {
00311   if (is_->camera_info_) {
00312     // turn off the camera power
00313     dc1394switch_t pwr;
00314     if (dc1394_video_get_transmission(is_->camera_info_, &pwr) == DC1394_SUCCESS &&
00315         pwr == DC1394_ON) {
00316       dc1394_video_set_transmission(is_->camera_info_, DC1394_OFF);
00317     }
00318 
00319     dc1394_capture_stop(is_->camera_info_);
00320     dc1394_camera_free(is_->camera_info_);
00321     is_->camera_info_ = NULL;
00322   }
00323   is_->vid_index_ = unsigned(-1);
00324 }
00325 
00326 
00327 //: Probe the bus to determine the valid parameter options
00328 bool
00329 vidl2_dc1394_istream::
00330 valid_params(vidl2_iidc1394_params::valid_options& options)
00331 {
00332   dc1394_t * d = dc1394_new();
00333   dc1394camera_list_t * list;
00334 
00335   // enumerate the cameras
00336   dc1394error_t err = dc1394_camera_enumerate(d, &list);
00337 
00338   if (err) {
00339     vcl_cerr << "error finding cameras: "
00340              << dc1394_error_get_string(err) << vcl_endl;
00341     dc1394_camera_free_list (list);
00342     dc1394_free(d);
00343     return false;
00344   }
00345 
00346   // No cameras found
00347   if (list->num == 0) {
00348     options.cameras.clear();
00349     dc1394_camera_free_list (list);
00350     dc1394_free(d);
00351     return true;
00352   }
00353 
00354   options.cameras.resize(list->num);
00355 
00356   // create a list of cameras
00357   for (unsigned int i=0; i<list->num; ++i) {
00358     dc1394camera_t *camera = dc1394_camera_new (d, list->ids[i].guid);
00359     if(!camera){
00360       vcl_cerr << "Warning, failed to initialize camera with guid " << vcl_hex << list->ids[i].guid << vcl_endl;
00361       continue;
00362     }
00363 
00364     options.cameras[i].guid = camera->guid;
00365     options.cameras[i].vendor = camera->vendor;
00366     options.cameras[i].model = camera->model;
00367 
00368     dc1394video_mode_t video_mode;
00369     if( dc1394_video_get_mode(camera,&video_mode) == DC1394_SUCCESS )
00370       options.cameras[i].curr_mode = static_cast<vidl2_iidc1394_params::video_mode_t>(video_mode);
00371 
00372     dc1394framerate_t framerate;
00373     if( dc1394_video_get_framerate(camera,&framerate) == DC1394_SUCCESS )
00374       options.cameras[i].curr_frame_rate = static_cast<vidl2_iidc1394_params::frame_rate_t>(framerate);
00375 
00376     dc1394operation_mode_t op_mode;
00377     if ( dc1394_video_get_operation_mode(camera, &op_mode) == DC1394_SUCCESS)
00378       options.cameras[i].b_mode = (op_mode == DC1394_OPERATION_MODE_1394B);
00379 
00380     dc1394speed_t iso_speed;
00381     if ( dc1394_video_get_iso_speed(camera, &iso_speed) == DC1394_SUCCESS)
00382       options.cameras[i].speed = static_cast<vidl2_iidc1394_params::speed_t>(iso_speed);
00383 
00384     dc1394video_modes_t modes;
00385     if ( dc1394_video_get_supported_modes(camera, &modes ) <0 ) {
00386       vcl_cerr << "Could not find any supported video modes\n";
00387       dc1394_camera_free_list (list);
00388       dc1394_free(d);
00389       return false;
00390     }
00391     //const vidl2_iidc1394_params::valid_options::valid_modes& m =
00392     options.cameras[i].modes.resize(modes.num);
00393     for (unsigned int j=0; j<modes.num; ++j)
00394     {
00395       options.cameras[i].modes[j].mode = (vidl2_iidc1394_params::video_mode_t) modes.modes[j];
00396       unsigned int format = vidl2_iidc1394_params::video_format_val(options.cameras[i].modes[j].mode);
00397       if (format > 5)
00398         continue;
00399       dc1394framerates_t framerates;
00400       dc1394_video_get_supported_framerates(camera, modes.modes[j], &framerates);
00401       options.cameras[i].modes[j].frame_rates.resize(framerates.num);
00402       for (unsigned int k=0; k<framerates.num; ++k) {
00403         options.cameras[i].modes[j].frame_rates[k] = (vidl2_iidc1394_params::frame_rate_t)framerates.framerates[k];
00404       }
00405     }
00406     dc1394featureset_t features;
00407     if (dc1394_feature_get_all(camera, &features) < 0){
00408       vcl_cerr << "Could not find any camera control features\n";
00409       dc1394_camera_free_list (list);
00410       dc1394_free(d);
00411       return false;
00412     }
00413     for (unsigned int k= DC1394_FEATURE_MIN, j= 0; k <= DC1394_FEATURE_MAX; k++, j++)  {
00414       const dc1394feature_info_t& f = features.feature[j];
00415       if (!f.available)
00416         continue;
00417       options.cameras[i].features.push_back(dc1394_feature_to_vidl2(f));
00418       vcl_cout << "feature: "<< dc1394_feature_get_string(f.id) << vcl_endl;
00419     }
00420     dc1394_feature_print_all(&features, stdout);
00421 
00422     dc1394_camera_free(camera);
00423   }
00424 
00425 
00426   dc1394_camera_free_list (list);
00427   dc1394_free(d);
00428 
00429   return true;
00430 }
00431 
00432 
00433 //: Return true if the stream is open for reading
00434 bool
00435 vidl2_dc1394_istream::
00436 is_open() const
00437 {
00438   return bool(is_->camera_info_);
00439 }
00440 
00441 
00442 //: Return true if the stream is in a valid state
00443 bool
00444 vidl2_dc1394_istream::
00445 is_valid() const
00446 {
00447   return is_open();
00448 }
00449 
00450 
00451 //: Return true if the stream support seeking
00452 bool
00453 vidl2_dc1394_istream::
00454 is_seekable() const
00455 {
00456   return false;
00457 }
00458 
00459 
00460 //: Return the current frame number
00461 unsigned int
00462 vidl2_dc1394_istream::
00463 frame_number() const
00464 {
00465   return is_->vid_index_;
00466 }
00467 
00468 
00469 //: Advance to the next frame (but don't acquire an image)
00470 bool
00471 vidl2_dc1394_istream::
00472 advance()
00473 {
00474   ++is_->vid_index_;
00475   is_->cur_frame_valid_ = false;
00476 
00477   if (is_->dc1394frame_)
00478     dc1394_capture_enqueue(is_->camera_info_, is_->dc1394frame_);
00479 
00480   if (dc1394_capture_dequeue(is_->camera_info_, DC1394_CAPTURE_POLICY_WAIT, &(is_->dc1394frame_))!=DC1394_SUCCESS) {
00481     vcl_cerr << "capture failed\n";
00482     return false;
00483   }
00484   return true;
00485 }
00486 
00487 
00488 //: Read the next frame from the stream
00489 vidl2_frame_sptr
00490 vidl2_dc1394_istream::read_frame()
00491 {
00492   if (advance())
00493     return current_frame();
00494   return NULL;
00495 }
00496 
00497 
00498 //: Return the current frame in the stream
00499 vidl2_frame_sptr
00500 vidl2_dc1394_istream::current_frame()
00501 {
00502   // Quick return if the stream isn't valid
00503   if ( !is_valid() ) {
00504     return NULL;
00505   }
00506 
00507 
00508   if (!is_->cur_frame_valid_) {
00509     if (is_->cur_frame_)
00510       is_->cur_frame_->invalidate();
00511 
00512     is_->cur_frame_ = new vidl2_shared_frame(is_->dc1394frame_->image,
00513                                              is_->dc1394frame_->size[0],
00514                                              is_->dc1394frame_->size[1],
00515                                              is_->pixel_format_);
00516 
00517     is_->cur_frame_valid_ = true;
00518   }
00519 
00520   return is_->cur_frame_;
00521 }
00522 
00523 
00524 //: Seek to the given frame number
00525 // \returns true if successful
00526 bool
00527 vidl2_dc1394_istream::
00528 seek_frame(unsigned int frame)
00529 {
00530   return false;
00531 }
00532 

Generated on Thu Jan 10 14:51:31 2008 for contrib/brl/bbas/vidl2 by  doxygen 1.4.4