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

📄 mt9t001_capture.c

📁 德州仪器新推出的达芬奇技术dm355的源代码
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>		/* low-level i/o */
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <asm/types.h>		/* for videodev2.h */
#include <pthread.h>
#include <time.h>
#include <linux/videodev.h>
#include <media/dm355_vpfe.h>	/*kernel header file, prefix path comes from
				   makefile */
#include "ipipe_example.h"
#include "mt9t001_capture.h"


static struct buffer *buffers = NULL;
static int fdCapture = -1;

static int nBuffers = 0;

extern int errno;

static int configCCDCraw();
static int SetDataFormat(v4l2_std_id std, int width, int height );
static int InitCaptureBuffers(void);

#define CLEAR(x) memset (&(x), 0, sizeof (x))

void CaptureFrame(int *phyaddr)
{
	
	static struct v4l2_buffer buf;
	enum v4l2_buf_type type;
	static int init_flag = 0;
	type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	if(!init_flag){
		CLEAR(buf);
		buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
		buf.memory = V4L2_MEMORY_MMAP;
		init_flag = 1;
	}else {
		/* requeue the buffer */
		if (-1 == ioctl(fdCapture, VIDIOC_QBUF, &buf)) {
			perror("StartCameraCaputre:ioctl:VIDIOC_QBUF");
		}
	}

	/*determine ready buffer */
	if (-1 == ioctl(fdCapture, VIDIOC_DQBUF, &buf)) {
		if (EAGAIN == errno)
			return ;
		perror("StartCameraCaputre:ioctl:VIDIOC_DQBUF");
		exit(0);
	}
	*phyaddr = buffers[buf.index].offset;
}

int InitCaptureDevice(v4l2_std_id std, int width, int height)
{
	int ret = 0;
	struct v4l2_capability cap;

	/*input-0 is selected by default, so no need to set it */
	if ((fdCapture =
	     open(CAPTURE_DEVICE, O_RDWR | O_NONBLOCK, 0)) <= -1) {

		perror("InitDevice:open::");
		exit(0);
	} else
		printf("\nOpen Done\n");

	/*Is capture supported? */
	if (-1 == ioctl(fdCapture, VIDIOC_QUERYCAP, &cap)) {
		perror("InitDevice:ioctl:VIDIOC_QUERYCAP:");
		exit(0);
	} else
		printf("\nQUERYCAP Done\n");

	if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
		printf("InitDevice:capture is not supported on:%s\n",
		       CAPTURE_DEVICE);
		exit(0);
	}

	/*is MMAP-IO supported? */
	if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
		printf
		    ("InitDevice:IO method MMAP is not supported on:%s\n",
		     CAPTURE_DEVICE);
		exit(0);
	}
	printf("setting data format\n");
	SetDataFormat(std, width, height);

	printf("\nCalling configCCDCraw()\n");
	ret = configCCDCraw();
	if (ret < 0) {
		perror("configCCDCraw");
		exit(0);
	} else {
		printf("\nconfigCCDCraw Done\n");
	}

	printf("initializing capture buffers\n");
	InitCaptureBuffers();

	return 0;
}

void CloseCaptureDevice()
{
	int i;
	enum v4l2_buf_type type;
	
	if (-1 == ioctl(fdCapture, VIDIOC_STREAMOFF, &type)) {
		perror("StopStreaming:ioctl:VIDIOC_STREAMOFF");
	}

	for(i = 0; i < nBuffers; i++)
		munmap(buffers[i].start, buffers[i].length);
	close(fdCapture);
}

static int SetDataFormat(v4l2_std_id std, int width, int height)
{
	v4l2_std_id ipipe_std, cur_std;
	struct v4l2_format fmt;

	cur_std = ipipe_std = std;


	printf("SetDataFormat:setting std to %d\n", (int)cur_std);

	if (-1 == ioctl(fdCapture, VIDIOC_S_STD, &cur_std)) {
		printf
		    ("SetDataFormat:unable to set standard automatically\n");
		exit(0);
	} else
		printf("\nS_STD Done\n");

	sleep(1);		/* wait until device is fully locked */
	cur_std = 0;
	if (-1 == ioctl(fdCapture, VIDIOC_G_STD, &cur_std)) {
		perror("SetDataFormat:ioctl:VIDIOC_G_STD:");
	} else
		printf("\nGetSTD Done WITH std = %u\n", (int) cur_std);

	printf("SetDataFormat:requesting width:%d height:%d\n", width, height);
	CLEAR(fmt);
	fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	fmt.fmt.pix.width = width;
	fmt.fmt.pix.height = height;
	fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;

	/* the field can be either interlaced together or
	   separated in a top, bottom fashion */
	fmt.fmt.pix.field = V4L2_FIELD_NONE;
	if (-1 == ioctl(fdCapture, VIDIOC_S_FMT, &fmt)) {
		perror("SetDataFormat:ioctl:VIDIOC_S_FMT");
		exit(0);
	} else
		printf("\nS_FMT Done\n");

	return 0;
}

static int InitCaptureBuffers(void)
{
	struct v4l2_requestbuffers req;
	int nIndex = 0;

	CLEAR(req);
	req.count = MIN_BUFFERS;
	req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	req.memory = V4L2_MEMORY_MMAP;

	if (-1 == ioctl(fdCapture, VIDIOC_REQBUFS, &req)) {
		perror("InitCaptureBuffers:ioctl:VIDIOC_REQBUFS");
		exit(0);
	} else
		printf("\nREQBUF Done\n");


	if (req.count < MIN_BUFFERS) {
		printf
		    ("InitCaptureBuffers only:%d buffers avilable,\n",
		     req.count);
		exit(0);
	}

	nBuffers = req.count;
	buffers =
	    (struct buffer *) calloc(req.count, sizeof(struct buffer));
	if (!buffers) {
		perror("InitCaptureBuffers:calloc:");
		exit(0);
	}

	for (nIndex = 0; nIndex < req.count; ++nIndex) {

		struct v4l2_buffer buf;
		CLEAR(buf);
		buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
		buf.memory = V4L2_MEMORY_MMAP;
		buf.index = nIndex;

		if (-1 == ioctl(fdCapture, VIDIOC_QUERYBUF, &buf)) {
			perror
			    ("InitCaptureBuffers:ioctl:VIDIOC_QUERYBUF:\n");
			exit(0);
		} else
			printf("\nQUERYBUF Done\n");

		buffers[nIndex].length = buf.length;
		buffers[nIndex].start =
		    mmap(NULL, buf.length, PROT_READ | PROT_WRITE,
			 MAP_SHARED, fdCapture, buf.m.offset);
		buffers[nIndex].offset = buf.m.offset;

		if (MAP_FAILED == buffers[nIndex].start) {
			perror("InitCaptureBuffers:mmap:");
			exit(0);
		}
	}

	return 0;
}

int StartStreaming(void)
{
	int i = 0;
	enum v4l2_buf_type type;

	for (i = 0; i < nBuffers; i++) {
		struct v4l2_buffer buf;
		CLEAR(buf);
		buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
		buf.memory = V4L2_MEMORY_MMAP;
		buf.index = i;
		printf("Queing buffer:%d\n", i);

		if (-1 == ioctl(fdCapture, VIDIOC_QBUF, &buf)) {
			perror("StartStreaming:ioctl:VIDIOC_QBUF:");
		} else
			printf("\nQ_BUF Done\n");

	}
	/* all done , get set go */
	type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	if (-1 == ioctl(fdCapture, VIDIOC_STREAMON, &type)) {
		perror("StartStreaming:ioctl:VIDIOC_STREAMON:");
	} else
		printf("\nSTREAMON Done\n");

	return 0;
}

static int configCCDCraw()
{
	ccdc_config_params_raw raw_params = {
                            .pix_fmt = CCDC_PIXFMT_RAW,
                            .frm_fmt = CCDC_FRMFMT_PROGRESSIVE,
                            .win = VPFE_WIN_VGA,
                            .fid_pol = CCDC_PINPOL_POSITIVE,
                            .vd_pol = CCDC_PINPOL_POSITIVE,
                            .hd_pol = CCDC_PINPOL_POSITIVE,
                            .image_invert_enable = FALSE,
                            .data_sz = _12BITS,
                            .med_filt_thres = 0,
                            .mfilt1 = NO_MEDIAN_FILTER1,
                            .mfilt2 =  NO_MEDIAN_FILTER2,
                            .ccdc_offset = 0,
                            .lpf_enable = FALSE,
                            .datasft = 2,
                            .alaw = {
                                     .b_alaw_enable = FALSE,
                                     .gama_wd = 0},
                            .blk_clamp = {
                                          .b_clamp_enable = FALSE,
                                          .sample_pixel = 1,
                                          .start_pixel = 0,
                                          .dc_sub = 0},
                            .blk_comp = {
                                         .b_comp = 0,
                                         .gb_comp = 0,
                                         .gr_comp = 0,
                                         .r_comp = 0},
                            .vertical_dft = {
                                          .ver_dft_en = FALSE},
                            .lens_sh_corr = {
                                          .lsc_enable = FALSE},
 			    .data_formatter_r = {
                                          	.fmt_enable = FALSE},
                            .color_space_con = {
                                          	.csc_enable = FALSE}
                      };


	if (-1 == ioctl(fdCapture, VPFE_CMD_CONFIG_CCDC_RAW, &raw_params)) {
		perror("InitDevice:ioctl:VPFE_CMD_CONFIG_CCDC_RAW:");
		exit(0);
	}
	return 0;

}

⌨️ 快捷键说明

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