瑞芯微 多路视频硬解码 代码
时间: 2023-09-18 20:10:26 浏览: 286
瑞芯微多路视频硬解码通常需要使用相关的SDK和驱动程序来实现。下面提供一个简单的代码示例,演示如何使用RK3399处理器进行H.264视频硬解码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "rockchip/rk_mpi.h"
#include "rockchip/rk_type.h"
#define IN_FILE "/path/to/input.h264"
#define OUT_FILE "/path/to/output.yuv"
#define WIDTH 1920
#define HEIGHT 1080
int main(int argc, char **argv) {
MPP_RET ret;
MPP_CTX mpp_ctx = NULL;
MppApi *mpi = NULL;
FILE *in_file = NULL, *out_file = NULL;
RK_U8 *buf = NULL;
MppBuffer pkt_buf = NULL;
MppBuffer frm_buf = NULL;
MppPacket pkt = NULL;
MppFrame frm = NULL;
RK_U32 pkt_eos = 0, frm_eos = 0;
RK_U32 pkt_size = 0, frm_size = 0;
RK_U32 width = WIDTH, height = HEIGHT;
RK_U32 hor_stride = 0, ver_stride = 0;
// 初始化MPP上下文
ret = mpp_create(&mpp_ctx, &mpi);
if (ret != MPP_OK) {
printf("mpp_create failed: %d\n", ret);
return -1;
}
// 配置解码参数
MppCodingType type = MPP_VIDEO_CodingAVC;
MppFrameFormat fmt = MPP_FMT_YUV420SP;
MppPacketCoding pkt_type = MPP_VIDEO_CodingAVC;
MppFrameCoding frm_type = MPP_VIDEO_CodingI420;
MppEncConfig enc_conf;
memset(&enc_conf, 0, sizeof(enc_conf));
enc_conf.coding = type;
enc_conf.width = width;
enc_conf.height = height;
enc_conf.hor_stride = hor_stride;
enc_conf.ver_stride = ver_stride;
enc_conf.fps_in = 30;
enc_conf.fps_out = 30;
enc_conf.qp_init = 0;
enc_conf.qp_max = 51;
enc_conf.qp_min = 0;
enc_conf.rc_mode = MPP_ENC_RC_MODE_CBR;
enc_conf.bps_target = 1024 * 1024;
enc_conf.profile = MPP_PROFILE_AVC_HIGH;
enc_conf.level = MPP_LEVEL_UNKNOWN;
enc_conf.gop = 30;
enc_conf.skip_cnt = 0;
enc_conf.max_key_interval = 30;
enc_conf.ref_num = 1;
enc_conf.i_frame_qp_delta = 0;
enc_conf.b_frame_qp_delta = 0;
enc_conf.init_vbv_buffer_size = 1024 * 1024;
enc_conf.vbv_buffer_size = 1024 * 1024;
enc_conf.vbv_buffer_delay = 1000;
// 初始化解码器
ret = mpi->control(mpp_ctx, MPP_DEC_SET_OUTPUT_FORMAT, &fmt);
if (ret != MPP_OK) {
printf("mpi->control MPP_DEC_SET_OUTPUT_FORMAT failed: %d\n", ret);
goto end;
}
ret = mpi->control(mpp_ctx, MPP_DEC_SET_CodingType, &type);
if (ret != MPP_OK) {
printf("mpi->control MPP_DEC_SET_CodingType failed: %d\n", ret);
goto end;
}
ret = mpi->control(mpp_ctx, MPP_DEC_SET_FRAME_INFO, &enc_conf);
if (ret != MPP_OK) {
printf("mpi->control MPP_DEC_SET_FRAME_INFO failed: %d\n", ret);
goto end;
}
// 打开输入文件和输出文件
in_file = fopen(IN_FILE, "rb");
out_file = fopen(OUT_FILE, "wb");
if (!in_file || !out_file) {
printf("open file failed\n");
goto end;
}
// 解码循环
while (!pkt_eos || !frm_eos) {
if (!pkt_eos) {
// 读取输入数据
if (!pkt_buf) {
ret = mpi->control(mpp_ctx, MPP_DEC_GET_PKT_BUF, &pkt_buf);
if (ret != MPP_OK) {
printf("mpi->control MPP_DEC_GET_PKT_BUF failed: %d\n", ret);
goto end;
}
}
buf = (RK_U8*)mpp_buffer_get_ptr(pkt_buf);
pkt_size = fread(buf, 1, mpp_buffer_get_size(pkt_buf), in_file);
if (pkt_size == 0) {
pkt_eos = 1;
}
// 构造输入数据包
ret = mpp_packet_init(&pkt, buf, pkt_size);
if (ret != MPP_OK) {
printf("mpp_packet_init failed: %d\n", ret);
goto end;
}
mpp_packet_set_length(pkt, pkt_size);
mpp_packet_set_pts(pkt, 0);
mpp_packet_set_dts(pkt, 0);
}
if (!frm_eos) {
if (!frm_buf) {
// 申请输出缓冲区
ret = mpi->control(mpp_ctx, MPP_DEC_GET_FRAME_BUF, &frm_buf);
if (ret != MPP_OK) {
printf("mpi->control MPP_DEC_GET_FRAME_BUF failed: %d\n", ret);
goto end;
}
}
// 解码数据包
ret = mpi->decode_put_packet(mpp_ctx, pkt);
if (ret != MPP_OK) {
printf("mpi->decode_put_packet failed: %d\n", ret);
goto end;
}
ret = mpi->decode_get_frame(mpp_ctx, &frm);
if (ret != MPP_OK) {
if (ret == MPP_NOK_ENOMEM) {
// 缓冲区不足,继续循环
usleep(1000);
continue;
} else if (ret == MPP_OK || ret == MPP_ERR_TIMEOUT) {
// 解码成功或者超时,继续循环
usleep(1000);
continue;
} else {
printf("mpi->decode_get_frame failed: %d\n", ret);
goto end;
}
}
// 将解码后的数据写入输出文件
buf = (RK_U8*)mpp_buffer_get_ptr(frm_buf);
frm_size = mpp_frame_get_size(frm);
fwrite(buf, 1, frm_size, out_file);
}
}
end:
// 释放资源
if (mpi) {
mpi->reset(mpp_ctx);
mpp_destroy(mpp_ctx);
}
if (in_file) {
fclose(in_file);
}
if (out_file) {
fclose(out_file);
}
if (pkt_buf) {
mpp_buffer_put(pkt_buf);
}
if (pkt) {
mpp_packet_deinit(&pkt);
}
if (frm_buf) {
mpp_buffer_put(frm_buf);
}
if (frm) {
mpp_frame_deinit(&frm);
}
return 0;
}
```
需要注意的是,这只是一个简单的示例代码,实际使用时需要根据具体的需求和硬件平台进行相应的修改和优化。
阅读全文