void camera_close() { free(current_pic.buffer); free_buf_mmap(); v4lconvert_destroy(lib); close(fd_cam); }
int camera_init(struct picture_t *out_info) { fd_cam = open(CAM_NAME, O_RDWR); if(fd_cam < 0){ perror("open camera " CAM_NAME); return 0; } if(!get_format()) goto label_close; lib = v4lconvert_create(fd_cam); if(!lib) { perror("v4lconvert_create"); goto label_close; } if(!buf_alloc_mmap()) goto label_free; YUV420_size = current_pic.width*current_pic.height*3/2; if(!(current_pic.buffer = malloc(YUV420_size))){ perror("malloc"); goto label_free; } *out_info = current_pic; return 1; label_free: free_buf_mmap(); v4lconvert_destroy(lib); label_close: close(fd_cam); return 0; }
void capture() { unsigned char *yuv420_buf; int yuv420_size = fmt.fmt.pix.width*fmt.fmt.pix.height*3/2; int src_size, i, j, k, nframe; struct v4lconvert_data *lib; buffersize = calc_size(fmt.fmt.pix.sizeimage); buf_alloc_mmap(); lib = v4lconvert_create(fd_cam); if(!lib) { perror("v4lconvert_create"); exit(1); } yuv420_buf = malloc(yuv420_size); if(!yuv420_buf){ perror("malloc"); exit(1); } if(ioctl(fd_cam, VIDIOC_STREAMON, &reqbuf.type) < 0) { perror("VIDIOC_STREAMON"); exit(1); } dst_fmt = fmt; dst_fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420; if(!v4lconvert_supported_dst_format(dst_fmt.fmt.pix.pixelformat)){ puts("v4lconvert_supported_dst_format"); exit(1); } for(errno = 0, nframe = 0; nframe < NUM_FRAME; nframe++) { struct v4l2_buffer cam_buf = {0}; cam_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; cam_buf.memory = reqbuf.memory; if(ioctl(fd_cam, VIDIOC_DQBUF, &cam_buf) < 0) { perror("VIDIOC_DQBUF"); exit(1); } printf("DQBUF: index=%d, seq=%d, time=%d.%06d\n", cam_buf.index, cam_buf.sequence, cam_buf.timestamp.tv_sec, cam_buf.timestamp.tv_usec); src_size = cam_buf.length; if(v4lconvert_convert(lib, &fmt, &dst_fmt, (void*)buf_pointer[cam_buf.index], src_size, yuv420_buf, yuv420_size) <= 0){ perror("v4lconvert_convert"); exit(1); } cam_buf.length = buffersize; if(ioctl(fd_cam, VIDIOC_QBUF, &cam_buf) < 0) { perror("VIDIOC_QBUF"); exit(1); } write(fd_out, yuv420_buf, yuv420_size); } if(ioctl(fd_cam, VIDIOC_STREAMOFF, &reqbuf.type) < 0) { perror("VIDIOC_STREAMOFF"); exit(1); } free(yuv420_buf); v4lconvert_destroy(lib); free_buf_mmap(); }