Thanks for your assistance.
I have modified the code to measure the runtime of different sections:
1. Capturing a frame and converting its color space (from YUV420SP to BGR).
2. Encoding the frame.
3. Streaming the frame via RTSP.
Code: Select all
#define IT_NUM 600
int main(int argc, char *argv[]) {
system("RkLunch-stop.sh");
RK_S32 s32Ret = 0;
struct timespec start = {0, 0};
struct timespec stop = {0, 0};
float times[3 * IT_NUM];
FILE *fp = NULL;
char f_path[] = "/root/time_log.txt";
fp = fopen(f_path, "wb");
int width = DISP_WIDTH;
int height = DISP_HEIGHT;
char fps_text[16];
float fps = 0;
memset(fps_text,0,16);
//h264_frame
VENC_STREAM_S stFrame;
stFrame.pstPack = (VENC_PACK_S *)malloc(sizeof(VENC_PACK_S));
RK_U64 H264_PTS = 0;
RK_U32 H264_TimeRef = 0;
VIDEO_FRAME_INFO_S stViFrame;
// Create Pool
MB_POOL_CONFIG_S PoolCfg;
memset(&PoolCfg, 0, sizeof(MB_POOL_CONFIG_S));
PoolCfg.u64MBSize = width * height * 3 ;
PoolCfg.u32MBCnt = 1;
PoolCfg.enAllocType = MB_ALLOC_TYPE_DMA;
//PoolCfg.bPreAlloc = RK_FALSE;
MB_POOL src_Pool = RK_MPI_MB_CreatePool(&PoolCfg);
printf("Create Pool success !\n");
// Get MB from Pool
MB_BLK src_Blk = RK_MPI_MB_GetMB(src_Pool, width * height * 3, RK_TRUE);
// Build h264_frame
VIDEO_FRAME_INFO_S h264_frame;
h264_frame.stVFrame.u32Width = width;
h264_frame.stVFrame.u32Height = height;
h264_frame.stVFrame.u32VirWidth = width;
h264_frame.stVFrame.u32VirHeight = height;
h264_frame.stVFrame.enPixelFormat = RK_FMT_RGB888;
h264_frame.stVFrame.u32FrameFlag = 160;
h264_frame.stVFrame.pMbBlk = src_Blk;
unsigned char *data = (unsigned char *)RK_MPI_MB_Handle2VirAddr(src_Blk);
cv::Mat frame(cv::Size(width,height),CV_8UC3,data);
// rkaiq init
RK_BOOL multi_sensor = RK_FALSE;
const char *iq_dir = "/etc/iqfiles";
rk_aiq_working_mode_t hdr_mode = RK_AIQ_WORKING_MODE_NORMAL;
//hdr_mode = RK_AIQ_WORKING_MODE_ISP_HDR2;
SAMPLE_COMM_ISP_Init(0, hdr_mode, multi_sensor, iq_dir);
SAMPLE_COMM_ISP_Run(0);
// rkmpi init
if (RK_MPI_SYS_Init() != RK_SUCCESS) {
RK_LOGE("rk mpi sys init fail!");
return -1;
}
// rtsp init
rtsp_demo_handle g_rtsplive = NULL;
rtsp_session_handle g_rtsp_session;
g_rtsplive = create_rtsp_demo(554);
g_rtsp_session = rtsp_new_session(g_rtsplive, "/live/0");
rtsp_set_video(g_rtsp_session, RTSP_CODEC_ID_VIDEO_H264, NULL, 0);
rtsp_sync_video_ts(g_rtsp_session, rtsp_get_reltime(), rtsp_get_ntptime());
// vi init
vi_dev_init();
vi_chn_init(0, width, height);
// venc init
RK_CODEC_ID_E enCodecType = RK_VIDEO_ID_AVC;
venc_init(0, width, height, enCodecType);
printf("init success\n");
sleep(10);
for (int i = 0;i < 3 * IT_NUM;i += 3) {
// get vi frame
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
h264_frame.stVFrame.u32TimeRef = H264_TimeRef++;
h264_frame.stVFrame.u64PTS = TEST_COMM_GetNowUs();
s32Ret = RK_MPI_VI_GetChnFrame(0, 0, &stViFrame, -1);
if(s32Ret == RK_SUCCESS)
{
void *vi_data = RK_MPI_MB_Handle2VirAddr(stViFrame.stVFrame.pMbBlk);
cv::Mat yuv420sp(height + height / 2, width, CV_8UC1, vi_data);
cv::Mat bgr(height, width, CV_8UC3, data);
cv::cvtColor(yuv420sp, bgr, cv::COLOR_YUV420sp2BGR);
cv::resize(bgr, frame, cv::Size(width ,height), 0, 0, cv::INTER_LINEAR);
//sprintf(fps_text,"fps = %.2f",fps);
//cv::putText(frame,fps_text,
// cv::Point(40, 40),
// cv::FONT_HERSHEY_SIMPLEX,1,
// cv::Scalar(0,255,0),2);
}
memcpy(data, frame.data, width * height * 3);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &stop);
times[i + 0] = (float)stop.tv_sec * 1000000 + (float)stop.tv_nsec / 1000 - ((float)start.tv_sec * 1000000 + (float)start.tv_nsec / 1000);
// encode H264
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
RK_MPI_VENC_SendFrame(0, &h264_frame ,-1);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &stop);
times[i + 1] = (float)stop.tv_sec * 1000000 + (float)stop.tv_nsec / 1000 - ((float)start.tv_sec * 1000000 + (float)start.tv_nsec / 1000);
// rtsp
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
s32Ret = RK_MPI_VENC_GetStream(0, &stFrame, -1);
if(s32Ret == RK_SUCCESS) {
if(g_rtsplive && g_rtsp_session) {
//printf("len = %d PTS = %d \n",stFrame.pstPack->u32Len, stFrame.pstPack->u64PTS);
void *pData = RK_MPI_MB_Handle2VirAddr(stFrame.pstPack->pMbBlk);
rtsp_tx_video(g_rtsp_session, (uint8_t *)pData, stFrame.pstPack->u32Len,
stFrame.pstPack->u64PTS);
rtsp_do_event(g_rtsplive);
}
//RK_U64 nowUs = TEST_COMM_GetNowUs();
//fps = (float) 1000000 / (float)(nowUs - h264_frame.stVFrame.u64PTS);
}
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &stop);
times[i + 2] = (float)stop.tv_sec * 1000000 + (float)stop.tv_nsec / 1000 - ((float)start.tv_sec * 1000000 + (float)start.tv_nsec / 1000);
// release frame
s32Ret = RK_MPI_VI_ReleaseChnFrame(0, 0, &stViFrame);
if (s32Ret != RK_SUCCESS) {
RK_LOGE("RK_MPI_VI_ReleaseChnFrame fail %x", s32Ret);
}
s32Ret = RK_MPI_VENC_ReleaseStream(0, &stFrame);
if (s32Ret != RK_SUCCESS) {
RK_LOGE("RK_MPI_VENC_ReleaseStream fail %x", s32Ret);
}
}
// Destory MB
RK_MPI_MB_ReleaseMB(src_Blk);
// Destory Pool
RK_MPI_MB_DestroyPool(src_Pool);
RK_MPI_VI_DisableChn(0, 0);
RK_MPI_VI_DisableDev(0);
SAMPLE_COMM_ISP_Stop(0);
RK_MPI_VENC_StopRecvFrame(0);
RK_MPI_VENC_DestroyChn(0);
free(stFrame.pstPack);
if (g_rtsplive)
rtsp_del_demo(g_rtsplive);
RK_MPI_SYS_Exit();
for (int j = 0;j < 3 * IT_NUM;j += 3){
fprintf(fp, "%f\t%f\t%f\n", times[j], times[j + 1], times[j + 2]);
}
fclose(fp);
return 0;
}
As shown in the (first) code snippet, the runtime for each section is stored. After capturing a set number of frames (IT_NUM), these measurements (in microseconds) are saved to a file.
Code: Select all
22173.625000 154.000000 1533.500000
14435.500000 112.625000 1518.125000
20612.375000 156.875000 2091.625000
13759.625000 155.750000 1576.125000
19583.125000 164.500000 1492.125000
13825.250000 149.125000 998.625000
17823.125000 158.625000 1265.750000
13793.250000 151.625000 858.875000
13627.250000 142.375000 855.500000
17497.375000 148.125000 1175.000000
13682.000000 137.375000 871.750000
13632.875000 118.125000 843.500000
20603.625000 146.125000 1190.000000
13709.000000 147.875000 830.125000
20613.500000 117.000000 854.500000
13705.125000 145.875000 848.750000
13711.250000 140.875000 824.500000
13610.375000 121.875000 821.375000
17487.875000 175.500000 1262.500000
13706.000000 150.250000 792.125000
13652.625000 118.125000 840.375000
17912.125000 156.875000 1190.250000
14300.000000 130.500000 867.500000
14249.500000 150.750000 883.625000
14226.125000 137.500000 848.375000
13975.625000 121.250000 836.375000
14207.125000 139.750000 859.625000
14184.625000 140.625000 844.250000
14291.000000 133.750000 820.250000
14389.750000 160.375000 888.375000
14249.000000 147.875000 826.875000
14455.000000 116.625000 829.500000
14548.625000 129.125000 822.500000
14668.250000 119.625000 820.875000
14813.125000 141.500000 858.625000
14680.250000 133.000000 864.500000
19677.375000 167.250000 1176.000000
15088.500000 119.000000 846.125000
15130.500000 128.125000 809.625000
15114.625000 140.000000 814.875000
14790.750000 125.125000 846.125000
14915.000000 116.250000 835.000000
14917.375000 157.000000 945.125000
14843.250000 141.500000 804.375000
14991.375000 119.250000 837.500000
14668.750000 141.750000 811.375000
15270.250000 120.500000 840.000000
14715.125000 154.250000 794.500000
14547.000000 152.625000 885.250000
14971.875000 153.500000 839.375000
14795.250000 163.500000 840.250000
14954.625000 231.000000 857.500000
15312.875000 124.250000 870.125000
15117.375000 150.500000 840.000000
19926.875000 157.500000 1249.875000
15270.750000 137.875000 824.000000
15268.500000 133.250000 851.125000
15313.625000 143.250000 852.250000
15089.250000 154.000000 849.250000
15011.000000 121.500000 827.250000
20253.750000 125.500000 884.500000
15370.500000 147.750000 808.500000
14572.750000 123.500000 816.750000
14608.750000 120.250000 813.250000
15001.500000 146.000000 810.000000
14877.250000 119.750000 836.750000
20241.000000 165.250000 1251.500000
15553.750000 161.500000 825.500000
15159.500000 146.000000 866.500000
14661.250000 136.500000 822.750000
15104.750000 150.500000 844.500000
14832.000000 125.500000 823.750000
14928.000000 132.250000 990.250000
15261.250000 140.500000 807.250000
14870.750000 3318.750000 697.500000
11880.750000 146.500000 861.250000
28051.000000 118.250000 910.750000
15398.750000 129.500000 884.750000
15319.500000 153.750000 927.750000
14649.500000 121.250000 841.750000
15176.500000 190.750000 840.500000
15344.250000 119.500000 842.000000
15392.000000 113.000000 829.250000
20590.000000 143.250000 912.750000
14858.000000 116.500000 856.500000
14885.000000 125.750000 834.250000
19999.750000 157.250000 1210.250000
15240.500000 146.250000 851.000000
15025.750000 136.000000 814.000000
14934.500000 210.750000 840.250000
15367.000000 112.000000 832.000000
15078.250000 140.500000 828.000000
14937.000000 240.250000 810.000000
14803.000000 145.000000 797.500000
14922.250000 162.000000 856.750000
14673.750000 133.000000 798.250000
15076.750000 140.750000 826.500000
14761.500000 151.500000 824.750000
14699.500000 131.750000 814.750000
15092.250000 126.000000 812.250000
14535.750000 116.750000 812.750000
19196.000000 160.750000 1143.750000
14688.000000 124.750000 914.750000
14187.500000 115.500000 811.500000
21286.250000 176.750000 1194.500000
14180.250000 123.500000 812.500000
15895.500000 169.500000 1234.500000
13917.000000 117.000000 809.750000
19556.000000 161.000000 1024.000000
13803.000000 147.750000 817.000000
17447.000000 163.000000 1162.750000
13714.250000 115.750000 792.750000
13591.250000 119.500000 819.500000
17453.750000 153.250000 1161.500000
13773.500000 128.750000 804.750000
15323.000000 155.250000 1201.750000
15631.250000 139.750000 773.750000
13696.500000 151.500000 815.500000
13760.500000 146.250000 813.000000
17472.250000 181.000000 1136.750000
13711.500000 143.500000 802.000000
13626.500000 117.000000 806.250000
17485.250000 154.250000 1155.500000
13719.750000 141.750000 809.250000
13582.250000 126.750000 824.750000
17523.750000 161.750000 1178.500000
13701.250000 121.500000 811.500000
13555.000000 156.500000 903.000000
17463.750000 159.000000 1145.000000
13707.500000 158.500000 819.500000
13589.250000 122.500000 819.000000
17452.500000 152.750000 1147.500000
13727.000000 137.250000 808.250000
13645.750000 126.000000 874.250000
20667.000000 171.250000 1172.000000
13656.500000 148.750000 763.750000
20606.250000 153.000000 1157.250000
13743.250000 135.250000 840.500000
19555.000000 154.500000 947.500000
13809.250000 143.500000 809.250000
17464.250000 157.500000 1129.500000
13724.000000 134.000000 806.000000
13639.500000 122.500000 808.250000
17463.500000 159.750000 1142.000000
13769.750000 126.000000 845.250000
13645.750000 120.000000 810.500000
17457.750000 162.250000 1159.500000
13673.750000 120.000000 819.500000
13753.750000 124.250000 838.500000
17476.500000 173.500000 1150.750000
13712.750000 140.750000 813.500000
13612.500000 118.500000 813.750000
17537.500000 168.250000 1237.250000
13722.500000 118.500000 808.000000
15262.000000 161.000000 1159.500000
13636.750000 138.500000 822.000000
19577.500000 159.250000 969.250000
13824.750000 122.250000 828.750000
17494.500000 169.250000 1242.500000
13772.250000 142.000000 792.250000
13580.500000 119.500000 828.250000
17494.250000 152.250000 1134.750000
13741.000000 122.250000 789.250000
13607.750000 118.750000 823.500000
17490.250000 174.000000 1250.250000
13953.500000 136.000000 808.250000
14053.250000 130.000000 800.250000
17547.250000 159.250000 1184.000000
14107.750000 134.750000 799.000000
14224.000000 148.500000 813.000000
18742.500000 162.250000 1175.250000
14767.750000 116.750000 825.500000
14976.000000 149.500000 847.500000
15217.500000 116.000000 814.500000
15269.500000 141.000000 822.750000
14812.000000 120.750000 834.750000
14628.250000 131.250000 828.500000
15037.250000 134.250000 836.250000
15029.500000 124.000000 838.000000
15052.500000 175.750000 812.500000
14759.250000 119.750000 825.500000
14835.500000 128.000000 831.000000
14528.000000 149.000000 816.000000
14341.000000 130.500000 888.500000
14517.500000 153.000000 835.000000
14704.500000 126.000000 831.500000
14783.000000 126.000000 813.500000
14539.000000 142.500000 784.500000
14821.000000 137.000000 830.000000
14616.000000 139.000000 935.500000
14270.500000 135.500000 806.500000
18273.500000 165.500000 1163.500000
13931.000000 146.500000 761.500000
15649.000000 153.000000 1181.000000
15646.000000 141.000000 805.000000
13723.500000 135.500000 827.000000
13609.500000 119.500000 807.500000
17492.000000 152.000000 1135.500000
13742.500000 120.500000 820.000000
13673.000000 145.000000 793.500000
17503.500000 152.000000 1166.500000
13697.500000 119.000000 807.000000
13639.500000 126.500000 783.000000
19471.000000 127.500000 840.500000
19659.500000 171.000000 907.000000
13774.500000 141.500000 830.000000
17484.500000 149.000000 1139.500000
13686.000000 138.500000 785.000000
13785.500000 151.000000 892.000000
20564.000000 163.000000 1145.500000
13705.000000 127.500000 821.500000
13596.000000 120.000000 818.500000
17471.500000 154.000000 1152.000000
13757.000000 150.500000 792.000000
13611.500000 125.000000 870.000000
17518.500000 168.000000 1159.000000
13688.000000 149.500000 819.000000
15358.000000 139.500000 1171.000000
13600.000000 159.500000 812.000000
19572.000000 163.500000 973.000000
13746.000000 155.500000 805.000000
17508.500000 184.000000 1140.500000
13687.000000 126.000000 800.000000
13622.000000 116.000000 812.000000
20630.000000 148.500000 1171.500000
14274.500000 2602.500000 686.500000
11790.000000 134.000000 834.000000
21637.000000 148.000000 785.000000
13622.500000 165.000000 792.000000
17441.000000 160.500000 1153.000000
13740.500000 138.500000 782.500000
13609.500000 119.000000 818.000000
17505.000000 144.500000 1132.000000
13704.000000 147.000000 855.000000
13585.500000 122.000000 812.500000
17372.500000 178.000000 1170.500000
13660.000000 116.500000 823.500000
13798.500000 127.500000 804.500000
17443.500000 157.500000 1166.500000
13730.500000 132.500000 882.000000
13619.000000 121.000000 815.500000
17444.000000 163.000000 1150.000000
13635.000000 125.000000 803.000000
13592.500000 126.500000 829.000000
17453.000000 165.000000 1163.000000
13741.500000 152.500000 814.500000
13626.500000 117.000000 810.500000
17480.000000 160.000000 1147.500000
13746.500000 143.000000 826.000000
13605.000000 118.000000 809.000000
17508.000000 165.500000 1198.000000
13692.000000 138.500000 785.500000
13616.500000 121.000000 814.500000
17413.000000 166.500000 1150.500000
13704.500000 122.000000 811.500000
13605.500000 115.500000 821.000000
17440.500000 156.000000 1156.000000
13759.500000 129.500000 819.000000
13571.000000 132.500000 897.500000
17493.000000 165.500000 1158.000000
13698.500000 124.000000 823.000000
13578.000000 131.500000 821.500000
17442.500000 162.000000 1168.500000
13724.000000 130.500000 803.000000
13580.000000 120.000000 875.500000
17467.500000 155.000000 1163.000000
13727.500000 133.500000 802.000000
13696.000000 134.500000 850.500000
17487.500000 154.500000 1127.500000
13698.000000 134.000000 827.500000
13617.500000 133.500000 802.500000
17479.500000 163.000000 1139.000000
13687.000000 116.000000 810.500000
13592.500000 124.500000 828.000000
17498.500000 145.500000 1149.500000
13709.500000 127.500000 811.500000
13656.000000 128.000000 823.500000
17483.000000 154.500000 1158.000000
13709.000000 120.500000 811.000000
20561.500000 164.500000 1177.500000
13640.000000 146.500000 774.500000
19561.000000 162.500000 982.000000
13746.500000 154.500000 806.500000
17495.500000 164.000000 1240.000000
13758.500000 157.500000 756.000000
13639.500000 118.500000 799.500000
17431.000000 165.500000 1153.000000
13662.500000 135.500000 799.500000
13598.000000 123.500000 841.000000
17496.000000 153.500000 1252.000000
13687.500000 111.500000 797.000000
20586.000000 145.500000 1158.000000
13705.500000 115.000000 807.500000
19643.000000 165.500000 887.000000
13719.000000 140.000000 824.000000
17503.500000 149.500000 1166.500000
13696.000000 127.500000 803.500000
13727.500000 142.500000 740.000000
17609.000000 162.000000 1078.500000
13697.000000 114.000000 769.000000
13582.500000 142.000000 771.000000
17530.000000 141.000000 1079.500000
13709.000000 142.000000 725.500000
13599.000000 129.000000 769.500000
17470.000000 157.000000 1088.500000
13663.000000 138.000000 749.500000
13629.500000 118.000000 782.000000
20555.000000 160.500000 1085.000000
13692.000000 142.000000 733.500000
20582.500000 152.000000 1109.000000
13606.500000 152.000000 729.500000
19519.500000 162.000000 907.500000
13718.500000 164.500000 763.500000
17430.000000 155.000000 1083.500000
13652.000000 125.000000 824.500000
13610.000000 120.500000 771.000000
17511.500000 157.500000 1083.500000
13724.000000 131.500000 754.000000
13543.000000 159.000000 731.000000
17505.500000 146.500000 1071.000000
13712.500000 139.000000 811.500000
13632.500000 123.500000 766.500000
17478.000000 163.000000 1092.500000
13726.500000 131.500000 767.500000
15273.500000 170.000000 1087.000000
13556.500000 139.000000 776.500000
19624.500000 162.500000 882.000000
13845.500000 120.000000 765.500000
17457.500000 152.000000 1085.500000
13722.500000 140.000000 745.500000
13641.500000 126.500000 757.000000
17470.500000 155.500000 1076.500000
13664.000000 134.000000 758.000000
13608.500000 117.000000 759.500000
20554.000000 148.000000 1090.500000
13696.500000 138.000000 744.500000
20572.500000 130.500000 773.500000
13704.500000 139.000000 744.500000
19574.000000 149.500000 889.500000
13764.000000 121.500000 846.500000
17472.000000 157.500000 1076.000000
13679.500000 146.000000 748.500000
13620.000000 122.000000 784.000000
17478.000000 166.500000 1077.000000
13719.500000 136.500000 763.500000
13589.500000 116.500000 834.000000
17438.500000 174.500000 1093.000000
13729.500000 140.000000 757.500000
13568.000000 125.000000 770.000000
17461.000000 165.000000 1092.000000
13668.500000 134.000000 749.500000
13575.000000 115.500000 777.500000
17506.500000 169.000000 1095.500000
13737.000000 124.000000 759.500000
15273.000000 167.000000 1065.000000
15527.500000 209.000000 751.000000
13734.000000 133.500000 761.000000
13748.000000 146.500000 763.000000
17488.500000 156.500000 1077.000000
13683.500000 130.500000 755.000000
13628.500000 116.500000 772.000000
17479.500000 159.500000 1083.000000
13690.000000 113.000000 759.500000
13581.500000 141.500000 816.500000
17429.000000 166.000000 1174.500000
13707.500000 149.500000 766.000000
13630.500000 139.000000 729.000000
17501.000000 155.000000 1101.000000
13652.500000 127.500000 756.500000
13541.500000 117.000000 775.500000
17500.000000 154.500000 1167.500000
13720.000000 142.000000 732.500000
13617.000000 136.000000 765.500000
17451.000000 158.000000 1089.000000
13681.500000 127.500000 764.000000
17450.500000 188.500000 820.000000
13966.500000 116.000000 794.500000
20573.000000 146.500000 751.000000
13702.000000 144.500000 745.000000
13683.500000 117.500000 766.500000
13613.500000 126.000000 776.000000
17515.000000 170.000000 1080.500000
13762.000000 150.500000 726.000000
13636.500000 139.000000 755.500000
17469.500000 156.000000 1081.500000
13662.500000 136.500000 762.000000
13806.000000 111.500000 765.000000
17447.000000 164.500000 1084.000000
13718.500000 121.500000 759.500000
15259.500000 167.000000 1170.500000
15563.500000 199.500000 758.000000
13671.000000 117.500000 759.000000
13609.500000 120.500000 772.000000
17473.000000 160.000000 1083.000000
13637.000000 125.500000 764.000000
13648.000000 132.500000 836.000000
17391.500000 176.000000 1094.000000
13755.000000 144.000000 747.000000
13532.500000 159.500000 748.000000
17440.000000 164.500000 1075.000000
13727.000000 141.000000 747.500000
13590.500000 130.500000 752.500000
17500.000000 178.500000 1090.500000
13718.000000 138.000000 766.500000
15306.500000 164.000000 1082.500000
13664.000000 141.500000 760.000000
19545.000000 168.000000 941.500000
13794.000000 166.000000 768.000000
17504.000000 162.500000 1096.500000
13741.000000 132.000000 742.500000
13622.500000 123.000000 766.000000
17453.500000 158.000000 1093.500000
13716.000000 156.000000 734.500000
13606.000000 116.500000 770.500000
17489.500000 152.000000 1146.500000
13717.000000 123.500000 750.000000
13725.500000 154.500000 838.500000
17484.000000 150.500000 1096.000000
13710.000000 137.000000 754.000000
13647.500000 128.500000 760.000000
17476.500000 158.000000 1155.500000
13704.500000 134.500000 772.000000
13574.500000 142.000000 743.500000
17459.500000 162.500000 1084.000000
13728.000000 151.000000 767.000000
13642.500000 114.000000 769.000000
17482.500000 142.500000 1092.000000
13764.500000 142.000000 759.000000
13566.000000 133.000000 770.000000
17467.000000 163.500000 1097.500000
13654.500000 131.000000 746.500000
13622.500000 117.000000 760.000000
17507.000000 152.000000 1076.000000
13697.500000 134.000000 758.000000
13596.000000 135.000000 761.000000
17414.000000 167.000000 1083.000000
13733.000000 137.000000 756.000000
13618.000000 116.000000 771.000000
17471.000000 158.000000 1073.000000
13674.000000 139.000000 808.000000
13585.000000 138.000000 762.000000
17530.000000 149.000000 1082.000000
13738.000000 135.000000 752.000000
13597.000000 121.000000 766.000000
17516.000000 168.000000 1087.000000
13690.000000 123.000000 837.000000
13623.000000 224.000000 803.000000
17524.000000 165.000000 1072.000000
13672.000000 146.000000 719.000000
13590.000000 135.000000 780.000000
17465.000000 158.000000 1080.000000
13735.000000 132.000000 758.000000
13579.000000 116.000000 769.000000
17451.000000 159.000000 1068.000000
13747.000000 132.000000 755.000000
13616.000000 125.000000 794.000000
17439.000000 166.000000 1082.000000
13700.000000 124.000000 765.000000
13613.000000 118.000000 770.000000
20554.000000 162.000000 1082.000000
13714.000000 123.000000 775.000000
13535.000000 150.000000 736.000000
17445.000000 161.000000 1067.000000
13669.000000 115.000000 778.000000
13627.000000 113.000000 769.000000
17522.000000 144.000000 1074.000000
13676.000000 158.000000 756.000000
15356.000000 176.000000 1095.000000
13636.000000 150.000000 761.000000
19555.000000 160.000000 929.000000
13795.000000 163.000000 859.000000
17434.000000 154.000000 1092.000000
13734.000000 149.000000 731.000000
13608.000000 116.000000 780.000000
17490.000000 161.000000 1076.000000
13670.000000 162.000000 758.000000
15435.000000 173.000000 1272.000000
13630.000000 123.000000 766.000000
19555.000000 166.000000 944.000000
13716.000000 120.000000 776.000000
17520.000000 161.000000 1091.000000
13690.000000 127.000000 762.000000
13638.000000 124.000000 755.000000
17474.000000 161.000000 1081.000000
13656.000000 132.000000 764.000000
13610.000000 117.000000 772.000000
17459.000000 160.000000 1086.000000
13674.000000 122.000000 754.000000
20624.000000 150.000000 1105.000000
13629.000000 150.000000 749.000000
19656.000000 153.000000 804.000000
13733.000000 148.000000 789.000000
17460.000000 165.000000 1090.000000
13692.000000 140.000000 742.000000
13584.000000 126.000000 765.000000
17469.000000 163.000000 1180.000000
13763.000000 137.000000 759.000000
15456.000000 163.000000 1101.000000
14516.000000 155.000000 781.000000
20550.000000 144.000000 843.000000
14421.000000 120.000000 786.000000
18763.000000 177.000000 1215.000000
14755.000000 115.000000 777.000000
15172.000000 129.000000 766.000000
22647.000000 160.000000 1015.000000
14737.000000 160.000000 754.000000
21485.000000 145.000000 761.000000
14751.000000 158.000000 751.000000
14636.000000 135.000000 761.000000
14300.000000 118.000000 769.000000
19594.000000 170.000000 1083.000000
14926.000000 128.000000 769.000000
14674.000000 116.000000 774.000000
14682.000000 119.000000 784.000000
14938.000000 122.000000 757.000000
14606.000000 143.000000 771.000000
14513.000000 128.000000 771.000000
14532.000000 138.000000 762.000000
14413.000000 125.000000 766.000000
19295.000000 179.000000 1092.000000
14897.000000 145.000000 833.000000
14131.000000 136.000000 758.000000
21007.000000 149.000000 1094.000000
13941.000000 128.000000 772.000000
20826.000000 146.000000 790.000000
13738.000000 148.000000 733.000000
16218.000000 155.000000 835.000000
11864.000000 123.000000 771.000000
26473.000000 173.000000 1366.000000
13790.000000 168.000000 777.000000
17561.000000 166.000000 1100.000000
13744.000000 140.000000 750.000000
13632.000000 143.000000 721.000000
17496.000000 160.000000 1098.000000
13723.000000 132.000000 759.000000
13640.000000 127.000000 767.000000
17555.000000 154.000000 1099.000000
13726.000000 147.000000 745.000000
13621.000000 139.000000 758.000000
17562.000000 174.000000 1096.000000
13734.000000 133.000000 762.000000
13602.000000 115.000000 785.000000
17487.000000 161.000000 1070.000000
13734.000000 141.000000 745.000000
13678.000000 140.000000 752.000000
17550.000000 150.000000 1150.000000
13692.000000 138.000000 732.000000
13595.000000 129.000000 775.000000
17453.000000 160.000000 1083.000000
13693.000000 124.000000 758.000000
13619.000000 115.000000 773.000000
17488.000000 146.000000 1195.000000
13742.000000 138.000000 752.000000
13614.000000 118.000000 767.000000
17503.000000 151.000000 1093.000000
13756.000000 133.000000 759.000000
13602.000000 118.000000 774.000000
17522.000000 173.000000 1103.000000
13697.000000 139.000000 733.000000
13601.000000 116.000000 782.000000
17535.000000 176.000000 1091.000000
13741.000000 142.000000 742.000000
13652.000000 133.000000 752.000000
17561.000000 175.000000 1093.000000
13734.000000 136.000000 741.000000
13635.000000 148.000000 732.000000
20650.000000 155.000000 1083.000000
13670.000000 136.000000 756.000000
20596.000000 128.000000 792.000000
13746.000000 158.000000 781.000000
19594.000000 151.000000 945.000000
13720.000000 140.000000 778.000000
17544.000000 159.000000 1112.000000
13724.000000 153.000000 740.000000
13653.000000 125.000000 778.000000
17439.000000 140.000000 1077.000000
13712.000000 128.000000 827.000000
13592.000000 116.000000 775.000000
17667.000000 155.000000 1077.000000
13752.000000 156.000000 718.000000
13606.000000 115.000000 767.000000
20562.000000 155.000000 1084.000000
13722.000000 128.000000 779.000000
15244.000000 183.000000 1094.000000
13608.000000 158.000000 765.000000
19543.000000 151.000000 912.000000
13782.000000 154.000000 772.000000
17586.000000 167.000000 1086.000000
13778.000000 119.000000 769.000000
13609.000000 119.000000 783.000000
17500.000000 141.000000 1098.000000
13729.000000 119.000000 765.000000
20591.000000 157.000000 1092.000000
13655.000000 120.000000 767.000000
19543.000000 167.000000 926.000000
13806.000000 150.000000 851.000000
17673.000000 167.000000 1079.000000
13716.000000 139.000000 759.000000
13589.000000 130.000000 754.000000
17508.000000 145.000000 1099.000000
From this data, it can be seen that the total processing time for a frame is slightly less than 20 milliseconds. Interestingly, the RTSP streaming section itself only takes around 1 millisecond. The most time-consuming part appears to be the combined "capturing frame and changing the color space" step.
To investigate further, I separated the "capturing frame and changing the color space" step into two distinct measurements:
1. Capturing the frame from the VI (Video Input) module.
2. Performing the color space transformation (YUV420SP to BGR).
Here's the modified code snippet for this finer-grained timing:
Code: Select all
int main(int argc, char *argv[]) {
system("RkLunch-stop.sh");
RK_S32 s32Ret = 0;
struct timespec start = {0, 0};
struct timespec stop = {0, 0};
float times[4 * IT_NUM];
FILE *fp = NULL;
char f_path[] = "/root/time_log.txt";
fp = fopen(f_path, "wb");
int width = DISP_WIDTH;
int height = DISP_HEIGHT;
char fps_text[16];
float fps = 0;
memset(fps_text,0,16);
//h264_frame
VENC_STREAM_S stFrame;
stFrame.pstPack = (VENC_PACK_S *)malloc(sizeof(VENC_PACK_S));
RK_U64 H264_PTS = 0;
RK_U32 H264_TimeRef = 0;
VIDEO_FRAME_INFO_S stViFrame;
// Create Pool
MB_POOL_CONFIG_S PoolCfg;
memset(&PoolCfg, 0, sizeof(MB_POOL_CONFIG_S));
PoolCfg.u64MBSize = width * height * 3 ;
PoolCfg.u32MBCnt = 1;
PoolCfg.enAllocType = MB_ALLOC_TYPE_DMA;
//PoolCfg.bPreAlloc = RK_FALSE;
MB_POOL src_Pool = RK_MPI_MB_CreatePool(&PoolCfg);
printf("Create Pool success !\n");
// Get MB from Pool
MB_BLK src_Blk = RK_MPI_MB_GetMB(src_Pool, width * height * 3, RK_TRUE);
// Build h264_frame
VIDEO_FRAME_INFO_S h264_frame;
h264_frame.stVFrame.u32Width = width;
h264_frame.stVFrame.u32Height = height;
h264_frame.stVFrame.u32VirWidth = width;
h264_frame.stVFrame.u32VirHeight = height;
h264_frame.stVFrame.enPixelFormat = RK_FMT_RGB888;
h264_frame.stVFrame.u32FrameFlag = 160;
h264_frame.stVFrame.pMbBlk = src_Blk;
unsigned char *data = (unsigned char *)RK_MPI_MB_Handle2VirAddr(src_Blk);
cv::Mat frame(cv::Size(width,height),CV_8UC3,data);
// rkaiq init
RK_BOOL multi_sensor = RK_FALSE;
const char *iq_dir = "/etc/iqfiles";
rk_aiq_working_mode_t hdr_mode = RK_AIQ_WORKING_MODE_NORMAL;
//hdr_mode = RK_AIQ_WORKING_MODE_ISP_HDR2;
SAMPLE_COMM_ISP_Init(0, hdr_mode, multi_sensor, iq_dir);
SAMPLE_COMM_ISP_Run(0);
// rkmpi init
if (RK_MPI_SYS_Init() != RK_SUCCESS) {
RK_LOGE("rk mpi sys init fail!");
return -1;
}
// rtsp init
rtsp_demo_handle g_rtsplive = NULL;
rtsp_session_handle g_rtsp_session;
g_rtsplive = create_rtsp_demo(554);
g_rtsp_session = rtsp_new_session(g_rtsplive, "/live/0");
rtsp_set_video(g_rtsp_session, RTSP_CODEC_ID_VIDEO_H264, NULL, 0);
rtsp_sync_video_ts(g_rtsp_session, rtsp_get_reltime(), rtsp_get_ntptime());
// vi init
vi_dev_init();
vi_chn_init(0, width, height);
// venc init
RK_CODEC_ID_E enCodecType = RK_VIDEO_ID_AVC;
venc_init(0, width, height, enCodecType);
printf("init success\n");
sleep(10);
for (int i = 0;i < 4 * IT_NUM;i += 4) {
// get vi frame
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
h264_frame.stVFrame.u32TimeRef = H264_TimeRef++;
h264_frame.stVFrame.u64PTS = TEST_COMM_GetNowUs();
s32Ret = RK_MPI_VI_GetChnFrame(0, 0, &stViFrame, -1);
if(s32Ret == RK_SUCCESS)
{
void *vi_data = RK_MPI_MB_Handle2VirAddr(stViFrame.stVFrame.pMbBlk);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &stop);
times[i + 0] = (float)stop.tv_sec * 1000000 + (float)stop.tv_nsec / 1000 - ((float)start.tv_sec * 1000000 + (float)start.tv_nsec / 1000);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
cv::Mat yuv420sp(height + height / 2, width, CV_8UC1, vi_data);
cv::Mat bgr(height, width, CV_8UC3, data);
cv::cvtColor(yuv420sp, bgr, cv::COLOR_YUV420sp2BGR);
cv::resize(bgr, frame, cv::Size(width ,height), 0, 0, cv::INTER_LINEAR);
//sprintf(fps_text,"fps = %.2f",fps);
//cv::putText(frame,fps_text,
// cv::Point(40, 40),
// cv::FONT_HERSHEY_SIMPLEX,1,
// cv::Scalar(0,255,0),2);
}
memcpy(data, frame.data, width * height * 3);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &stop);
times[i + 1] = (float)stop.tv_sec * 1000000 + (float)stop.tv_nsec / 1000 - ((float)start.tv_sec * 1000000 + (float)start.tv_nsec / 1000);
// encode H264
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
RK_MPI_VENC_SendFrame(0, &h264_frame ,-1);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &stop);
times[i + 2] = (float)stop.tv_sec * 1000000 + (float)stop.tv_nsec / 1000 - ((float)start.tv_sec * 1000000 + (float)start.tv_nsec / 1000);
// rtsp
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
s32Ret = RK_MPI_VENC_GetStream(0, &stFrame, -1);
if(s32Ret == RK_SUCCESS) {
if(g_rtsplive && g_rtsp_session) {
//printf("len = %d PTS = %d \n",stFrame.pstPack->u32Len, stFrame.pstPack->u64PTS);
void *pData = RK_MPI_MB_Handle2VirAddr(stFrame.pstPack->pMbBlk);
rtsp_tx_video(g_rtsp_session, (uint8_t *)pData, stFrame.pstPack->u32Len,
stFrame.pstPack->u64PTS);
rtsp_do_event(g_rtsplive);
}
//RK_U64 nowUs = TEST_COMM_GetNowUs();
//fps = (float) 1000000 / (float)(nowUs - h264_frame.stVFrame.u64PTS);
}
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &stop);
times[i + 3] = (float)stop.tv_sec * 1000000 + (float)stop.tv_nsec / 1000 - ((float)start.tv_sec * 1000000 + (float)start.tv_nsec / 1000);
// release frame
s32Ret = RK_MPI_VI_ReleaseChnFrame(0, 0, &stViFrame);
if (s32Ret != RK_SUCCESS) {
RK_LOGE("RK_MPI_VI_ReleaseChnFrame fail %x", s32Ret);
}
s32Ret = RK_MPI_VENC_ReleaseStream(0, &stFrame);
if (s32Ret != RK_SUCCESS) {
RK_LOGE("RK_MPI_VENC_ReleaseStream fail %x", s32Ret);
}
}
// Destory MB
RK_MPI_MB_ReleaseMB(src_Blk);
// Destory Pool
RK_MPI_MB_DestroyPool(src_Pool);
RK_MPI_VI_DisableChn(0, 0);
RK_MPI_VI_DisableDev(0);
SAMPLE_COMM_ISP_Stop(0);
RK_MPI_VENC_StopRecvFrame(0);
RK_MPI_VENC_DestroyChn(0);
free(stFrame.pstPack);
if (g_rtsplive)
rtsp_del_demo(g_rtsplive);
RK_MPI_SYS_Exit();
for (int j = 0;j < 4 * IT_NUM;j += 4){
fprintf(fp, "%f\t%f\t%f\t%f\n", times[j], times[j + 1], times[j + 2], times[j + 3]);
}
fclose(fp);
return 0;
}
1/2