1 Star 0 Fork 26

nanhe01/demo_h264_rkmedia

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
h264_rkmedia_demo.c 4.63 KB
一键复制 编辑 原始数据 按行查看 历史
吴堉民 提交于 2020-11-23 18:59 . use RKAIQ
#include <assert.h>
#include <fcntl.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "common/sample_common.h"
#include "rkmedia_api.h"
#include "rkmedia_venc.h"
#if 0
#define DBG(...) do { fprintf(stderr, __VA_ARGS__); } while (0)
#else
#define DBG(...) do {} while (0)
#endif
#define ENABLE_SAVE
#ifdef ENABLE_SAVE
static FILE *g_save_file;
#endif
static MPP_CHN_S stSrcChn;
static MPP_CHN_S stDestChn;
static bool quit = false;
static void sigterm_handler(int sig)
{
fprintf(stderr, "signal %d\n", sig);
quit = true;
}
static void video_packet_cb(MEDIA_BUFFER mb)
{
DBG("Get Video Encoded packet:ptr:%p, fd:%d, size:%zu, mode:%d\n",
RK_MPI_MB_GetPtr(mb), RK_MPI_MB_GetFD(mb), RK_MPI_MB_GetSize(mb),
RK_MPI_MB_GetModeID(mb));
#ifdef ENABLE_SAVE
if (g_save_file)
fwrite(RK_MPI_MB_GetPtr(mb), 1, RK_MPI_MB_GetSize(mb), g_save_file);
#endif
RK_MPI_MB_ReleaseBuffer(mb);
}
int rkmedia_h264_enc_init(int argc, char **argv)
{
int ret = 0;
RK_MPI_SYS_Init();
#ifdef RKAIQ
rk_aiq_working_mode_t hdr_mode = RK_AIQ_WORKING_MODE_NORMAL;
RK_BOOL fec_enable = RK_FALSE;
int fps = 30;
char *iq_file_dir = NULL;
if ((argc > 1) && !strcmp(argv[1], "-h")) {
printf("\n\n/Usage:./%s [--aiq iq_file_dir]\n", argv[0]);
printf("\t --aiq iq_file_dir : init isp\n");
return -1;
}
if (argc == 3) {
if (strcmp(argv[1], "--aiq") == 0) {
iq_file_dir = argv[2];
}
}
SAMPLE_COMM_ISP_Init(hdr_mode, fec_enable, iq_file_dir);
SAMPLE_COMM_ISP_Run();
SAMPLE_COMM_ISP_SetFrameRate(fps);
#else
(void)argc;
(void)argv;
#endif
VI_CHN_ATTR_S vi_chn_attr;
vi_chn_attr.pcVideoNode = "rkispp_scale0";
vi_chn_attr.u32BufCnt = 4;
vi_chn_attr.u32Width = 1920;
vi_chn_attr.u32Height = 1080;
vi_chn_attr.enPixFmt = IMAGE_TYPE_NV12;
vi_chn_attr.enWorkMode = VI_WORK_MODE_NORMAL;
ret = RK_MPI_VI_SetChnAttr(0, 1, &vi_chn_attr);
ret |= RK_MPI_VI_EnableChn(0, 1);
if (ret) {
printf("TEST: ERROR: Create vi[0] error! code:%d\n", ret);
return -1;
}
VENC_CHN_ATTR_S venc_chn_attr;
venc_chn_attr.stVencAttr.enType = RK_CODEC_TYPE_H264;
venc_chn_attr.stVencAttr.imageType = IMAGE_TYPE_NV12;
venc_chn_attr.stVencAttr.u32PicWidth = 1920;
venc_chn_attr.stVencAttr.u32PicHeight = 1080;
venc_chn_attr.stVencAttr.u32VirWidth = 1920;
venc_chn_attr.stVencAttr.u32VirHeight = 1080;
venc_chn_attr.stVencAttr.u32Profile = 77;
venc_chn_attr.stRcAttr.enRcMode = VENC_RC_MODE_H264CBR;
venc_chn_attr.stRcAttr.stH264Cbr.u32Gop = 30;
venc_chn_attr.stRcAttr.stH264Cbr.u32BitRate = 2000000; // 2Mb
venc_chn_attr.stRcAttr.stH264Cbr.fr32DstFrameRateDen = 1;
venc_chn_attr.stRcAttr.stH264Cbr.fr32DstFrameRateNum = 30;
venc_chn_attr.stRcAttr.stH264Cbr.u32SrcFrameRateDen = 1;
venc_chn_attr.stRcAttr.stH264Cbr.u32SrcFrameRateNum = 30;
ret = RK_MPI_VENC_CreateChn(0, &venc_chn_attr);
if (ret) {
printf("TEST: ERROR: Create venc[0] error! code:%d\n", ret);
return -1;
}
#ifdef ENABLE_SAVE
g_save_file = fopen("/data/output.h264", "w");
if (!g_save_file)
printf("#VENC TEST:: Open /data/output.h264 failed!\n");
#endif
MPP_CHN_S stEncChn;
stEncChn.enModId = RK_ID_VENC;
stEncChn.s32DevId = 0;
stEncChn.s32ChnId = 0;
ret = RK_MPI_SYS_RegisterOutCb(&stEncChn, video_packet_cb);
if (ret) {
printf("TEST: ERROR: Register cb for venc[0] error! code:%d\n", ret);
return -1;
}
// Bind VI and VENC
// stSrcChn
stSrcChn.enModId = RK_ID_VI;
stSrcChn.s32DevId = 0;
stSrcChn.s32ChnId = 1;
// stDestChn
stDestChn.enModId = RK_ID_VENC;
stDestChn.s32DevId = 0;
stDestChn.s32ChnId = 0;
ret = RK_MPI_SYS_Bind(&stSrcChn, &stDestChn);
if (ret) {
printf("TEST: ERROR: Bind vi[0] to venc[0] error! code:%d\n", ret);
return -1;
}
printf("%s initial finish\n", __func__);
return 0;
}
void rkmedia_h264_enc_exit(void)
{
#ifdef ENABLE_SAVE
if (g_save_file) {
printf("#VENC TEST:: Close save file!\n");
fclose(g_save_file);
}
#endif
#ifdef RKAIQ
SAMPLE_COMM_ISP_Stop(); // isp aiq stop before vi streamoff
#endif
RK_MPI_SYS_UnBind(&stSrcChn, &stDestChn);
RK_MPI_VENC_DestroyChn(0);
RK_MPI_VI_DisableChn(0, 1);
printf("%s\n", __func__);
}
void demo_running(void)
{
signal(SIGINT, sigterm_handler);
printf("\n\n>>> demo_h264_rkmedia is running...\n");
printf("Press Ctrl + C to exit this program.\n\n");
while(!quit) {
usleep(1000);
}
}
int main(int argc, char **argv)
{
int ret;
ret = rkmedia_h264_enc_init(argc, argv);
if(ret) {
printf("rkmedia_h264_enc_init failed!\n");
return -1;
}
demo_running();
rkmedia_h264_enc_exit();
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/nanhe01/demo_h264_rkmedia.git
[email protected]:nanhe01/demo_h264_rkmedia.git
nanhe01
demo_h264_rkmedia
demo_h264_rkmedia
master

搜索帮助