技术标签: C++ c++ RK驱动开发 linux系统编程 数据结构
RK3568 重新封装V4l2为一个C++ Camera管理类 (基于RK编译环境,纯linux环境得小伙伴可以自己阅读代码做出对应得修改主要修改(CameraReader.cpp CameraReader,h thread.h))
CameraReader.cpp
/*
* Copyright 2015 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define MODULE_TAG "CameraReader"
#include "mpp_log.h"
#include "mpp_mem.h"
#include "CameraReader.h"
CameraReader::CameraReader(const char *device, RK_U32 bufcnt, RK_U32 width, RK_U32 height, MppFrameFormat fmt)
:thread(device),
mBufcnt(bufcnt),
mWidth(width),
mHeight(height),
mFmt(fmt)
{
strcpy(mDevice,device);
}
// Wrap ioctl() to spin on EINTR
RK_S32 CameraReader::Ioctl(RK_S32 fd, RK_S32 req, void* arg)
{
struct timespec poll_time;
RK_S32 ret;
while ((ret = ioctl(fd, req, arg))) {
if (ret == -1 && (EINTR != errno && EAGAIN != errno)) {
// mpp_err("ret = %d, errno %d", ret, errno);
break;
}
// 10 milliseconds
poll_time.tv_sec = 0;
poll_time.tv_nsec = 10000000;
nanosleep(&poll_time, NULL);
}
return ret;
}
// Create a new context to capture frames from <fname>.
// Returns NULL on error.
RK_S32 CameraReader::Init(const char *device, RK_U32 bufcnt, RK_U32 width, RK_U32 height, MppFrameFormat format)
{
struct v4l2_capability cap;
struct v4l2_format vfmt;
struct v4l2_requestbuffers req;
struct v4l2_buffer buf;
enum v4l2_buf_type type;
RK_U32 i;
RK_U32 buf_len = 0;
mCtx = mpp_calloc(CamSource, 1);
if (!mCtx)
return -1;
mCtx->bufcnt = bufcnt;
mCtx->fd = open(device, O_RDWR, 0);
if (mCtx->fd < 0) {
mpp_err_f("Cannot open device\n");
goto FAIL;
}
// Determine if fd is a V4L2 Device
if (0 != Ioctl(mCtx->fd, VIDIOC_QUERYCAP, &cap)) {
mpp_err_f("Not v4l2 compatible\n");
goto FAIL;
}
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) && !(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE)) {
mpp_err_f("Capture not supported\n");
goto FAIL;
}
if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
mpp_err_f("Streaming IO Not Supported\n");
goto FAIL;
}
// Preserve original settings as set by v4l2-ctl for example
vfmt = (struct v4l2_format) {0};
vfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
vfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
vfmt.fmt.pix.width = width;
vfmt.fmt.pix.height = height;
if (MPP_FRAME_FMT_IS_YUV(format)) {
vfmt.fmt.pix.pixelformat = V4L2_yuv_cfg[format - MPP_FRAME_FMT_YUV];
} else if (MPP_FRAME_FMT_IS_RGB(format)) {
vfmt.fmt.pix.pixelformat = V4L2_RGB_cfg[format - MPP_FRAME_FMT_RGB];
}
if (!vfmt.fmt.pix.pixelformat)
vfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_NV12;
type = (v4l2_buf_type) vfmt.type;
mCtx->type = (v4l2_buf_type) vfmt.type;
if (-1 == Ioctl(mCtx->fd, VIDIOC_S_FMT, &vfmt)) {
mpp_err_f("VIDIOC_S_FMT\n");
goto FAIL;
}
if (-1 == Ioctl(mCtx->fd, VIDIOC_G_FMT, &vfmt)) {
mpp_err_f("VIDIOC_G_FMT\n");
goto FAIL;
}
mpp_log("get width %d height %d", vfmt.fmt.pix.width, vfmt.fmt.pix.height);
// Request memory-mapped buffers
req = (struct v4l2_requestbuffers) {0};
req.count = mCtx->bufcnt;
req.type = type;
req.memory = V4L2_MEMORY_MMAP;
if (-1 == Ioctl(mCtx->fd, VIDIOC_REQBUFS, &req)) {
mpp_err_f("Device does not support mmap\n");
goto FAIL;
}
if (req.count != mCtx->bufcnt) {
mpp_err_f("Device buffer count mismatch\n");
goto FAIL;
}
// mmap() the buffers into userspace memory
for (i = 0 ; i < mCtx->bufcnt; i++) {
buf = (struct v4l2_buffer) {0};
buf.type = type;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i;
struct v4l2_plane planes[FMT_NUM_PLANES];
buf.memory = V4L2_MEMORY_MMAP;
if (V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE == type) {
buf.m.planes = planes;
buf.length = FMT_NUM_PLANES;
}
if (-1 == Ioctl(mCtx->fd, VIDIOC_QUERYBUF, &buf)) {
mpp_err_f("ERROR: VIDIOC_QUERYBUF\n");
goto FAIL;
}
mCtx->fbuf[i].start = mmap(NULL, buf.length,
PROT_READ | PROT_WRITE, MAP_SHARED,
mCtx->fd, buf.m.offset);
if (V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE == buf.type) {
// tmp_buffers[n_buffers].length = buf.m.planes[0].length;
buf_len = buf.m.planes[0].length;
mCtx->fbuf[i].start =
mmap(NULL /* start anywhere */,
buf.m.planes[0].length,
PROT_READ | PROT_WRITE /* required */,
MAP_SHARED /* recommended */,
mCtx->fd, buf.m.planes[0].m.mem_offset);
} else {
buf_len = buf.length;
mCtx->fbuf[i].start =
mmap(NULL /* start anywhere */,
buf.length,
PROT_READ | PROT_WRITE /* required */,
MAP_SHARED /* recommended */,
mCtx->fd, buf.m.offset);
}
if (MAP_FAILED == mCtx->fbuf[i].start) {
mpp_err_f("ERROR: Failed to map device frame buffers\n");
goto FAIL;
}
struct v4l2_exportbuffer expbuf = (struct v4l2_exportbuffer) {0} ;
// xcam_mem_clear (expbuf);
expbuf.type = type;
expbuf.index = i;
expbuf.flags = O_CLOEXEC;
if (Ioctl(mCtx->fd, VIDIOC_EXPBUF, &expbuf) < 0) {
mpp_err_f("get dma buf failed\n");
goto FAIL;
} else {
mpp_log("get dma buf(%d)-fd: %d\n", i, expbuf.fd);
MppBufferInfo info;
memset(&info, 0, sizeof(MppBufferInfo));
info.type = MPP_BUFFER_TYPE_EXT_DMA;
info.fd = expbuf.fd;
info.size = buf_len & 0x07ffffff;
info.index = (buf_len & 0xf8000000) >> 27;
mpp_buffer_import(&mCtx->fbuf[i].buffer, &info);
}
mCtx->fbuf[i].export_fd = expbuf.fd;
}
for (i = 0; i < mCtx->bufcnt; i++ ) {
struct v4l2_plane planes[FMT_NUM_PLANES];
buf = (struct v4l2_buffer) {0};
buf.type = type;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i;
buf.memory = V4L2_MEMORY_MMAP;
if (V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE == type) {
buf.m.planes = planes;
buf.length = FMT_NUM_PLANES;
}
if (-1 == Ioctl(mCtx->fd, VIDIOC_QBUF, &buf)) {
mpp_err_f("ERROR: VIDIOC_QBUF %d\n", i);
Deinit(mCtx);
goto FAIL;
}
}
// Start capturing
if (-1 == Ioctl(mCtx->fd, VIDIOC_STREAMON, &type)) {
mpp_err_f("ERROR: VIDIOC_STREAMON\n");
Deinit(mCtx);
goto FAIL;
}
//skip some frames at start
for (i = 0; i < mCtx->bufcnt; i++ ) {
RK_S32 idx = GetFrame(mCtx);
if (idx >= 0)
PutFrame(mCtx, idx);
}
code = new RkH264Encode();
code->start();
return MPP_OK;
FAIL:
Deinit(mCtx);
mCtx = NULL;
return MPP_NOK;
}
// Free a context to capture frames from <fname>.
// Returns NULL on error.
MPP_RET CameraReader::Deinit(CamSource *ctx)
{
struct v4l2_buffer buf;
enum v4l2_buf_type type;
RK_U32 i;
if (NULL == ctx)
return MPP_OK;
if (ctx->fd < 0)
return MPP_OK;
// Stop capturing
type = ctx->type;
Ioctl(ctx->fd, VIDIOC_STREAMOFF, &type);
// un-mmap() buffers
for (i = 0 ; i < ctx->bufcnt; i++) {
buf = (struct v4l2_buffer) {0};
buf.type = type;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i;
Ioctl(ctx->fd, VIDIOC_QUERYBUF, &buf);
if (ctx->fbuf[buf.index].buffer) {
mpp_buffer_put(ctx->fbuf[buf.index].buffer);
}
munmap(ctx->fbuf[buf.index].start, buf.length);
}
// Close v4l2 device
close(ctx->fd);
MPP_FREE(ctx);
return MPP_OK;
}
// Returns a pointer to a captured frame and its meta-data. NOT thread-safe.
RK_S32 CameraReader::GetFrame(CamSource *ctx)
{
struct v4l2_buffer buf;
enum v4l2_buf_type type;
type = ctx->type;
buf = (struct v4l2_buffer) {0};
buf.type = type;
buf.memory = V4L2_MEMORY_MMAP;
struct v4l2_plane planes[FMT_NUM_PLANES];
if (V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE == type) {
buf.m.planes = planes;
buf.length = FMT_NUM_PLANES;
}
if (-1 == Ioctl(ctx->fd, VIDIOC_DQBUF, &buf)) {
mpp_err_f("VIDIOC_DQBUF\n");
return MPP_NOK;
}
if (buf.index > ctx->bufcnt) {
mpp_err_f("buffer index out of bounds\n");
return MPP_NOK;
}
if (V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE == type)
buf.bytesused = buf.m.planes[0].bytesused;
return buf.index;
}
// It's OK to capture into this framebuffer now
MPP_RET CameraReader::PutFrame(CamSource *ctx, RK_S32 idx)
{
struct v4l2_buffer buf;
enum v4l2_buf_type type;
if (idx < 0)
return MPP_OK;
type = ctx->type;
buf = (struct v4l2_buffer) {0};
buf.type = type;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = idx;
struct v4l2_plane planes[FMT_NUM_PLANES];
if (V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE == type) {
buf.m.planes = planes;
buf.length = FMT_NUM_PLANES;
}
// Tell kernel it's ok to overwrite this frame
if (-1 == Ioctl(ctx->fd, VIDIOC_QBUF, &buf)) {
mpp_err_f("VIDIOC_QBUF\n");
return MPP_OK;
}
return MPP_OK;
}
MppBuffer CameraReader::Frame2Buf(CamSource *ctx, RK_S32 idx)
{
if (idx < 0)
return NULL;
return ctx->fbuf[idx].buffer;
}
RK_S32 CameraReader::Open(){
return Init(mDevice, mBufcnt,mWidth, mHeight, mFmt);
}
bool CameraReader::readyToRun(){
return Open()==MPP_OK;
}
void CameraReader::start(){
run();//线程开始跑
}
//线程主体函数
bool CameraReader::threadLoop(){
//printf("threadLoop\n");
static FILE *fp = NULL;
static int count = 0;
int index = GetFrame(mCtx);
if(index == MPP_NOK){
usleep(2000);
return true;
}
Frame2Buf(mCtx,index)//在这里出数据
/×code->write();
while(code->dealImageCompletes()){
usleep(10);
}×/
PutFrame(mCtx,index);
return true;
}
CameraReader.h
/*
* Copyright 2015 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __CAMERA_READER_H__
#define __CAMERA_READER_H__
#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_frame.h"
#include "thread.h"
#include "RkH264Encode.h"
typedef struct CamSource CamSource;
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
class CameraReader : public threadBase{
private:
CamSource *mCtx = NULL;
char mDevice[20];
RK_U32 mBufcnt;
RK_U32 mWidth;
RK_U32 mHeight;
MppFrameFormat mFmt;
RkH264Encode *code;
// Create a new context to capture frames from <fname>. Returns NULL on error.
RK_S32 Init(const char *device, RK_U32 bufcnt, RK_U32 width, RK_U32 height, MppFrameFormat fmt);
RK_S32 Open();
// Stop capturing and free a context.
MPP_RET Deinit(CamSource *ctx);
RK_S32 Ioctl(RK_S32 fd, RK_S32 req, void* arg);
// Returns the next captured frame and its meta-data.
RK_S32 GetFrame(CamSource *ctx);
// Tells the kernel it's OK to overwrite a frame captured
MPP_RET PutFrame(CamSource *ctx, RK_S32 idx);
MppBuffer Frame2Buf(CamSource *ctx, RK_S32 idx);
bool readyToRun();
bool threadLoop();
public:
CameraReader(const char *device, RK_U32 bufcnt, RK_U32 width, RK_U32 height, MppFrameFormat fmt);
void start();
};
#endif /* __CAMERA_READER_H__ */
main_test
#include <string.h>
#include "rk_mpi.h"
#include "mpp_env.h"
#include "mpp_mem.h"
#include "mpp_time.h"
#include "mpp_debug.h"
#include "mpp_common.h"
#include "CameraReader.h"
int main(){
CameraReader *camera1 = new CameraReader("/dev/video0", 10, 1920, 1080, MPP_FMT_YUV420SP);
camera1->start();//线程开始
while(1){
usleep(33333);
}
}
mpp/mpp-develop/test/CMakeLists.txt
....
# new dec multi unit test
add_mpp_test(mpi_dec_multi c)
#add
add_mpp_test(main cpp)
...
mpp/mpp-develop/util/CMakeLists.txt
# vim: syntax=cmake
# ----------------------------------------------------------------------------
# add libvpu implement
# ----------------------------------------------------------------------------
include_directories(${PROJECT_SOURCE_DIR}/mpp/base/inc)
add_library(utils STATIC
mpp_enc_roi_utils.c
mpi_enc_utils.c
mpi_dec_utils.c
mpp_opt.c
utils.c
iniparser.c
dictionary.c
camera_source.c
CameraReader.cpp
thread.cpp
thread.h
)
target_link_libraries(utils mpp_base)
此类继承于threadBase在我上一片文章有linux下 C++ 封装一个简洁管理方便线程类_hmbbPdx_的博客-程序员宅基地
文章浏览阅读1.6k次。安装配置gi、安装数据库软件、dbca建库见下:http://blog.csdn.net/kadwf123/article/details/784299611、检查集群节点及状态:[root@rac2 ~]# olsnodes -srac1 Activerac2 Activerac3 Activerac4 Active[root@rac2 ~]_12c查看crs状态
文章浏览阅读1.3w次,点赞45次,收藏99次。我个人用的是anaconda3的一个python集成环境,自带jupyter notebook,但在我打开jupyter notebook界面后,却找不到对应的虚拟环境,原来是jupyter notebook只是通用于下载anaconda时自带的环境,其他环境要想使用必须手动下载一些库:1.首先进入到自己创建的虚拟环境(pytorch是虚拟环境的名字)activate pytorch2.在该环境下下载这个库conda install ipykernelconda install nb__jupyter没有pytorch环境
文章浏览阅读5.2k次,点赞19次,收藏28次。选择scoop纯属意外,也是无奈,因为电脑用户被锁了管理员权限,所有exe安装程序都无法安装,只可以用绿色软件,最后被我发现scoop,省去了到处下载XXX绿色版的烦恼,当然scoop里需要管理员权限的软件也跟我无缘了(譬如everything)。推荐添加dorado这个bucket镜像,里面很多中文软件,但是部分国外的软件下载地址在github,可能无法下载。以上两个是官方bucket的国内镜像,所有软件建议优先从这里下载。上面可以看到很多bucket以及软件数。如果官网登陆不了可以试一下以下方式。_scoop-cn
文章浏览阅读4.5k次,点赞2次,收藏3次。首先要有一个color-picker组件 <el-color-picker v-model="headcolor"></el-color-picker>在data里面data() { return {headcolor: ’ #278add ’ //这里可以选择一个默认的颜色} }然后在你想要改变颜色的地方用v-bind绑定就好了,例如:这里的:sty..._vue el-color-picker
文章浏览阅读640次。基于芯片日益增长的问题,所以内核开发者们引入了新的方法,就是在内核中只保留函数,而数据则不包含,由用户(应用程序员)自己把数据按照规定的格式编写,并放在约定的地方,为了不占用过多的内存,还要求数据以根精简的方式编写。boot启动时,传参给内核,告诉内核设备树文件和kernel的位置,内核启动时根据地址去找到设备树文件,再利用专用的编译器去反编译dtb文件,将dtb还原成数据结构,以供驱动的函数去调用。firmware是三星的一个固件的设备信息,因为找不到固件,所以内核启动不成功。_exynos 4412 刷机
文章浏览阅读2w次,点赞24次,收藏42次。Linux系统配置jdkLinux学习教程,Linux入门教程(超详细)_linux配置jdk
文章浏览阅读3.3k次,点赞5次,收藏19次。xlabel('\delta');ylabel('AUC');具体符号的对照表参照下图:_matlab微米怎么输入
文章浏览阅读119次。顺序读写指的是按照文件中数据的顺序进行读取或写入。对于文本文件,可以使用fgets、fputs、fscanf、fprintf等函数进行顺序读写。在C语言中,对文件的操作通常涉及文件的打开、读写以及关闭。文件的打开使用fopen函数,而关闭则使用fclose函数。在C语言中,可以使用fread和fwrite函数进行二进制读写。 Biaoge 于2024-03-09 23:51发布 阅读量:7 ️文章类型:【 C语言程序设计 】在C语言中,用于打开文件的函数是____,用于关闭文件的函数是____。
文章浏览阅读3.4k次,点赞2次,收藏13次。跟随鼠标移动的粒子以grid(SOP)为partical(SOP)的资源模板,调整后连接【Geo组合+point spirit(MAT)】,在连接【feedback组合】适当调整。影响粒子动态的节点【metaball(SOP)+force(SOP)】添加mouse in(CHOP)鼠标位置到metaball的坐标,实现鼠标影响。..._touchdesigner怎么让一个模型跟着鼠标移动
文章浏览阅读178次。项目运行环境配置:Jdk1.8 + Tomcat7.0 + Mysql + HBuilderX(Webstorm也行)+ Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)。项目技术:Springboot + mybatis + Maven +mysql5.7或8.0+html+css+js等等组成,B/S模式 + Maven管理等等。环境需要1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。_基于java技术的停车场管理系统实现与设计
文章浏览阅读3.5k次。前言对于MediaPlayer播放器的源码分析内容相对来说比较多,会从Java-&amp;gt;Jni-&amp;gt;C/C++慢慢分析,后面会慢慢更新。另外,博客只作为自己学习记录的一种方式,对于其他的不过多的评论。MediaPlayerDemopublic class MainActivity extends AppCompatActivity implements SurfaceHolder.Cal..._android多媒体播放源码分析 时序图
文章浏览阅读2.4k次,点赞41次,收藏13次。java 数据结构与算法 ——快速排序法_快速排序法