camera_source.h
#ifndef __CAMERA_SOURCE_H__
#define __CAMERA_SOURCE_H__
#include "mpp_frame.h"
typedef struct CamSource CamSource;
#ifdef __cplusplus
extern "C" {
#endif
// Create a new context to capture frames from <fname>. Returns NULL on error.
CamSource* camera_source_init(const char *device, RK_U32 bufcnt, RK_U32 width, RK_U32 height, MppFrameFormat fmt);
// Stop capturing and free a context.
MPP_RET camera_source_deinit(CamSource *ctx);
// Returns the next captured frame and its meta-data.
RK_S32 camera_source_get_frame(CamSource *ctx);
// Tells the kernel it's OK to overwrite a frame captured
MPP_RET camera_source_put_frame(CamSource *ctx, RK_S32 idx);
MppBuffer camera_frame_to_buf(CamSource *ctx, RK_S32 idx);
#ifdef __cplusplus
}
#endif
#endif /* __CAMERA_SOURCE_H__ */
camera_source.c
#define MODULE_TAG "camera_source"
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/select.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>
#include "mpp_log.h"
#include "mpp_mem.h"
#include "camera_source.h"
typedef struct CamFrame_t {
void *start;
size_t length;
RK_S32 export_fd;
RK_S32 sequence;
MppBuffer buffer;
} CamFrame;
struct CamSource {
RK_S32 fd; // Device handle
RK_U32 bufcnt; // # of buffers
enum v4l2_buf_type type;
MppFrameFormat fmt;
CamFrame fbuf[10];// frame buffers
};
static RK_U32 V4L2_yuv_cfg[MPP_FMT_YUV_BUTT] = {
V4L2_PIX_FMT_NV12,
0,
V4L2_PIX_FMT_NV16,
0,
V4L2_PIX_FMT_YVU420,
V4L2_PIX_FMT_NV21,
V4L2_PIX_FMT_YUV422P,
V4L2_PIX_FMT_NV61,
V4L2_PIX_FMT_YUYV,
V4L2_PIX_FMT_YVYU,
V4L2_PIX_FMT_UYVY,
V4L2_PIX_FMT_VYUY,
V4L2_PIX_FMT_GREY,
0,
0,
0,
};
static RK_U32 V4L2_RGB_cfg[MPP_FMT_RGB_BUTT - MPP_FRAME_FMT_RGB] = {
V4L2_PIX_FMT_RGB565,
0,
V4L2_PIX_FMT_RGB555,
0,
V4L2_PIX_FMT_RGB444,
0,
V4L2_PIX_FMT_RGB24,
V4L2_PIX_FMT_BGR24,
0,
0,
V4L2_PIX_FMT_RGB32,
V4L2_PIX_FMT_BGR32,
0,
0,
};
#define FMT_NUM_PLANES 1
// Wrap ioctl() to spin on EINTR
static RK_S32 camera_source_ioctl(RK_S32 fd, RK_S32 req, void* arg)