#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <getopt.h> /* getopt_long() */
#include <fcntl.h> /* low-level i/o */
#include <unistd.h>
#include <errno.h>
#include <malloc.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <asm/types.h> /* for videodev2.h */
#include "videodev2.h"
#include "camera.h"
#include <stdarg.h>
#include <sys/mman.h>
#include <assert.h>
#include <pthread.h>
#include <semaphore.h>
#define CLEAR(x) memset (&(x), 0, sizeof (x))
#define LOG(...) fprintf(stderr, __VA_ARGS__)
#define ERRSTR strerror(errno)
#define ERR(...) __info("Error", __FILE__, __LINE__, __VA_ARGS__)
#define ERR_ON(cond, ...) ((cond) ? ERR(__VA_ARGS__) : 0)
#define CRIT(...) \
do { \
__info("Critical", __FILE__, __LINE__, __VA_ARGS__); \
exit(EXIT_FAILURE); \
} while(0)
#define CRIT_ON(cond, ...) do { if (cond) CRIT(__VA_ARGS__); } while(0)
extern BUFTYPE* fimc0_out_buf;
extern BUFTYPE* cam_buffers;
extern BUFTYPE* fimc0_cap_buf;
extern int fimc0_out_buf_length;
extern int fimc0_cap_buf_length;
Camera::Camera(char *DEV_NAME,char *FIMC_NAME,int w, int h)//构造函数
{
memcpy(dev_name,DEV_NAME,strlen(DEV_NAME));
memcpy(fimc_name,FIMC_NAME,strlen(DEV_NAME));
io = IO_METHOD_MMAP;//IO_METHOD_READ;//IO_METHOD_MMAP;
cap_image_size=0;
width=w;
height=h;
out_NV12= fopen("nv12.yuv420", "wb");
}
Camera::~Camera(){
}
static inline int __info(const char *prefix, const char *file, int line,
const char *fmt, ...)
{
int errsv = errno;
va_list va;
va_start(va, fmt);
fprintf(stderr, "%s(%s:%d): ", prefix, file, line);
vfprintf(stderr, fmt, va);
va_end(va);
errno = errsv;
return 1;
}
unsigned int Camera::getImageSize(){
return cap_image_size;
}
void Camera::CloseDevice() {
stop_capturing();
// uninit_device();
close_device();
}
void Camera::dump_format(char *str, struct v4l2_format *fmt)
{
if (V4L2_TYPE_IS_MULTIPLANAR(fmt->type)) {
struct v4l2_pix_format_mplane *pix = &fmt->fmt.pix_mp;
LOG("%s: width=%u height=%u format=%.4s bpl=%u\n", str,
pix->width, pix->height, (char*)&pix->pixelformat,
pix->plane_fmt[0].bytesperline);
} else {
struct v4l2_pix_format *pix = &fmt->fmt.pix;
LOG("%s: width=%u height=%u format=%.4s bpl=%u\n", str,
pix->width, pix->height, (char*)&pix->pixelformat,
pix->bytesperline);
}
}
void Camera::errno_exit(const char * s) {
fprintf(stderr, "%s error %d, %s\n", s, errno, strerror(errno));
exit(EXIT_FAILURE);
}
int Camera::xioctl(int fd, int request, void * arg) {
int r;
do
r = ioctl(fd, request, arg);
while (-1 == r && EINTR == errno);
return r;
}
void Camera::stop_capturing(void) {
enum v4l2_buf_type type;
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if(-1 == ioctl(fd,VIDIOC_STREAMOFF,&type))
{
perror("Fail to ioctl 'VIDIOC_STREAMOFF'");
exit(EXIT_FAILURE);
}
return;
}
void Camera::close_camer_device()
{
if(-1 == close(fd))
{
perror("Fail to close lcd_fd");
exit(EXIT_FAILURE);
}
if(-1 == close(fimc0_fd))
{
perror("Fail to close cam_fd");
exit(EXIT_FAILURE);
}
return;
}
bool Camera::start_capturing()
{
unsigned int i;
enum v4l2_buf_type type;
int ret;
struct v4l2_buffer b;
struct v4l2_plane plane;
printf("%s +\n", __func__);
for(i = 0;i < CAPTURE_BUFFER_NUMBER;i ++)
{
struct v4l2_buffer buf;
CLEAR(buf);
CLEAR(buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i;
if(-1 == ioctl(fd,VIDIOC_QBUF,&buf))
{
perror("cam Fail to ioctl 'VIDIOC_QBUF'");
exit(EXIT_FAILURE);
}
}
for(i = 0; i < CAPTURE_BUFFER_NUMBER; i++)
{
CLEAR(plane);
CLEAR(b);
b.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
b.memory = V4L2_MEMORY_MMAP;
b.index = i;
b.m.planes = &plane;
b.length = 1;
if(b.memory == V4L2_MEMORY_USERPTR)
{
plane.m.userptr = (unsigned long)fimc0_cap_buf[i].start;
plane.length = (unsigned long)fimc0_cap_buf_length;
plane.bytesused = fimc0_cap_buf_length;
}
ret = ioctl(fimc0_fd, VIDIOC_QBUF, &b);
if (ERR_ON(ret < 0, "fimc0: VIDIOC_QBUF: %s\n", ERRSTR))
return -errno;
}
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if(-1 == ioctl(fd,VIDIOC_STREAMON,&type))
{
printf("i = %d.\n",i);
perror("cam_fd Fail to ioctl 'VIDIOC_STREAMON'");
exit(EXIT_FAILURE);
}
/* start processing */
type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
ret = ioctl(fimc0_fd, VIDIOC_STREAMON, &type);
if (ERR_ON(ret < 0, "fimc0: VIDIOC_STREAMON: %s\n", ERRSTR))
return -errno;
type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
ret = ioctl(fimc0_fd, VIDIOC_STREAMON, &type);
if (ERR_ON(ret < 0, "fimc0: VIDIOC_STREAMON: %s\n", ERRSTR))
return -errno;
printf("%s -\n", __func__);
return 0;
}
bool Camera::cam_reqbufs()
{
struct v4l2_requestbuffers req;
int i;
printf("%s: +\n", __func__);
CLEAR(req);
req.count = CAPTURE_BUFFER_NUMBER;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP;
if (-1 == ioctl(fd, VIDIOC_REQBUFS, &req)) {
if (EINVAL == errno) {
fprintf(stderr, "%s does not support "
"user pointer i/o\n", "campture");
exit(EXIT_FAILURE);
}
else {
printf("VIDIOC_REQBUFS");
exit(EXIT_FAILURE);
}
}
cam_buffers = (BUFTYPE *)calloc(CAPTURE_BUFFER_NUMBER, sizeof(BUFTYPE));
if (!cam_buffers) {
fprintf(stderr, "Out of memory\n");
exit(EXIT_FAILURE);
}
for (i = 0; i < CAPTURE_BUFFER_NUMBER; ++i) {
struct v4l2_buffer buf;
CLEAR(buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i;
if(-1 == ioctl(fd, VIDIOC_QUERYBUF, &buf))
{
perror("Fail to ioctl : VIDIOC_QUERYBUF");
exit(EXIT_FAILURE);
}
cam_buffers[i].length = buf.length;
cam_buffers[i].start =
mmap(
NULL,/*start anywhere*/
buf.length,
PROT_READ | PROT_WRITE,
MAP_SHARED,
fd,buf.m.offset
);
if(MAP_FAILED == cam_buffers[i].start)
{
perror("Fail to mmap\n");
printf("%d\n",i);
exit(EXIT_FAILURE);
}
//printf("cam_capture rebuf::08%lx\n", (long unsigned int)cam_buffers[i].start);
}
printf("%s: -\n", __func__);
return true;
}
int Camera::cam_setrate()
{
int err;
struct v4l2_streamparm stream;
CLEAR(stream);
stream.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
stream.parm.capture.capturemode = 0;
stream.parm.capture.timeperframe.numerator = 1;
stream.parm.capture.timeperframe.denominator = FPS;
err = xioctl(fd, VIDIOC_S_PARM, &stream);
if(err < 0)
printf("FimcV4l2 start: error %d, VIDIOC_S_PARM", err);
return 0;
}
bool Camera::cam_setfmt(void) {
v4l2_input input;
memset(&input, 0, sizeof(struct v4l2_input));
input.index = 0;
if (xioctl(fd, VIDIOC_ENUMINPUT, &input) != 0) {
fprintf(stderr, "No matching index found\n");
return false;
}
if (!input.name) {
fprintf(stderr, "No matching index found\n");
return false;
}
if (xioctl(fd, VIDIOC_S_INPUT, &input) < 0) {
fprintf(stderr, "VIDIOC_S_INPUT failed\n");
return false;
}
/*******************查询当前摄像头所支持的输出格式************/
struct v4l2_fmtdesc fmt_aviable; //v4l2_fmtdesc : 帧格式结构体
int ret;
memset(&fmt_aviable, 0, sizeof(fmt_aviable));//将fmt1结构体填充为0
fmt_aviable.index = 0; //初始化为0,要查询的格式序号
fmt_aviable.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; // 数据流类型,必须永远是V4L2_BUF_TYPE_VIDEO_CAPTURE
while ((ret = xioctl(fd, VIDIOC_ENUM_FMT, &fmt_aviable)) == 0) //显示所有支持的格式
{
fmt_aviable.index++;
printf("{ pixelformat = '%c%c%c%c', description = '%s' }\n",fmt_aviable.pixelformat & 0xFF,
(fmt_aviable.pixelformat >> 8) & 0xFF,(fmt_aviable.pixelformat >> 16) & 0xFF,
(fmt_aviable.pixelformat >> 24) & 0xFF,fmt_aviable.description); // pixelformat;格式32位 description[32];// 格式名称8位
}
/********
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
YUYV422 to NV12(fimc).rar (6个子文件)
YUYV422 to NV12(fimc)
camera.h 1KB
camera.cpp 19KB
main.cpp 3KB
Makefile 552B
videodev2.h 99KB
compiler.h 8KB
共 6 条
- 1
资源评论
小葱_哥
- 粉丝: 10
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功