⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dc1394_capture.c

📁 DCAM1394相机Linux下的驱动源码
💻 C
📖 第 1 页 / 共 2 页
字号:
 dc1394_release_camera Frees buffer space contained in the cameracapture  structure*****************************************************/int dc1394_release_camera(raw1394handle_t handle,dc1394_cameracapture *camera){    dc1394_unset_one_shot(handle, camera->node);    if (camera->capture_buffer != NULL)     {        free(camera->capture_buffer);    }    return DC1394_SUCCESS;}/***************************************************** dc1394_single_capture Captures a frame of video from the camera specified******************************************************/int dc1394_single_capture(raw1394handle_t handle,                      dc1394_cameracapture *camera){    return dc1394_multi_capture(handle, camera, 1);}/*************************************************************************** dc1394_multi_capture This routine captures a frame from each camera specified in the cams array. Cameras must be set up first using dc1394_setup_camera. Returns DC1394_FAILURE if it fails, DC1394_SUCCESS if it succeeds****************************************************************************/int dc1394_multi_capture(raw1394handle_t handle, dc1394_cameracapture *cams,                     int num) {    int i, j;    _dc1394_all_captured= num;    /*      this first routine does the setup-      sets up the global variables needed in the handler,      sets the iso handler,      tells the 1394 subsystem to listen for iso packets    */    for (i= 0; i < num; i++)     {        _dc1394_buffer[cams[i].channel]= cams[i].capture_buffer;        if (raw1394_set_iso_handler(handle,cams[i].channel,                                    _dc1394_video_iso_handler) < 0)         {            /* error handling- for some reason something didn't work,                so we have to reset everything....*/            printf("(%s:%d) error!\n",__FILE__, __LINE__);            for (j= i - 1; j > -1; j--)             {                raw1394_stop_iso_rcv(handle, cams[j].channel);                raw1394_set_iso_handler(handle, cams[j].channel, NULL);            }            return DC1394_FAILURE;        }        _dc1394_frame_captured[cams[i].channel]= 0;        _dc1394_quadlets_per_frame[cams[i].channel]=            cams[i].quadlets_per_frame;        _dc1394_quadlets_per_packet[cams[i].channel]=            cams[i].quadlets_per_packet;        if (raw1394_start_iso_rcv(handle,cams[i].channel) < 0)         {            /* error handling- for some reason something didn't work,                so we have to reset everything....*/            printf("(%s:%d) error!\n", __FILE__, __LINE__);            for (j= 0; j < num; j++)             {                raw1394_stop_iso_rcv(handle, cams[j].channel);                raw1394_set_iso_handler(handle, cams[j].channel, NULL);            }            return DC1394_FAILURE;        }    }    /* now we iterate till the data is here*/    while (_dc1394_all_captured != 0)     {        raw1394_loop_iterate(handle);    }      /* now stop the subsystem from listening*/    for (i= 0; i < num; i++)     {        raw1394_stop_iso_rcv(handle, cams[i].channel);        raw1394_set_iso_handler(handle,cams[i].channel, NULL);    }    return DC1394_SUCCESS;}/********************************** DMA Capture Functions  These routines will be much faster than the above capture routines.***********************************//***************************************************** dc1394_dma_setup_capture This sets up the given camera to capture images using the dma engine.  Should be much faster than the above routines******************************************************/intdc1394_dma_setup_capture(raw1394handle_t handle, nodeid_t node,                         int channel, int format, int mode,                         int speed, int frame_rate,                         int num_dma_buffers, 			 int drop_frames,			 const char *dma_device_file,			 dc1394_cameracapture *camera){     dc1394_camerahandle *camera_handle;    camera_handle = (dc1394_camerahandle*) raw1394_get_userdata( handle );     if( format == FORMAT_SCALABLE_IMAGE_SIZE)    {      return dc1394_dma_setup_format7_capture( handle, node, channel, mode, speed,					       QUERY_FROM_CAMERA, /*bytes_per_paket*/					       QUERY_FROM_CAMERA, /*left*/					       QUERY_FROM_CAMERA, /*top*/					       QUERY_FROM_CAMERA, /*width*/					       QUERY_FROM_CAMERA, /*height*/					       num_dma_buffers,					       drop_frames,					       dma_device_file,					       camera);      }    else {      camera->port = camera_handle->port;      camera->dma_device_file = dma_device_file;      camera->drop_frames = drop_frames;      if (_dc1394_basic_setup(handle,node, channel, format, mode,			      speed,frame_rate, camera) != DC1394_SUCCESS)	{	  return DC1394_FAILURE;	}      return _dc1394_dma_basic_setup (channel, num_dma_buffers, camera);    }}/***************************************************** dc1394_dma_release_camera This releases memory that was mapped by dc1394_dma_setup_camera*****************************************************/int dc1394_dma_release_camera(raw1394handle_t handle,                          dc1394_cameracapture *camera) {    //dc1394_stop_iso_transmission(handle,camera->node);    //ioctl(_dc1394_dma_fd,VIDEO1394_IOC_UNLISTEN_CHANNEL, &(camera->channel));    if (camera->dma_ring_buffer)    {        munmap((void*)camera->dma_ring_buffer,camera->dma_buffer_size);	_dc1394_num_using_fd[camera->port]--;		if (_dc1394_num_using_fd[camera->port] == 0)	  {	    	    while (close(camera->dma_fd) != 0)	      {		printf("(%s) waiting for dma_fd to close\n", __FILE__);		sleep(1);	      }	    	  }    }    return DC1394_SUCCESS;}/***************************************************** dc1394_dma_unlisten This tells video1394 to halt iso reception.*****************************************************/int dc1394_dma_unlisten(raw1394handle_t handle, dc1394_cameracapture *camera){    if (ioctl(camera->dma_fd, VIDEO1394_IOC_UNLISTEN_CHANNEL, &(camera->channel))        < 0)    {        return DC1394_FAILURE;    }    else    {        return DC1394_SUCCESS;    }}/**************************************************** _dc1394_dma_multi_capture_private This capture a frame from each of the cameras passed in cams.  After you are finished with the frame, you must return the buffer to the pool by calling dc1394_dma_done_with_buffer. This function is private.*****************************************************/int_dc1394_dma_multi_capture_private(dc1394_cameracapture *cams, int num, dc1394videopolicy_t policy) {    struct video1394_wait vwait;    int i;    int cb;    int j;    int result=-1;    int last_buffer_orig;    int extra_buf;    for (i= 0; i < num; i++)    {        last_buffer_orig = cams[i].dma_last_buffer;        cb = (cams[i].dma_last_buffer + 1) % cams[i].num_dma_buffers;        cams[i].dma_last_buffer = cb;			vwait.channel = cams[i].channel;			vwait.buffer = cb;			switch (policy) {			case VIDEO1394_POLL:			  result=ioctl(cams[i].dma_fd, VIDEO1394_IOC_LISTEN_POLL_BUFFER, &vwait);			  break;			case VIDEO1394_WAIT:			default:			  result=ioctl(cams[i].dma_fd, VIDEO1394_IOC_LISTEN_WAIT_BUFFER, &vwait);			  break;			}			if ( result != 0) 			{       			        cams[i].dma_last_buffer = last_buffer_orig;                                if ((policy==VIDEO1394_POLL) && (errno == EINTR))				  {                       				    // when no frames is present, say so.				    return DC1394_NO_FRAME;				  }				else				  {				    printf("(%s) VIDEO1394_IOC_LISTEN_WAIT/POLL_BUFFER ioctl failed!\n",					   __FILE__);				    cams[i].dma_last_buffer++; //Ringbuffer-index or counter?				    return DC1394_FAILURE;				  }			}			extra_buf = vwait.buffer;			if (cams[i].drop_frames)			{				if (extra_buf > 0) 				{				    for (j = 0; j < extra_buf; j++)				    {					vwait.buffer = (cb + j) % cams[i].num_dma_buffers;					if (ioctl(cams[i].dma_fd, VIDEO1394_IOC_LISTEN_QUEUE_BUFFER, &vwait) < 0) 					{						printf("(%s) VIDEO1394_IOC_LISTEN_QUEUE_BUFFER failed in "							   "multi capture!\n", __FILE__);						return DC1394_FAILURE;					}				    }				    cams[i].dma_last_buffer = (cb + extra_buf) % cams[i].num_dma_buffers;				    /* Get the corresponding filltime: */				    vwait.buffer = cams[i].dma_last_buffer;				    if(ioctl(cams[i].dma_fd, VIDEO1394_IOC_LISTEN_POLL_BUFFER, &vwait) < 0)				    {					printf("(%s) VIDEO1394_IOC_LISTEN_POLL_BUFFER "					       "failed in multi capture!\n",					       __FILE__);					return DC1394_FAILURE;				    }				}			}			/* point to the next buffer in the dma ringbuffer */			cams[i].capture_buffer = (int*)(cams[i].dma_ring_buffer +							cams[i].dma_last_buffer *							cams[i].dma_frame_size);			cams[i].filltime = vwait.filltime;			cams[i].num_dma_buffers_behind = extra_buf;    }    return DC1394_SUCCESS;}/**************************************************** dc1394_dma_single_capture This captures a frame from the given camera. Two policies are available: wait for a frame or return if no frame is available (POLL)*****************************************************/int dc1394_dma_single_capture(dc1394_cameracapture *camera) {    return _dc1394_dma_multi_capture_private(camera,1, VIDEO1394_WAIT);}intdc1394_dma_single_capture_poll(dc1394_cameracapture *camera){    return _dc1394_dma_multi_capture_private(camera,1, VIDEO1394_POLL);}/**************************************************** dc1394_dma_multi_capture This captures a frame from the given camera. Two policies are available: wait for a frame or return if no frame is available (POLL)*****************************************************/int dc1394_dma_multi_capture(dc1394_cameracapture *camera, int num) {    return _dc1394_dma_multi_capture_private(camera, num, VIDEO1394_WAIT);}intdc1394_dma_multi_capture_poll(dc1394_cameracapture *camera, int num){    return _dc1394_dma_multi_capture_private(camera, num, VIDEO1394_POLL);}/**************************************************** dc1394_dma_done_with_buffer This allows the driver to use the buffer previously handed to the user by dc1394_dma_*_capture*****************************************************/int dc1394_dma_done_with_buffer(dc1394_cameracapture *camera) {        struct video1394_wait vwait;        if (camera->dma_last_buffer == -1)        return DC1394_SUCCESS;    vwait.channel= camera->channel;    vwait.buffer= camera->dma_last_buffer;    if (ioctl(camera->dma_fd, VIDEO1394_IOC_LISTEN_QUEUE_BUFFER, &vwait) < 0)     {        printf("(%s) VIDEO1394_IOC_LISTEN_QUEUE_BUFFER failed in "               "done with buffer!\n", __FILE__);        return DC1394_FAILURE;    }    return DC1394_SUCCESS;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -