rk3588 + ubuntu20.02 用mpp库编写视频采集及保存视频文件 C++代码
时间: 2024-03-20 08:39:14 浏览: 322
能新电极工具 V20.02(Free)安装包.zip
以下是一个简单的C++代码示例,用于使用MPP库在RK3588 + Ubuntu20.02上进行视频采集和保存视频文件:
```c++
#include <iostream>
#include <unistd.h>
#include "rockchip/rk_mpi.h"
#include "rockchip/mpp_buffer.h"
#include "rockchip/mpp_frame.h"
#include "rockchip/mpp_packet.h"
#include "rockchip/mpp_video.h"
using namespace std;
#define MAX_BUFFER_NUM 4
int main(int argc, char** argv) {
RK_U32 width = 1920;
RK_U32 height = 1080;
RK_U32 frame_rate = 30;
RK_U32 bit_rate = 2000000;
RK_U32 gop = 30;
RK_U32 fps_in = 30;
RK_U32 fps_out = 30;
RK_U32 rotate = 0;
RK_U32 mirror = 0;
/* 初始化 MPP 库 */
RK_MPI_SYS_Init();
/* 创建视频编码器 */
MPP_ENC_CONFIG enc_config;
memset(&enc_config, 0, sizeof(enc_config));
enc_config.format = MPP_FMT_YUV420SP;
enc_config.width = width;
enc_config.height = height;
enc_config.rc_mode = MPP_ENC_RC_MODE_CBR;
enc_config.bps = bit_rate;
enc_config.fps_in = fps_in;
enc_config.fps_out = fps_out;
enc_config.gop = gop;
enc_config.rotate = rotate;
enc_config.mirror = mirror;
MPP_RET ret;
MppCtx ctx;
ret = mpp_create(&ctx, &enc_config);
if (ret) {
cout << "Failed to create mpp context" << endl;
return 1;
}
/* 初始化编码器 */
ret = mpp_init(ctx);
if (ret) {
cout << "Failed to init mpp context" << endl;
return 1;
}
/* 创建输入帧 */
MppFrame frame_in;
ret = mpp_frame_init(&frame_in);
if (ret) {
cout << "Failed to init input frame" << endl;
return 1;
}
mpp_frame_set_fmt(frame_in, enc_config.format);
mpp_frame_set_width(frame_in, enc_config.width);
mpp_frame_set_height(frame_in, enc_config.height);
/* 创建编码输出帧 */
MppFrame frame_out;
ret = mpp_frame_init(&frame_out);
if (ret) {
cout << "Failed to init output frame" << endl;
return 1;
}
/* 创建编码输出数据包 */
MppPacket packet;
ret = mpp_packet_init(&packet, NULL, 0);
if (ret) {
cout << "Failed to init packet" << endl;
return 1;
}
/* 创建输入缓冲区 */
MppBufferGroup buf_grp_in;
ret = mpp_buffer_group_get_internal(&buf_grp_in, MPP_BUFFER_TYPE_ION);
if (ret) {
cout << "Failed to get input buffer group" << endl;
return 1;
}
ret = mpp_buffer_group_limit_config(buf_grp_in, MAX_BUFFER_NUM, 0);
if (ret) {
cout << "Failed to config input buffer group" << endl;
return 1;
}
/* 创建输出缓冲区 */
MppBufferGroup buf_grp_out;
ret = mpp_buffer_group_get_internal(&buf_grp_out, MPP_BUFFER_TYPE_ION);
if (ret) {
cout << "Failed to get output buffer group" << endl;
return 1;
}
ret = mpp_buffer_group_limit_config(buf_grp_out, MAX_BUFFER_NUM, 0);
if (ret) {
cout << "Failed to config output buffer group" << endl;
return 1;
}
/* 打开输入设备 */
MppCtx input_ctx;
MppParam param = NULL;
ret = mpp_open(&input_ctx, MPP_CTX_DEC, param);
if (ret) {
cout << "Failed to open input device" << endl;
return 1;
}
/* 打开输出设备 */
MppCtx output_ctx;
ret = mpp_open(&output_ctx, MPP_CTX_ENC, param);
if (ret) {
cout << "Failed to open output device" << endl;
return 1;
}
/* 开始采集 */
MppBuffer buffer = NULL;
MppPacket packet_out = NULL;
MppFrame frame = NULL;
while (1) {
/* 从输入设备读取数据 */
ret = mpp_device_poll(input_ctx, MPP_POLL_BLOCK);
if (ret) {
cout << "Failed to poll input device" << endl;
break;
}
ret = mpp_device_dequeue(input_ctx, &frame);
if (ret) {
cout << "Failed to dequeue input frame" << endl;
break;
}
/* 将输入帧存储到输入缓冲区 */
buffer = mpp_buffer_group_get(buf_grp_in);
if (buffer == NULL) {
cout << "Failed to get input buffer" << endl;
break;
}
mpp_frame_set_buffer(frame_in, buffer);
ret = mpp_buffer_write(buffer, 0, mpp_frame_get_ptr(frame), mpp_frame_get_size(frame));
if (ret) {
cout << "Failed to write data to input buffer" << endl;
break;
}
/* 进行视频编码 */
ret = mpp_encode_put_frame(ctx, frame_in);
if (ret) {
cout << "Failed to put input frame to encoder" << endl;
break;
}
ret = mpp_encode_get_packet(ctx, &packet_out);
if (ret) {
cout << "Failed to get output packet" << endl;
break;
}
/* 将编码后的数据存储到输出缓冲区 */
buffer = mpp_buffer_group_get(buf_grp_out);
if (buffer == NULL) {
cout << "Failed to get output buffer" << endl;
break;
}
ret = mpp_packet_write(packet_out, 0, mpp_buffer_get_ptr(buffer), mpp_buffer_get_size(buffer));
if (ret) {
cout << "Failed to write data to output buffer" << endl;
break;
}
/* 将输出缓冲区的数据写入保存的视频文件中 */
// 这里省略了将数据写入文件的代码
/* 释放输入帧和输出数据包 */
mpp_frame_deinit(&frame);
mpp_packet_deinit(&packet_out);
}
/* 关闭输入和输出设备 */
mpp_close(input_ctx);
mpp_close(output_ctx);
/* 销毁 MPP 组件 */
mpp_buffer_group_put(buf_grp_in);
mpp_buffer_group_put(buf_grp_out);
mpp_packet_deinit(&packet);
mpp_frame_deinit(&frame_out);
mpp_frame_deinit(&frame_in);
mpp_destroy(ctx);
RK_MPI_SYS_Exit();
return 0;
}
```
请注意,此示例仅用于演示如何使用MPP库进行视频采集和编码,它并没有包含完整的代码,例如文件读写和错误处理等。在实际的应用中,你需要根据自己的需求进行修改和完善。
阅读全文