📄 prograssiveexample.c
字号:
buf.memory = V4L2_MEMORY_MMAP;
/*determine ready buffer */
if (-1 == ioctl(fdCapture, VIDIOC_DQBUF, &buf)) {
if (EAGAIN == errno)
continue;
printf("StartCameraCaputre:ioctl:VIDIOC_DQBUF\n");
return -1;
}
DisplayFrame(VID1, buffers[buf.index].start);
Displaybitmaposd0();
Displaybitmaposd1();
/* Wait for vertical sync */
if (ioctl(fd_vid1, FBIO_WAITFORVSYNC, &dummy) < -1) {
printf("Failed FBIO_WAITFORVSYNC\n");
return -1;
}
printf("time:%d frame:%u\n", time(NULL), captFrmCnt++);
/* requeue the buffer */
if (-1 == ioctl(fdCapture, VIDIOC_QBUF, &buf)) {
printf("StartCameraCaputre:ioctl:VIDIOC_QBUF\n");
}
}
}
/* ************************************************************************/
struct v4l2_cropcap cropcap;
static int InitCaptureDevice(void)
{
struct v4l2_capability cap;
struct v4l2_crop crop;
/*input-0 is selected by default, so no need to set it */
if ((fdCapture =
open(CAPTURE_DEVICE, O_RDWR | O_NONBLOCK, 0)) <= -1) {
printf("InitDevice:open::\n");
return -1;
}
/*is capture supported? */
if (-1 == ioctl(fdCapture, VIDIOC_QUERYCAP, &cap)) {
printf("InitDevice:ioctl:VIDIOC_QUERYCAP:\n");
return -1;
}
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
printf("InitDevice:capture is not supported on:%s\n",
CAPTURE_DEVICE);
return -1;
}
/*is MMAP-IO supported? */
if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
printf
("InitDevice:IO method MMAP is not supported on:%s\n",
CAPTURE_DEVICE);
return -1;
}
/*select cropping as deault rectangle */
cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (-1 == ioctl(fdCapture, VIDIOC_CROPCAP, &cropcap)) {
printf("InitDevice:ioctl:VIDIOC_CROPCAP\n");
/*ignore error */
}
printf
("Default crop capbility bounds - %d %d %d %d ; default - %d %d %d %d \n",
cropcap.bounds.left, cropcap.bounds.top, cropcap.bounds.width,
cropcap.bounds.height, cropcap.defrect.left,
cropcap.defrect.top, cropcap.defrect.width,
cropcap.defrect.height);
return 0;
}
/* ************************************************************************/
static int SetDataFormat(void)
{
v4l2_std_id prev_std, cur_std;
struct v4l2_format fmt;
unsigned int min;
cur_std = prev_std = VPFE_STD_AUTO;
printf("SetDataFormat:setting std to auto select\n");
if (-1 == ioctl(fdCapture, VIDIOC_S_STD, &cur_std)) {
printf
("SetDataFormat:unable to set standard automatically\n");
}
sleep(1); /* wait until decoder is fully locked */
if (-1 == ioctl(fdCapture, VIDIOC_QUERYSTD, &cur_std)) {
printf("SetDataFormat:ioctl:VIDIOC_QUERYSTD:\n");
}
if (cur_std == V4L2_STD_NTSC)
printf("Input video standard is NTSC.\n");
else if (cur_std == V4L2_STD_PAL)
printf("Input video standard is PAL.\n");
else if (cur_std == V4L2_STD_PAL_M)
printf("Input video standard is PAL-M.\n");
else if (cur_std == V4L2_STD_PAL_N)
printf("Input video standard is PAL-N.\n");
else if (cur_std == V4L2_STD_SECAM)
printf("Input video standard is SECAM.\n");
else if (cur_std == V4L2_STD_PAL_60)
printf("Input video standard to PAL60.\n");
printf("SetDataFormat:setting data format\n");
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_UYVY;
/* the field can be either interlaced together or
separated in a top, bottom fashion */
//fmt.fmt.pix.field = V4L2_FIELD_SEQ_TB;
fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
if (-1 == ioctl(fdCapture, VIDIOC_S_FMT, &fmt)) {
printf("SetDataFormat:ioctl:VIDIOC_S_FMT\n");
}
if (-1 == ioctl(fdCapture, VIDIOC_G_FMT, &fmt)) {
printf("SetDataFormat:ioctl:VIDIOC_QUERYSTD:\n");
}
nWidthFinal = fmt.fmt.pix.width;
nHeightFinal = fmt.fmt.pix.height;
printf("SetDataFormat:finally negotiated width:%d height:%d\n",
nWidthFinal, nHeightFinal);
/*checking what is finally negotiated */
min = fmt.fmt.pix.width * 2;
if (fmt.fmt.pix.bytesperline < min) {
printf
("SetDataFormat:driver reports bytes_per_line:%d(bug)\n",
fmt.fmt.pix.bytesperline);
/*correct it */
fmt.fmt.pix.bytesperline = min;
}
min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
if (fmt.fmt.pix.sizeimage < min) {
printf("SetDataFormat:driver reports size:%d(bug)\n",
fmt.fmt.pix.sizeimage);
/*correct it */
fmt.fmt.pix.sizeimage = min;
}
printf("SetDataFormat:Finally negitaited width:%d height:%d\n",
nWidthFinal, nHeightFinal);
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)) {
printf("InitCaptureBuffers:ioctl:VIDIOC_REQBUFS\n");
return -1;
}
if (req.count < MIN_BUFFERS) {
printf
("InitCaptureBuffers only:%d buffers avilable, can't proceed\n",
req.count);
return -1;
}
nBuffers = req.count;
printf("device buffers:%d\n", req.count);
buffers =
(struct buffer *) calloc(req.count, sizeof(struct buffer));
if (!buffers) {
printf("InitCaptureBuffers:calloc:\n");
return -1;
}
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)) {
printf
("InitCaptureBuffers:ioctl:VIDIOC_QUERYBUF:\n\n");
return -1;
}
buffers[nIndex].length = buf.length;
buffers[nIndex].start =
mmap(NULL, buf.length, PROT_READ | PROT_WRITE,
MAP_SHARED, fdCapture, buf.m.offset);
printf("buffer:%d phy:%x mmap:%x length:%d\n", buf.index,
buf.m.offset, buffers[nIndex].start, buf.length);
if (MAP_FAILED == buffers[nIndex].start) {
printf("InitCaptureBuffers:mmap:\n");
return -1;
}
//printf("buffer:%d addr:%x\n",nIndex,buffers[nIndex].start);
}
return 0;
}
/* ************************************************************************/
static 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)) {
printf("StartStreaming:ioctl:VIDIOC_QBUF:\n");
}
}
/* all done , get set go */
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (-1 == ioctl(fdCapture, VIDIOC_STREAMON, &type)) {
printf("StartStreaming:ioctl:VIDIOC_STREAMON:\n");
}
return 0;
}
/* ************************************************************************/
static int Displaybitmap(char id)
{
static unsigned int nDisplayIdx = 1;
static unsigned int nWorkingIndex = 0;
int y;
int xres, yres;
char *dst;
char *src;
int fd;
if (id == OSD0) {
dst = osd0_display[nWorkingIndex];
if (dst == NULL)
return -1;
fd = fd_osd0;
if (rgb565_enable) {
src = (char *) rgb16;
} else
src = (char *) test_8;
}
if (id == OSD1) {
dst = osd1_display[nWorkingIndex];
if (dst == NULL)
return -1;
fd = fd_osd1;
if (rgb565_enable) {
src = (char *) rgb16;
} else
src = (char *) test_8;
}
for (y = 0; y < test_data.osd0_height; y++) {
memcpy(dst, src, (test_data.osd0_width * 2));
dst += (test_data.osd0_width * 2);
src += (704 * 2);
}
if ((FlipBitmapBuffers(fd, nDisplayIdx)) < 0)
return -1;
nWorkingIndex = (nWorkingIndex + 1) % OSD_NUM_BUFS;
nDisplayIdx = (nDisplayIdx + 1) % OSD_NUM_BUFS;
return 0;
}
/* ************************************************************************/
static int Displaybitmaposd0()
{
static unsigned int nDisplayIdx = 1;
static unsigned int nWorkingIndex = 0;
int y;
int xres, yres;
char *dst;
char *src;
int fd;
struct fb_fix_screeninfo fixInfo;
dst = osd0_display[nWorkingIndex];
if (dst == NULL)
return -1;
fd = fd_osd0;
if (rgb565_enable == 1) //RGB565
{
src = (char *) rgb16;
for (y = 0; y < test_data.osd0_height; y++) {
memcpy(dst, src, (test_data.osd0_width * 2));
dst += osd0_fixInfo.line_length;
src += (704 * 2);
}
} else if (rgb565_enable == 2) //8 bit bitmap
{
src = (char *) test_8;
for (y = 0; y < test_data.osd0_height; y++) {
memcpy(dst, src, (test_data.osd0_width));
dst += osd0_fixInfo.line_length;
src += (704);
}
} else //1/2/4 bit bitmap and attribute
memset(dst, test_data.osd0_coloridx, osd0_size);
if ((FlipBitmapBuffers(fd, nDisplayIdx)) < 0)
return -1;
nWorkingIndex = (nWorkingIndex + 1) % OSD_NUM_BUFS;
nDisplayIdx = (nDisplayIdx + 1) % OSD_NUM_BUFS;
return 0;
}
/* ************************************************************************/
static int Displaybitmaposd1()
{
static unsigned int nDisplayIdx = 1;
static unsigned int nWorkingIndex = 0;
int y;
int xres, yres;
char *dst;
char *src;
int fd;
dst = osd1_display[nWorkingIndex];
if (dst == NULL)
return -1;
fd = fd_osd1;
if (rgb565_enable_osd1 == 1) //RGB565
{
src = (char *) rgb16;
for (y = 0; y < test_data.osd1_height; y++) {
memcpy(dst, src, (test_data.osd1_width * 2));
dst += osd1_fixInfo.line_length;
src += (704 * 2);
}
} else if (rgb565_enable_osd1 == 2) //8 bit bitmap
{
src = (char *) test_8;
for (y = 0; y < test_data.osd1_height; y++) {
memcpy(dst, src, (test_data.osd1_width));
dst += osd1_fixInfo.line_length;
src += (704);
}
} else //1/2/4 bit bitmap and attribute
memset(dst, test_data.osd1_coloridx, osd1_size);
if ((FlipBitmapBuffers(fd, nDisplayIdx)) < 0)
return -1;
nWorkingIndex = (nWorkingIndex + 1) % OSD_NUM_BUFS;
nDisplayIdx = (nDisplayIdx + 1) % OSD_NUM_BUFS;
return 0;
}
/* ************************************************************************/
static int DisplayFrame(char id, void *ptrBuffer)
{
static unsigned int nDisplayIdx = 0;
static unsigned int nWorkingIndex = 1;
int y;
int xres, yres;
char *dst;
char *src;
int fd;
xres = test_data.vid1_width;
yres = test_data.vid1_height;
dst = vid1_display[nWorkingIndex];
if (dst == NULL)
return -1;
fd = fd_vid1;
src = ptrBuffer;
for (y = 0; y < yres; y++) {
memcpy(dst, src, (720 * 2));
dst += vid1_fixInfo.line_length;
src += (720 * 2);
}
//memcpy(ptrFbbuf[nWorkingIndex],ptrBuffer,WIDTH*HEIGHT*2);
nWorkingIndex = (nWorkingIndex + 1) % VIDEO_NUM_BUFS;
nDisplayIdx = (nDisplayIdx + 1) % VIDEO_NUM_BUFS;
if ((FlipVideoBuffers(fd, nDisplayIdx)) < 0)
return -1;
return 0;
}
/* ************************************************************************/
int FlipBitmapBuffers(int fd, int nBufIndex)
{
struct fb_var_screeninfo vInfo;
int dummy;
if (ioctl(fd, FBIOGET_VSCREENINFO, &vInfo) < -1) {
printf("FlipbitmapBuffers:FBIOGET_VSCREENINFO\n");
printf("\n");
return -1;
}
vInfo.yoffset = vInfo.yres * nBufIndex;
/* Swap the working buffer for the displayed buffer */
if (ioctl(fd, FBIOPAN_DISPLAY, &vInfo) < -1) {
printf("FlipbitmapBuffers:FBIOPAN_DISPLAY\n");
printf("\n");
return -1;
}
return 0;
}
/* ************************************************************************/
int FlipVideoBuffers(int fd, int nBufIndex)
{
struct fb_var_screeninfo vInfo;
if (ioctl(fd, FBIOGET_VSCREENINFO, &vInfo) < -1) {
printf("FlipVideoBuffers:FBIOGET_VSCREENINFO\n");
printf("\n");
return -1;
}
vInfo.yoffset = vInfo.yres * nBufIndex;
/* Swap the working buffer for the displayed buffer */
if (ioctl(fd, FBIOPAN_DISPLAY, &vInfo) < -1) {
printf("FlipVideoBuffers:FBIOPAN_DISPLAY\n");
printf("\n");
return -1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -