#include #include #include #include #include #include #include #include #include #include "rk_debug.h" #include "rk_defines.h" #include "rk_mpi_adec.h" #include "rk_mpi_aenc.h" #include "rk_mpi_ai.h" #include "rk_mpi_ao.h" #include "rk_mpi_avs.h" #include "rk_mpi_cal.h" #include "rk_mpi_ivs.h" #include "rk_mpi_mb.h" #include "rk_mpi_rgn.h" #include "rk_mpi_sys.h" #include "rk_mpi_tde.h" #include "rk_mpi_vdec.h" #include "rk_mpi_venc.h" #include "rk_mpi_vi.h" #include "rk_mpi_vo.h" #include "rk_mpi_vpss.h" static RK_S32 g_s32FrameCnt = -1; static bool quit = false; FILE *file = NULL; static RK_CHAR *g_pOutPath = "/tmp/"; static void sigterm_handler(int sig) { fprintf(stderr, "signal %d\n", sig); quit = true; } RK_U64 TEST_COMM_GetNowUs() { struct timespec time = {0, 0}; clock_gettime(CLOCK_MONOTONIC, &time); return (RK_U64)time.tv_sec * 1000000 + (RK_U64)time.tv_nsec / 1000; /* microseconds */ } static void *GetMediaBuffer0(void *arg) { (void)arg; printf("========%s========\n", __func__); void *pData = RK_NULL; int s32Ret; static RK_U32 jpeg_id = 0; char jpeg_path[128]; VENC_STREAM_S stFrame; stFrame.pstPack = malloc(sizeof(VENC_PACK_S)); while (!quit) { memset(jpeg_path, 0, sizeof(jpeg_path)); sprintf(jpeg_path, "%s/test_jpeg%d.jpeg", g_pOutPath, jpeg_id); file = fopen(jpeg_path, "w"); s32Ret = RK_MPI_VENC_GetStream(0, &stFrame, 500); if (s32Ret == RK_SUCCESS) { if (file) { pData = RK_MPI_MB_Handle2VirAddr(stFrame.pstPack->pMbBlk); fwrite(pData, 1, stFrame.pstPack->u32Len, file); fflush(file); } RK_U64 nowUs = TEST_COMM_GetNowUs(); RK_LOGD("chn:0, jpeg_id:%d enc->seq:%d wd:%d pts=%lld delay=%lldus\n", jpeg_id, stFrame.u32Seq, stFrame.pstPack->u32Len, stFrame.pstPack->u64PTS, nowUs - stFrame.pstPack->u64PTS); s32Ret = RK_MPI_VENC_ReleaseStream(0, &stFrame); if (s32Ret != RK_SUCCESS) { RK_LOGE("RK_MPI_VENC_ReleaseStream fail %x", s32Ret); } jpeg_id++; } } free(stFrame.pstPack); return NULL; } static RK_S32 test_venc_init(int chnId, int width, int height, RK_CODEC_ID_E enType) { printf("========%s========\n", __func__); VENC_RECV_PIC_PARAM_S stRecvParam; VENC_CHN_ATTR_S stAttr; VENC_CHN_PARAM_S stParam; memset(&stAttr, 0, sizeof(VENC_CHN_ATTR_S)); memset(&stParam, 0, sizeof(VENC_CHN_PARAM_S)); stAttr.stVencAttr.enType = enType; stAttr.stVencAttr.enPixelFormat = RK_FMT_YUV420SP; stAttr.stVencAttr.u32PicWidth = width; stAttr.stVencAttr.u32PicHeight = height; stAttr.stVencAttr.u32VirWidth = width; stAttr.stVencAttr.u32VirHeight = height; stAttr.stVencAttr.u32StreamBufCnt = 2; stAttr.stVencAttr.u32BufSize = width * height * 3 / 2; stAttr.stVencAttr.enMirror = MIRROR_NONE; stAttr.stVencAttr.stAttrJpege.bSupportDCF = RK_FALSE; stAttr.stVencAttr.stAttrJpege.stMPFCfg.u8LargeThumbNailNum = 0; stAttr.stVencAttr.stAttrJpege.enReceiveMode = VENC_PIC_RECEIVE_SINGLE; RK_MPI_VENC_CreateChn(chnId, &stAttr); stParam.stFrameRate.bEnable = RK_FALSE; stParam.stFrameRate.s32SrcFrmRateNum = 25; stParam.stFrameRate.s32SrcFrmRateDen = 1; stParam.stFrameRate.s32DstFrmRateNum = 10; stParam.stFrameRate.s32DstFrmRateDen = 1; RK_MPI_VENC_SetChnParam(chnId, &stParam); memset(&stRecvParam, 0, sizeof(VENC_RECV_PIC_PARAM_S)); stRecvParam.s32RecvPicNum = 1; RK_MPI_VENC_StartRecvFrame(chnId, &stRecvParam); return 0; } // demo板dev默认都是0,根据不同的channel 来选择不同的vi节点 int vi_dev_init() { printf("%s\n", __func__); int ret = 0; int devId = 0; int pipeId = devId; VI_DEV_ATTR_S stDevAttr; VI_DEV_BIND_PIPE_S stBindPipe; memset(&stDevAttr, 0, sizeof(stDevAttr)); memset(&stBindPipe, 0, sizeof(stBindPipe)); // 0. get dev config status ret = RK_MPI_VI_GetDevAttr(devId, &stDevAttr); if (ret == RK_ERR_VI_NOT_CONFIG) { // 0-1.config dev ret = RK_MPI_VI_SetDevAttr(devId, &stDevAttr); if (ret != RK_SUCCESS) { printf("RK_MPI_VI_SetDevAttr %x\n", ret); return -1; } } else { printf("RK_MPI_VI_SetDevAttr already\n"); } // 1.get dev enable status ret = RK_MPI_VI_GetDevIsEnable(devId); if (ret != RK_SUCCESS) { // 1-2.enable dev ret = RK_MPI_VI_EnableDev(devId); if (ret != RK_SUCCESS) { printf("RK_MPI_VI_EnableDev %x\n", ret); return -1; } // 1-3.bind dev/pipe stBindPipe.u32Num = 1; stBindPipe.PipeId[0] = pipeId; ret = RK_MPI_VI_SetDevBindPipe(devId, &stBindPipe); if (ret != RK_SUCCESS) { printf("RK_MPI_VI_SetDevBindPipe %x\n", ret); return -1; } } else { printf("RK_MPI_VI_EnableDev already\n"); } return 0; } int vi_chn_init(int channelId, int width, int height) { int ret; int buf_cnt = 2; // VI init VI_CHN_ATTR_S vi_chn_attr; memset(&vi_chn_attr, 0, sizeof(vi_chn_attr)); vi_chn_attr.stIspOpt.u32BufCount = buf_cnt; vi_chn_attr.stIspOpt.enMemoryType = VI_V4L2_MEMORY_TYPE_DMABUF; // VI_V4L2_MEMORY_TYPE_MMAP; vi_chn_attr.stSize.u32Width = width; vi_chn_attr.stSize.u32Height = height; vi_chn_attr.enPixelFormat = RK_FMT_YUV420SP; vi_chn_attr.enCompressMode = COMPRESS_MODE_NONE; // COMPRESS_AFBC_16x16; vi_chn_attr.u32Depth = 0; //0, get fail, 1 - u32BufCount, can get, if bind to other device, must be < u32BufCount ret = RK_MPI_VI_SetChnAttr(0, channelId, &vi_chn_attr); ret |= RK_MPI_VI_EnableChn(0, channelId); if (ret) { printf("ERROR: create VI error! ret=%d\n", ret); return ret; } return ret; } static RK_CHAR optstr[] = "?::w:h:I:e:"; static void print_usage(const RK_CHAR *name) { printf("usage example:\n"); printf("\t%s -I 0 -w 1920 -h 1080 \n", name); printf("\t-w | --width: VI width, Default:1920\n"); printf("\t-h | --heght: VI height, Default:1080\n"); printf("\t-I | --camid: camera ctx id, Default 0. " "0:rkisp_mainpath,1:rkisp_selfpath,2:rkisp_bypasspath\n"); } int main(int argc, char *argv[]) { RK_S32 s32Ret = RK_FAILURE; RK_U32 u32Width = 1920; RK_U32 u32Height = 1080; RK_CODEC_ID_E enCodecType = RK_VIDEO_ID_JPEG; RK_CHAR *pCodecName = "JPEG"; RK_S32 s32chnlId = 0; int c; int ret = -1; while ((c = getopt(argc, argv, optstr)) != -1) { switch (c) { case 'w': u32Width = atoi(optarg); break; case 'h': u32Height = atoi(optarg); break; case 'I': s32chnlId = atoi(optarg); break; case '?': default: print_usage(argv[0]); return -1; } } printf("#CodecName:%s\n", pCodecName); printf("#Resolution: %dx%d\n", u32Width, u32Height); printf("#CameraIdx: %d\n\n", s32chnlId); printf("#Frame Count to save: %d\n", g_s32FrameCnt); signal(SIGINT, sigterm_handler); if (RK_MPI_SYS_Init() != RK_SUCCESS) { RK_LOGE("rk mpi sys init fail!"); goto __FAILED; } vi_dev_init(); vi_chn_init(s32chnlId, u32Width, u32Height); // venc init test_venc_init(0, u32Width, u32Height, enCodecType); MPP_CHN_S stSrcChn, stDestChn; // bind vi to venc stSrcChn.enModId = RK_ID_VI; stSrcChn.s32DevId = 0; stSrcChn.s32ChnId = s32chnlId; stDestChn.enModId = RK_ID_VENC; stDestChn.s32DevId = 0; stDestChn.s32ChnId = 0; printf("====RK_MPI_SYS_Bind vi0 to venc0====\n"); s32Ret = RK_MPI_SYS_Bind(&stSrcChn, &stDestChn); if (s32Ret != RK_SUCCESS) { RK_LOGE("bind 0 ch venc failed"); goto __FAILED; } VENC_RECV_PIC_PARAM_S stRecvParam; pthread_t main_thread; pthread_create(&main_thread, NULL, GetMediaBuffer0, NULL); char cmd[64]; VENC_JPEG_PARAM_S stJpegParam; memset(&stJpegParam, 0, sizeof(stJpegParam)); stJpegParam.u32Qfactor = 77; RK_MPI_VENC_SetJpegParam(0, &stJpegParam); while (!quit) { fgets(cmd, sizeof(cmd), stdin); printf("#Input cmd:%s\n", cmd); if (strstr(cmd, "quit")) { printf("#Get 'quit' cmd!\n"); quit = true; break; } memset(&stRecvParam, 0, sizeof(VENC_RECV_PIC_PARAM_S)); stRecvParam.s32RecvPicNum = 1; s32Ret = RK_MPI_VENC_StartRecvFrame(0, &stRecvParam); if (s32Ret) { printf("RK_MPI_VENC_StartRecvFrame failed!\n"); break; } usleep(30000); // sleep 30 ms. } sleep(1); pthread_join(main_thread, NULL); s32Ret = RK_MPI_SYS_UnBind(&stSrcChn, &stDestChn); if (s32Ret != RK_SUCCESS) { RK_LOGE("RK_MPI_SYS_UnBind fail %x", s32Ret); } s32Ret = RK_MPI_VI_DisableChn(0, s32chnlId); RK_LOGE("RK_MPI_VI_DisableChn %x", s32Ret); s32Ret = RK_MPI_VENC_StopRecvFrame(0); if (s32Ret != RK_SUCCESS) { return s32Ret; } s32Ret = RK_MPI_VENC_DestroyChn(0); if (s32Ret != RK_SUCCESS) { RK_LOGE("RK_MPI_VDEC_DestroyChn fail %x", s32Ret); } s32Ret = RK_MPI_VI_DisableDev(0); RK_LOGE("RK_MPI_VI_DisableDev %x", s32Ret); ret = 0; __FAILED: RK_LOGE("test running exit:%d", s32Ret); RK_MPI_SYS_Exit(); return ret; }