📄 loopback_ntsc_cv_sv.c
字号:
/* Header files */
#include <stdio.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <stdlib.h>
#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 <time.h>
#include <media/davinci_vpfe.h> /*kernel header file, prefix path comes from makefile */
#if 0
#include "test_8.h"
#endif
int ch_no=0;
int numbuffers=3;
int fdglobal;
int format=0;
void usage()
{
printf
("Usage:stream -c channelno -n number of buffers -t format (0-YUYV, 1 - UYVY) \n");
}
char dev_name[2][20] = { "/dev/video2", "/dev/video3" };
#define IS_VALID_CHANNEL(ch) (((ch) == 0) || ((ch) == 1))
struct buffer
{
void *start;
int index;
size_t length;
};
#define CAPTURE_DEVICE "/dev/video0"
#define WIDTH 720
#define HEIGHT 480
#define MIN_BUFFERS 2
#define UYVY_BLACK 0x10801080
/* Device parameters */
#define V4L2VID0_DEVICE "/dev/video2"
#define V4L2VID1_DEVICE "/dev/video3"
/* Function error codes */
#define SUCCESS 0
#define FAILURE -1
/* Bits per pixel for video window */
#define DISPLAY_INTERFACE COMPOSITE
#define DISPLAY_MODE NTSC
#define round_32(width) ((((width) + 31) / 32) * 32 )
/* D1 screen dimensions */
#define VID0_WIDTH 720
#define VID0_HEIGHT 480
#if 0
#define VID0_BPP 16
#define VID0_FRAME_SIZE (VID0_WIDTH*VID0_HEIGHT)
#define VID0_VMODE FB_VMODE_INTERLACED
#endif
#define VID1_WIDTH 720
#define VID1_HEIGHT 480
#if 0
#define VID1_BPP 16
#define VID1_FRAME_SIZE (VID1_WIDTH*VID1_HEIGHT)
#define VID1_VMODE FB_VMODE_INTERLACED
//position
#define VID0_XPOS 0
#define VID0_YPOS 0
#define VID1_XPOS 0
#define VID1_YPOS 0
// Zoom Params
#define OSD0_HZOOM 0
#define OSD0_VZOOM 0
#define OSD1_HZOOM 0
#define OSD1_VZOOM 0
#define VID0_HZOOM 0
#define VID0_VZOOM 0
#define VID1_HZOOM 0
#define VID1_VZOOM 0
#endif
#define VIDEO_NUM_BUFS 3
struct vpbe_test_info
{
int vid0_width;
int vid0_height;
int vid0_frame_size;
int vid1_width;
int vid1_height;
int vid1_frame_size;
//POSITION
int vid0_xpos;
int vid0_ypos;
int vid1_xpos;
int vid1_ypos;
// Zoom Params
int vid0_hzoom;
int vid0_vzoom;
int vid1_hzoom;
int vid1_vzoom;
};
#define DISPLAY_SIZE 720*480*2
static struct vpbe_test_info test_data = {
720,
480,
DISPLAY_SIZE,
720,
480,
DISPLAY_SIZE,
0,
0,
0,
0,
0,
0,
0,
0,
};
#define DEBUG
#ifdef DEBUG
#define DBGENTER printf("%s : E", __FUNCTION__);
#define DBGEXIT printf("%s : L", __FUNCTION__);
#define PREV_DEBUG(x) printf("DEBUG:%s:%s:%s",__FUNCTION__,__LINE__,x);
#else
#define DBGENTER
#define DBGEXIT
#define PREV_DEBUG(x)
#endif
#define CLEAR(x) memset (&(x), 0, sizeof (x))
/* ************************************************************************/
//Globals
/* ************************************************************************/
static int fdCapture = -1;
static int fdDisplay = -1;
struct buffer *cap_buffers = NULL;
struct buffer *disp_buff_info = NULL;
static int nBuffers = 0;
static int nWidthFinal = 0;
static int nHeightFinal = 0;
static int quit = 0;
char *vid0_display[VIDEO_NUM_BUFS] = { NULL, NULL, NULL };
char *vid1_display[VIDEO_NUM_BUFS] = { NULL, NULL, NULL };
int fd_vid0 = 0, fd_vid1 = 0;
int vid0_size, vid1_size;
#if 0
zoom_params_t zoom;
#endif
int stress_test =1;// 1-default Abhishek; //Flag to indicate currently stress test is going on
int startLoopCnt = 10000;
extern int errno;
int StartLoop (void);
static int InitCaptureDevice (void);
static int SetDataFormat (void);
static int InitCaptureBuffers (void);
static int StartStreaming (void);
static void Initialize_Capture (void);
//static int DisplayFrame (char, void *);
static int DisplayFrame(int,void *);
int unmap_and_disable (char);
/* ************************************************************************/
void
Initialize_Capture (void)
{
printf ("initializing capture device\n");
InitCaptureDevice ();
printf ("setting data format\n");
SetDataFormat ();
printf ("initializing capture buffers\n");
InitCaptureBuffers ();
printf ("initializing capture device\n");
StartStreaming ();
}
/* ************************************************************************/
int
StartLoop (void)
{
struct v4l2_buffer buf;
static int captFrmCnt = 0;
char *ptrPlanar = NULL;
int dummy;
struct timeval timev;
struct timezone zone;
//StartStreaming();
ptrPlanar = (char *) calloc (1, nWidthFinal * nHeightFinal * 2);
while (!quit)
{
fd_set fds;
struct timeval tv;
int r;
bzero((void *)&zone, sizeof (struct timezone));
if (stress_test)
{
startLoopCnt--;
if (startLoopCnt == 0)
{
startLoopCnt = 50;
break;
}
}
FD_ZERO (&fds);
FD_SET (fdCapture, &fds);
/* Timeout. */
tv.tv_sec = 2;
tv.tv_usec = 0;
r = select (fdCapture + 1, &fds, NULL, NULL, &tv);
if (-1 == r)
{
if (EINTR == errno)
continue;
printf ("StartCameraCaputre:select\n");
return -1;
}
if (0 == r)
{
continue;
}
CLEAR (buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
//printf("Debug 6............\n");
/*determine ready buffer */
if (-1 == ioctl (fdCapture, VIDIOC_DQBUF, &buf))
{
if (EAGAIN == errno)
continue;
printf ("StartCameraCaputre:ioctl:VIDIOC_DQBUF\n");
return -1;
}
#if 0
gettimeofday(&timev,&zone);
printf("index = %d,before Display Frame sec=%d,usec=%d\n",buf.index,timev.tv_sec,timev.tv_usec);
printf("buffer times sec=%d,usec=%d\n",buf.timestamp.tv_sec,buf.timestamp.tv_usec);
#endif
DisplayFrame (fdglobal, cap_buffers[buf.index].start);
#if 0
gettimeofday(&timev,&zone);
printf("after Display Frame sec=%d,usec=%d\n",timev.tv_sec,timev.tv_usec);
#endif
#if 0
DisplayFrame (VID1, buffers[buf.index].start);
Displaybitmaposd0();
/* Wait for vertical sync */
if (ioctl (fd_vid0, FBIO_WAITFORVSYNC, &dummy) < -1)
{
printf ("Failed FBIO_WAITFORVSYNC\n");
return -1;
}
#endif
//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");
}
#if 0
gettimeofday(&timev,&zone);
printf("after QBUF sec=%d,usec=%d\n",timev.tv_sec,timev.tv_usec);
#endif
}
}
/* ************************************************************************/
struct v4l2_cropcap cropcap;
static int
InitCaptureDevice (void)
{
struct v4l2_capability cap;
struct v4l2_crop crop;
struct v4l2_input input;
/*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);
input.type = V4L2_INPUT_TYPE_CAMERA;
input.index = 0;
if (-1 == ioctl (fdCapture, VIDIOC_S_INPUT, &input))
{
printf ("InitDevice:ioctl:VIDIOC_S_INPUT\n");
return -1;
}
printf ("InitDevice:ioctl:VIDIOC_S_INPUT, selected COMPOSITE INPUT input\n");
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; //SSK Buffer Size
fmt.fmt.pix.height = HEIGHT;
//#if 0
if(format==0)
{
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
}
else
{
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
}
//#endif
//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);
cap_buffers = (struct buffer *) calloc (req.count, sizeof (struct buffer));
if (!cap_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;
}
printf("\nBuf.offset=%x\n",buf.m.offset);
cap_buffers[nIndex].length = buf.length;
cap_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, cap_buffers[nIndex].start, buf.length);
if (MAP_FAILED == cap_buffers[nIndex].start)
{
printf ("InitCaptureBuffers:mmap:\n");
return -1;
}
//printf("buffer:%d addr:%x\n",nIndex,buffers[nIndex].start);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -