bool Libdc1394SequenceGrabber::initFrameRate(unsigned int rate) { msg(osg::INFO) << "initFrameRate" << std::endl; if (!_camera) return false; if (_format7) { uint32_t bit_size; dc1394_get_color_coding_bit_size(_sourceFormat,&bit_size); int packet_size = DC1394_USE_MAX_AVAIL; if(rate != 0) { double bus_period; if(_speed == DC1394_ISO_SPEED_800) { bus_period = 0.0000625; } else { bus_period = 0.000125; } int num_packets = (int)(1.0/(bus_period*rate)+0.5); packet_size = ((_roi.width - _roi.x)*(_roi.height - _roi.y)*bit_size + (num_packets*8) - 1) / (num_packets*8); } dc1394error_t err = dc1394_format7_set_packet_size(_camera, _videomode, packet_size); checkSuccess(err, "dc1394_format7_set_packet_size failed"); err = dc1394_format7_set_roi(_camera, _videomode, _sourceFormat, packet_size, _roi.x,_roi.y,_roi.width,_roi.height); checkSuccess(err, "dc1394_format7_set_roi failed"); return (err == DC1394_SUCCESS); } dc1394framerates_t framerates; dc1394error_t err=dc1394_video_get_supported_framerates(_camera,_videomode, &framerates); checkSuccess(err, "dc1394_video_get_supported_framerates failed"); dc1394framerate_t framerate=framerates.framerates[framerates.num-1]; switch (rate) { case 15: framerate = DC1394_FRAMERATE_15; break; case 30: framerate = DC1394_FRAMERATE_30; break; case 60: framerate = DC1394_FRAMERATE_60; break; case 120: framerate = DC1394_FRAMERATE_120; break; case 240: framerate = DC1394_FRAMERATE_240; } err=dc1394_video_set_framerate(_camera, framerate); checkSuccess(err, "dc1394_video_set_framerate failed"); return (err == DC1394_SUCCESS); }
//-------------------------------------------------------------------- bool ofxVideoGrabberPtgrey::initGrabber(int w, int h, bool setUseTexture){ width = w; height = h; bUseTexture = setUseTexture; // In format_7 the framerate is set by setting the packet_size // this is used in dc1394_format7_set_roi() // The following formula is from the libdc1394 faq // http://damien.douxchamps.net/ieee1394/libdc1394/v2.x/faq/#How_can_I_work_out_the_packet_size_for_a_wanted_frame_rate float bus_period = 0.000125; // for firewire 400 int frame_rate = 60; int depth = 3; int num_packets = (int)(1.0/(bus_period*frame_rate) + 0.5); //packet_size = (width*height*depth + (num_packets*8) - 1)/(num_packets*8); packet_size = DC1394_USE_MAX_AVAIL; //first choose a device to use if( bChooseDevice ) { bool bFound = false; for (uint32_t index = 0; index < deviceList->num; index++) { if (deviceList->ids[index].unit == deviceID) { bFound = true; deviceID = deviceList->ids[index].guid; } } if (!bFound) { printf("initGrabber warning: Device ID for unit %x not found, using first device\n", deviceID); deviceID = deviceList->ids[0].guid; } } else if(deviceList->num > 0) { deviceID = deviceList->ids[0].guid; } else { ofLog(OF_LOG_ERROR, "in initGrabber, No cameras found"); } camera = dc1394_camera_new(driver, deviceID); if (!camera) { ofLog(OF_LOG_ERROR, "Failed to initialize camera with guid %x", deviceID); return 1; } /*----------------------------------------------------------------------- * setup capture *-----------------------------------------------------------------------*/ err = dc1394_video_set_iso_speed(camera, DC1394_ISO_SPEED_400); if( err != DC1394_SUCCESS ) { ofLog(OF_LOG_ERROR, "failed to set iso speed"); } err = dc1394_video_set_mode( camera, video_mode ); if( err != DC1394_SUCCESS ) { ofLog(OF_LOG_ERROR, "failed to set format 7 video mode"); } err = dc1394_format7_set_color_coding(camera, video_mode, color_coding); if( err != DC1394_SUCCESS ) { ofLog(OF_LOG_ERROR, "failed to set format 7 color coding"); } err = dc1394_format7_set_packet_size(camera, video_mode, packet_size); if( err != DC1394_SUCCESS ) { ofLog(OF_LOG_ERROR, "failed to set format 7 packet_size"); } err = dc1394_format7_set_roi(camera, video_mode, color_coding, packet_size, 0,0, width,height); if( err != DC1394_SUCCESS ) { ofLog(OF_LOG_ERROR, "failed to set format 7"); } //err = dc1394_video_set_framerate(camera, framerate); //DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not set framerate"); err = dc1394_capture_setup(camera,2, DC1394_CAPTURE_FLAGS_DEFAULT); if( err != DC1394_SUCCESS ) { ofLog(OF_LOG_ERROR, "failed to setup dma capturing"); } /*----------------------------------------------------------------------- * set camera's features *-----------------------------------------------------------------------*/ err = dc1394_feature_set_mode(camera, DC1394_FEATURE_BRIGHTNESS, DC1394_FEATURE_MODE_MANUAL); err = dc1394_feature_set_value(camera, DC1394_FEATURE_BRIGHTNESS, 200); //1-255 err = dc1394_feature_set_mode(camera, DC1394_FEATURE_EXPOSURE, DC1394_FEATURE_MODE_MANUAL); err = dc1394_feature_set_value(camera, DC1394_FEATURE_EXPOSURE, 62); //7-62 err = dc1394_feature_set_mode(camera, DC1394_FEATURE_SHUTTER, DC1394_FEATURE_MODE_MANUAL); err = dc1394_feature_set_value(camera, DC1394_FEATURE_SHUTTER, 60); //1-263, 64 being the max for 60fps err = dc1394_feature_set_mode(camera, DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_MANUAL); err = dc1394_feature_set_value(camera, DC1394_FEATURE_GAIN, 50); //16-64 //err = dc1394_feature_set_mode(camera, DC1394_FEATURE_PAN, DC1394_FEATURE_MODE_MANUAL); //err = dc1394_feature_set_value(camera, DC1394_FEATURE_PAN, 56); /*----------------------------------------------------------------------- * report camera's features *-----------------------------------------------------------------------*/ err=dc1394_feature_get_all(camera,&features); if (err!=DC1394_SUCCESS) { dc1394_log_warning("Could not get feature set"); } else { dc1394_feature_print_all(&features, stdout); } /*----------------------------------------------------------------------- * have the camera start sending us data *-----------------------------------------------------------------------*/ err=dc1394_video_set_transmission(camera, DC1394_ON); //DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not start camera iso transmission"); //create pixel buffer pixels = new unsigned char[width * height * 3]; //create texture if (bUseTexture) { tex.allocate(width,height,GL_LUMINANCE); memset(pixels, 0, width*height); tex.loadData(pixels, width, height, GL_LUMINANCE); } }
// Note: // the following was tested on a IIDC camera over USB therefore might not work as // well on a camera over proper firewire transport void FirewireVideo::init_format7_camera( uint64_t guid, int dma_frames, dc1394speed_t iso_speed, dc1394video_mode_t video_mode, float framerate, uint32_t width, uint32_t height, uint32_t left, uint32_t top, bool reset_at_boot ) { if(video_mode< DC1394_VIDEO_MODE_FORMAT7_0) throw VideoException("roi can be specified only for format7 modes"); camera = dc1394_camera_new (d, guid); if (!camera) throw VideoException("Failed to initialize camera"); // Attempt to stop camera if it is already running dc1394switch_t is_iso_on = DC1394_OFF; dc1394_video_get_transmission(camera, &is_iso_on); if (is_iso_on==DC1394_ON) { dc1394_video_set_transmission(camera, DC1394_OFF); } cout << "Using camera with GUID " << camera->guid << endl; if(reset_at_boot){ dc1394_camera_reset(camera); } //----------------------------------------------------------------------- // setup mode and roi //----------------------------------------------------------------------- if(iso_speed >= DC1394_ISO_SPEED_800) { err=dc1394_video_set_operation_mode(camera, DC1394_OPERATION_MODE_1394B); if( err != DC1394_SUCCESS ) throw VideoException("Could not set DC1394_OPERATION_MODE_1394B"); } err=dc1394_video_set_iso_speed(camera, iso_speed); if( err != DC1394_SUCCESS ) throw VideoException("Could not set iso speed"); // check that the required mode is actually supported dc1394format7mode_t format7_info; err = dc1394_format7_get_mode_info(camera, video_mode, &format7_info); if( err != DC1394_SUCCESS ) throw VideoException("Could not get format7 mode info"); // safely set the video mode err=dc1394_video_set_mode(camera, video_mode); if( err != DC1394_SUCCESS ) throw VideoException("Could not set format7 video mode"); // set position to 0,0 so that setting any size within min and max is a valid command err = dc1394_format7_set_image_position(camera, video_mode,0,0); if( err != DC1394_SUCCESS ) throw VideoException("Could not set format7 image position"); // work out the desired image size width = nearest_value(width, format7_info.unit_pos_x, 0, format7_info.max_size_x - left); height = nearest_value(height, format7_info.unit_pos_y, 0, format7_info.max_size_y - top); // set size err = dc1394_format7_set_image_size(camera,video_mode,width,height); if( err != DC1394_SUCCESS ) throw VideoException("Could not set format7 size"); // get the info again since many parameters depend on image size err = dc1394_format7_get_mode_info(camera, video_mode, &format7_info); if( err != DC1394_SUCCESS ) throw VideoException("Could not get format7 mode info"); // work out position of roi left = nearest_value(left, format7_info.unit_size_x, format7_info.unit_size_x, format7_info.max_size_x - width); top = nearest_value(top, format7_info.unit_size_y, format7_info.unit_size_y, format7_info.max_size_y - height); // set roi position err = dc1394_format7_set_image_position(camera,video_mode,left,top); if( err != DC1394_SUCCESS ) throw VideoException("Could not set format7 size"); this->width = width; this->height = height; this->top = top; this->left = left; cout<<"roi: "<<left<<" "<<top<<" "<<width<<" "<<height<<" "; //----------------------------------------------------------------------- // setup frame rate //----------------------------------------------------------------------- err=dc1394_format7_set_packet_size(camera,video_mode, format7_info.max_packet_size); if( err != DC1394_SUCCESS ) throw VideoException("Could not set format7 packet size"); if((framerate != MAX_FR) && (framerate != EXT_TRIG)){ //set the framerate by using the absolute feature as suggested by the //folks at PointGrey err = dc1394_feature_set_absolute_control(camera,DC1394_FEATURE_FRAME_RATE,DC1394_ON); if( err != DC1394_SUCCESS ) throw VideoException("Could not turn on absolute frame rate control"); err = dc1394_feature_set_mode(camera,DC1394_FEATURE_FRAME_RATE,DC1394_FEATURE_MODE_MANUAL); if( err != DC1394_SUCCESS ) throw VideoException("Could not make frame rate manual "); err=dc1394_feature_set_absolute_value(camera,DC1394_FEATURE_FRAME_RATE,framerate); if( err != DC1394_SUCCESS ) throw VideoException("Could not set format7 framerate "); } // ask the camera what is the resulting framerate (this assume that such a rate is actually // allowed by the shutter time) float value; err=dc1394_feature_get_absolute_value(camera,DC1394_FEATURE_FRAME_RATE,&value); if( err != DC1394_SUCCESS ) throw VideoException("Could not get framerate"); cout<<" framerate(shutter permitting):"<<value<<endl; //----------------------------------------------------------------------- // setup capture //----------------------------------------------------------------------- err=dc1394_capture_setup(camera,dma_frames, DC1394_CAPTURE_FLAGS_DEFAULT); if( err != DC1394_SUCCESS ) throw VideoException("Could not setup camera - check settings"); Start(); }
// Note: // the following was tested on a IIDC camera over USB therefore might not work as // well on a camera over proper firewire transport void FirewireVideo::init_format7_camera( uint64_t guid, int dma_frames, dc1394speed_t iso_speed, dc1394video_mode_t video_mode, int framerate, uint32_t width, uint32_t height, uint32_t left, uint32_t top, bool reset_at_boot ) { if(video_mode< DC1394_VIDEO_MODE_FORMAT7_0) throw VideoException("roi can be specified only for format7 modes"); camera = dc1394_camera_new (d, guid); if (!camera) throw VideoException("Failed to initialize camera"); // Attempt to stop camera if it is already running dc1394switch_t is_iso_on = DC1394_OFF; dc1394_video_get_transmission(camera, &is_iso_on); if (is_iso_on==DC1394_ON) { dc1394_video_set_transmission(camera, DC1394_OFF); } cout << "Using camera with GUID " << camera->guid << endl; if(reset_at_boot){ dc1394_camera_reset(camera); } //----------------------------------------------------------------------- // setup mode and roi //----------------------------------------------------------------------- if(iso_speed >= DC1394_ISO_SPEED_800) { err=dc1394_video_set_operation_mode(camera, DC1394_OPERATION_MODE_1394B); if( err != DC1394_SUCCESS ) throw VideoException("Could not set DC1394_OPERATION_MODE_1394B"); } err=dc1394_video_set_iso_speed(camera, iso_speed); if( err != DC1394_SUCCESS ) throw VideoException("Could not set iso speed"); // check that the required mode is actually supported dc1394format7mode_t format7_info; err = dc1394_format7_get_mode_info(camera, video_mode, &format7_info); if( err != DC1394_SUCCESS ) throw VideoException("Could not get format7 mode info"); // safely set the video mode err=dc1394_video_set_mode(camera, video_mode); if( err != DC1394_SUCCESS ) throw VideoException("Could not set format7 video mode"); // set position to 0,0 so that setting any size within min and max is a valid command err = dc1394_format7_set_image_position(camera, video_mode,0,0); if( err != DC1394_SUCCESS ) throw VideoException("Could not set format7 image position"); // work out the desired image size width = nearest_value(width, format7_info.unit_pos_x, 0, format7_info.max_size_x - left); height = nearest_value(height, format7_info.unit_pos_y, 0, format7_info.max_size_y - top); // set size err = dc1394_format7_set_image_size(camera,video_mode,width,height); if( err != DC1394_SUCCESS ) throw VideoException("Could not set format7 size"); // get the info again since many parameters depend on image size err = dc1394_format7_get_mode_info(camera, video_mode, &format7_info); if( err != DC1394_SUCCESS ) throw VideoException("Could not get format7 mode info"); // work out position of roi left = nearest_value(left, format7_info.unit_size_x, format7_info.unit_size_x, format7_info.max_size_x - width); top = nearest_value(top, format7_info.unit_size_y, format7_info.unit_size_y, format7_info.max_size_y - height); // set roi position err = dc1394_format7_set_image_position(camera,video_mode,left,top); if( err != DC1394_SUCCESS ) throw VideoException("Could not set format7 size"); this->width = width; this->height = height; this->top = top; this->left = left; cout<<"roi: "<<left<<" "<<top<<" "<<width<<" "<<height<<" "; //----------------------------------------------------------------------- // setup frame rate //----------------------------------------------------------------------- if((framerate == MAX_FR)||(framerate == EXT_TRIG)){ err=dc1394_format7_set_packet_size(camera,video_mode, format7_info.max_packet_size); if( err != DC1394_SUCCESS ) throw VideoException("Could not set format7 packet size"); } else { // setting packet size to get the desired frame rate according to the libdc docs // does not do the trick, so for now we support only max frame rate throw VideoException("In format 7 only max frame rate is currently supported"); // uint32_t depth; // err = dc1394_format7_get_data_depth(camera, video_mode, &depth); // if( err != DC1394_SUCCESS ) // throw VideoException("Could not get format7 depth"); // // // the following is straight from the libdc docs // double bus_period = bus_period_from_iso_speed(iso_speed); // // // work out the max number of packets that the bus can deliver // int num_packets = (int) (1.0/(bus_period*framerate) + 0.5); // // if((num_packets > 4095)||(num_packets < 0)) // throw VideoException("number of format7 packets out of range"); // // // work out what the packet size should be for the requested size and framerate // uint32_t packet_size = (width*964*depth + (num_packets*8) - 1)/(num_packets*8); // packet_size = nearest_value(packet_size,format7_info.unit_packet_size,format7_info.unit_packet_size,format7_info.max_packet_size); // // if(packet_size > format7_info.max_packet_size){ // throw VideoException("format7 requested frame rate and size exceed bus bandwidth"); // } // // err=dc1394_format7_set_packet_size(camera,video_mode, packet_size); // if( err != DC1394_SUCCESS ){ // throw VideoException("Could not set format7 packet size"); // } } // ask the camera what is the resulting framerate (this assume that such a rate is actually // allowed by the shutter time) err = dc1394_feature_set_power(camera,DC1394_FEATURE_FRAME_RATE,DC1394_OFF); if( err != DC1394_SUCCESS ) throw VideoException("Could not turn off frame rate"); float value; err=dc1394_feature_get_absolute_value(camera,DC1394_FEATURE_FRAME_RATE,&value); if( err != DC1394_SUCCESS ) throw VideoException("Could not get framerate"); cout<<" framerate(shutter permitting):"<<value<<endl; //----------------------------------------------------------------------- // setup capture //----------------------------------------------------------------------- err=dc1394_capture_setup(camera,dma_frames, DC1394_CAPTURE_FLAGS_DEFAULT); if( err != DC1394_SUCCESS ) throw VideoException("Could not setup camera - check settings"); Start(); }