Exemplo n.º 1
0
// 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();

}
Exemplo n.º 2
0
static gboolean
gst_dc1394_open_cam_with_best_caps (GstDc1394 * src)
{
    dc1394camera_list_t *cameras = NULL;
    gint err = 0;
    int framerateconst;

    GST_LOG_OBJECT (src, "Opening the camera!!!");


    if (dc1394_camera_enumerate (src->dc1394, &cameras) != DC1394_SUCCESS) {
        GST_ELEMENT_ERROR (src, RESOURCE, FAILED, ("Can't find cameras"),
                           ("Can't find cameras"));
        goto error;
    }

    GST_LOG_OBJECT (src, "Found  %d  cameras", cameras->num);

    if (src->camnum > (cameras->num - 1)) {
        GST_ELEMENT_ERROR (src, RESOURCE, FAILED, ("Invalid camera number"),
                           ("Invalid camera number"));
        goto error;
    }

    GST_LOG_OBJECT (src, "Opening camera : %d", src->camnum);

    src->camera =
        dc1394_camera_new_unit (src->dc1394, cameras->ids[src->camnum].guid,
                                cameras->ids[src->camnum].unit);

    dc1394_camera_free_list (cameras);
    cameras = NULL;

    // figure out mode
    framerateconst = gst_dc1394_framerate_frac_to_const (src->rate_numerator,
                     src->rate_denominator);

    GST_LOG_OBJECT (src, "The dma buffer queue size is %d  buffers",
                    src->bufsize);

    switch (src->iso_speed) {
    case 100:
        err = dc1394_video_set_iso_speed (src->camera, DC1394_ISO_SPEED_100);
        break;
    case 200:
        err = dc1394_video_set_iso_speed (src->camera, DC1394_ISO_SPEED_200);
        break;
    case 400:
        err = dc1394_video_set_iso_speed (src->camera, DC1394_ISO_SPEED_400);
        break;
    case 800:
        if (src->camera->bmode_capable > 0) {
            dc1394_video_set_operation_mode (src->camera,
                                             DC1394_OPERATION_MODE_1394B);
            err = dc1394_video_set_iso_speed (src->camera, DC1394_ISO_SPEED_800);
        }
        break;
    case 1600:
        if (src->camera->bmode_capable > 0) {
            dc1394_video_set_operation_mode (src->camera,
                                             DC1394_OPERATION_MODE_1394B);
            err = dc1394_video_set_iso_speed (src->camera, DC1394_ISO_SPEED_1600);
        }
        break;
    case 3200:
        if (src->camera->bmode_capable > 0) {
            dc1394_video_set_operation_mode (src->camera,
                                             DC1394_OPERATION_MODE_1394B);
            err = dc1394_video_set_iso_speed (src->camera, DC1394_ISO_SPEED_3200);
        }
        break;
    default:
        GST_ELEMENT_ERROR (src, RESOURCE, FAILED, ("Invalid ISO speed"),
                           ("Invalid ISO speed"));
        goto error;
        break;
    }

    if (err != DC1394_SUCCESS) {
        GST_ELEMENT_ERROR (src, RESOURCE, FAILED, ("Could not set ISO speed"),
                           ("Could not set ISO speed"));
        goto error;
    }

    GST_LOG_OBJECT (src, "Setting mode :  %d", src->vmode);
    err = dc1394_video_set_mode (src->camera, src->vmode);

    if (err != DC1394_SUCCESS) {
        GST_ELEMENT_ERROR (src, RESOURCE, FAILED, ("Could not set video mode %d",
                           src->vmode), ("Could not set video mode %d", src->vmode));
        goto error;
    }

    GST_LOG_OBJECT (src, "Setting framerate :  %d", framerateconst);
    dc1394_video_set_framerate (src->camera, framerateconst);

    if (err != DC1394_SUCCESS) {
        GST_ELEMENT_ERROR (src, RESOURCE, FAILED, ("Could not set framerate to %d",
                           framerateconst), ("Could not set framerate to %d", framerateconst));
        goto error;
    }
    // set any format-7 parameters if this is a format-7 mode
    if (src->vmode >= DC1394_VIDEO_MODE_FORMAT7_MIN &&
            src->vmode <= DC1394_VIDEO_MODE_FORMAT7_MAX) {
        // the big thing we care about right now is frame size
        err = dc1394_format7_set_image_size (src->camera, src->vmode,
                                             src->width, src->height);
        if (err != DC1394_SUCCESS) {
            GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
                               ("Could not set format 7 image size to %d x %d", src->width,
                                src->height), ("Could not set format 7 image size to %d x %d",
                                               src->width, src->height));

            goto error;
        }

    }
    err = dc1394_capture_setup (src->camera, src->bufsize,
                                DC1394_CAPTURE_FLAGS_DEFAULT);
    if (err != DC1394_SUCCESS) {
        GST_ELEMENT_ERROR (src, RESOURCE, FAILED, ("Error setting capture mode"),
                           ("Error setting capture mode"));
    }
    if (err != DC1394_SUCCESS) {
        if (err == DC1394_NO_BANDWIDTH) {
            GST_LOG_OBJECT (src, "Capture setup_dma failed."
                            "Trying to cleanup the iso_channels_and_bandwidth and retrying");

            // try to cleanup the bandwidth and retry
            err = dc1394_iso_release_all (src->camera);
            if (err != DC1394_SUCCESS) {
                GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
                                   ("Could not cleanup bandwidth"), ("Could not cleanup bandwidth"));
                goto error;
            } else {
                err =
                    dc1394_capture_setup (src->camera, src->bufsize,
                                          DC1394_CAPTURE_FLAGS_DEFAULT);
                if (err != DC1394_SUCCESS) {
                    GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
                                       ("unable to setup camera error %d", err),
                                       ("unable to setup camera error %d", err));
                    goto error;
                }
            }
        } else {
            GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
                               ("unable to setup camera error %d", err),
                               ("unable to setup camera error %d", err));
            goto error;

        }
    }


    return TRUE;

error:

    if (src->camera) {
        dc1394_camera_free (src->camera);
        src->camera = NULL;
    }

    return FALSE;

}
Exemplo n.º 3
0
// 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();

}