-
Notifications
You must be signed in to change notification settings - Fork 6
/
sample_vi_fd.c
284 lines (232 loc) · 8.96 KB
/
sample_vi_fd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#define LOG_TAG "SampleFD"
#define LOG_LEVEL LOG_LEVEL_INFO
#include "middleware_utils.h"
#include "sample_utils.h"
#include "vi_vo_utils.h"
#include <core/utils/vpss_helper.h>
#include <cvi_comm.h>
#include <rtsp.h>
#include <sample_comm.h>
#include "cvi_tdl.h"
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static volatile bool bExit = false;
static cvtdl_face_t g_stFaceMeta = {0};
static uint32_t g_size = 0;
MUTEXAUTOLOCK_INIT(ResultMutex);
typedef struct {
SAMPLE_TDL_MW_CONTEXT *pstMWContext;
cvitdl_service_handle_t stServiceHandle;
} SAMPLE_TDL_VENC_THREAD_ARG_S;
void *run_venc(void *args) {
printf("Enter encoder thread\n");
SAMPLE_TDL_VENC_THREAD_ARG_S *pstArgs = (SAMPLE_TDL_VENC_THREAD_ARG_S *)args;
VIDEO_FRAME_INFO_S stFrame;
CVI_S32 s32Ret;
cvtdl_face_t stFaceMeta = {0};
while (bExit == false) {
s32Ret = CVI_VPSS_GetChnFrame(0, 0, &stFrame, 2000);
if (s32Ret != CVI_SUCCESS) {
printf("CVI_VPSS_GetChnFrame chn0 failed with %#x\n", s32Ret);
break;
}
{
MutexAutoLock(ResultMutex, lock);
memset(&stFaceMeta, 0, sizeof(cvtdl_face_t));
if (NULL != g_stFaceMeta.info) {
CVI_TDL_CopyFaceMeta(&g_stFaceMeta, &stFaceMeta);
}
CVI_TDL_Free(&g_stFaceMeta);
}
s32Ret = CVI_TDL_Service_FaceDrawRect(pstArgs->stServiceHandle, &stFaceMeta, &stFrame, false,
CVI_TDL_Service_GetDefaultBrush());
if (s32Ret != CVI_TDL_SUCCESS) {
CVI_VPSS_ReleaseChnFrame(0, 0, &stFrame);
printf("Draw fame fail!, ret=%x\n", s32Ret);
goto error;
}
s32Ret = SAMPLE_TDL_Send_Frame_RTSP(&stFrame, pstArgs->pstMWContext);
if (s32Ret != CVI_SUCCESS) {
CVI_VPSS_ReleaseChnFrame(0, 0, &stFrame);
printf("Send Output Frame NG, ret=%x\n", s32Ret);
goto error;
}
error:
CVI_TDL_Free(&stFaceMeta);
CVI_VPSS_ReleaseChnFrame(0, 0, &stFrame);
if (s32Ret != CVI_SUCCESS) {
bExit = true;
}
}
printf("Exit encoder thread\n");
pthread_exit(NULL);
}
void *run_tdl_thread(void *pHandle) {
printf("Enter TDL thread\n");
cvitdl_handle_t pstTDLHandle = (cvitdl_handle_t)pHandle;
VIDEO_FRAME_INFO_S stFrame;
cvtdl_face_t stFaceMeta = {0};
CVI_S32 s32Ret;
while (bExit == false) {
s32Ret = CVI_VPSS_GetChnFrame(0, VPSS_CHN1, &stFrame, 2000);
if (s32Ret != CVI_SUCCESS) {
printf("CVI_VPSS_GetChnFrame failed with %#x\n", s32Ret);
goto get_frame_failed;
}
memset(&stFaceMeta, 0, sizeof(cvtdl_face_t));
s32Ret = CVI_TDL_ScrFDFace(pstTDLHandle, &stFrame, &stFaceMeta);
if (s32Ret != CVI_TDL_SUCCESS) {
printf("inference failed!, ret=%x\n", s32Ret);
goto inf_error;
}
if (stFaceMeta.size != g_size) {
printf("face count: %d\n", stFaceMeta.size);
g_size = stFaceMeta.size;
}
{
MutexAutoLock(ResultMutex, lock);
memset(&g_stFaceMeta, 0, sizeof(cvtdl_face_t));
if (NULL != stFaceMeta.info) {
CVI_TDL_CopyFaceMeta(&stFaceMeta, &g_stFaceMeta);
}
}
inf_error:
CVI_VPSS_ReleaseChnFrame(0, 1, &stFrame);
get_frame_failed:
CVI_TDL_Free(&stFaceMeta);
if (s32Ret != CVI_SUCCESS) {
bExit = true;
}
}
printf("Exit TDL thread\n");
pthread_exit(NULL);
}
static void SampleHandleSig(CVI_S32 signo) {
signal(SIGINT, SIG_IGN);
signal(SIGTERM, SIG_IGN);
printf("handle signal, signo: %d\n", signo);
if (SIGINT == signo || SIGTERM == signo) {
bExit = true;
}
}
int main(int argc, char *argv[]) {
if (argc != 2) {
printf(
"\nUsage: %s RETINA_MODEL_PATH.\n\n"
"\tRETINA_MODEL_PATH, path to retinaface model.\n",
argv[0]);
return CVI_TDL_FAILURE;
}
signal(SIGINT, SampleHandleSig);
signal(SIGTERM, SampleHandleSig);
SAMPLE_TDL_MW_CONFIG_S stMWConfig = {0};
CVI_S32 s32Ret = SAMPLE_TDL_Get_VI_Config(&stMWConfig.stViConfig);
if (s32Ret != CVI_SUCCESS || stMWConfig.stViConfig.s32WorkingViNum <= 0) {
printf("Failed to get senor infomation from ini file (/mnt/data/sensor_cfg.ini).\n");
return -1;
}
// Get VI size
PIC_SIZE_E enPicSize;
s32Ret = SAMPLE_COMM_VI_GetSizeBySensor(stMWConfig.stViConfig.astViInfo[0].stSnsInfo.enSnsType,
&enPicSize);
if (s32Ret != CVI_SUCCESS) {
printf("Cannot get senor size\n");
return -1;
}
SIZE_S stSensorSize;
s32Ret = SAMPLE_COMM_SYS_GetPicSize(enPicSize, &stSensorSize);
if (s32Ret != CVI_SUCCESS) {
printf("Cannot get senor size\n");
return -1;
}
// Setup frame size of video encoder to 1080p
SIZE_S stVencSize = {
.u32Width = 1280,
.u32Height = 720,
};
stMWConfig.stVBPoolConfig.u32VBPoolCount = 3;
// VBPool 0 for VPSS Grp0 Chn0
stMWConfig.stVBPoolConfig.astVBPoolSetup[0].enFormat = VI_PIXEL_FORMAT;
stMWConfig.stVBPoolConfig.astVBPoolSetup[0].u32BlkCount = 5;
stMWConfig.stVBPoolConfig.astVBPoolSetup[0].u32Height = stSensorSize.u32Height;
stMWConfig.stVBPoolConfig.astVBPoolSetup[0].u32Width = stSensorSize.u32Width;
stMWConfig.stVBPoolConfig.astVBPoolSetup[0].bBind = true;
stMWConfig.stVBPoolConfig.astVBPoolSetup[0].u32VpssChnBinding = VPSS_CHN0;
stMWConfig.stVBPoolConfig.astVBPoolSetup[0].u32VpssGrpBinding = (VPSS_GRP)0;
// VBPool 1 for VPSS Grp0 Chn1
stMWConfig.stVBPoolConfig.astVBPoolSetup[1].enFormat = VI_PIXEL_FORMAT;
stMWConfig.stVBPoolConfig.astVBPoolSetup[1].u32BlkCount = 5;
stMWConfig.stVBPoolConfig.astVBPoolSetup[1].u32Height = stVencSize.u32Height;
stMWConfig.stVBPoolConfig.astVBPoolSetup[1].u32Width = stVencSize.u32Width;
stMWConfig.stVBPoolConfig.astVBPoolSetup[1].bBind = true;
stMWConfig.stVBPoolConfig.astVBPoolSetup[1].u32VpssChnBinding = VPSS_CHN1;
stMWConfig.stVBPoolConfig.astVBPoolSetup[1].u32VpssGrpBinding = (VPSS_GRP)0;
// VBPool 2 for TDL preprocessing
stMWConfig.stVBPoolConfig.astVBPoolSetup[2].enFormat = PIXEL_FORMAT_BGR_888_PLANAR;
stMWConfig.stVBPoolConfig.astVBPoolSetup[2].u32BlkCount = 3;
stMWConfig.stVBPoolConfig.astVBPoolSetup[2].u32Height = 720;
stMWConfig.stVBPoolConfig.astVBPoolSetup[2].u32Width = 1280;
stMWConfig.stVBPoolConfig.astVBPoolSetup[2].bBind = false;
// Setup VPSS Grp0
stMWConfig.stVPSSPoolConfig.u32VpssGrpCount = 1;
#ifndef CV186X
stMWConfig.stVPSSPoolConfig.stVpssMode.aenInput[0] = VPSS_INPUT_MEM;
stMWConfig.stVPSSPoolConfig.stVpssMode.enMode = VPSS_MODE_DUAL;
stMWConfig.stVPSSPoolConfig.stVpssMode.ViPipe[0] = 0;
stMWConfig.stVPSSPoolConfig.stVpssMode.aenInput[1] = VPSS_INPUT_ISP;
stMWConfig.stVPSSPoolConfig.stVpssMode.ViPipe[1] = 0;
#endif
SAMPLE_TDL_VPSS_CONFIG_S *pstVpssConfig = &stMWConfig.stVPSSPoolConfig.astVpssConfig[0];
pstVpssConfig->bBindVI = true;
// Assign device 1 to VPSS Grp0, because device1 has 3 outputs in dual mode.
VPSS_GRP_DEFAULT_HELPER2(&pstVpssConfig->stVpssGrpAttr, stSensorSize.u32Width,
stSensorSize.u32Height, VI_PIXEL_FORMAT, 1);
pstVpssConfig->u32ChnCount = 2;
pstVpssConfig->u32ChnBindVI = 0;
VPSS_CHN_DEFAULT_HELPER(&pstVpssConfig->astVpssChnAttr[0], stVencSize.u32Width,
stVencSize.u32Height, VI_PIXEL_FORMAT, true);
VPSS_CHN_DEFAULT_HELPER(&pstVpssConfig->astVpssChnAttr[1], stVencSize.u32Width,
stVencSize.u32Height, VI_PIXEL_FORMAT, true);
// Get default VENC configurations
SAMPLE_TDL_Get_Input_Config(&stMWConfig.stVencConfig.stChnInputCfg);
stMWConfig.stVencConfig.u32FrameWidth = stVencSize.u32Width;
stMWConfig.stVencConfig.u32FrameHeight = stVencSize.u32Height;
// Get default RTSP configurations
SAMPLE_TDL_Get_RTSP_Config(&stMWConfig.stRTSPConfig.stRTSPConfig);
SAMPLE_TDL_MW_CONTEXT stMWContext = {0};
s32Ret = SAMPLE_TDL_Init_WM(&stMWConfig, &stMWContext);
if (s32Ret != CVI_SUCCESS) {
printf("init middleware failed! ret=%x\n", s32Ret);
return -1;
}
cvitdl_handle_t stTDLHandle = NULL;
// Create TDL handle and assign VPSS Grp1 Device 0 to TDL SDK
GOTO_IF_FAILED(CVI_TDL_CreateHandle2(&stTDLHandle, 1, 0), s32Ret, create_tdl_fail);
GOTO_IF_FAILED(CVI_TDL_SetVBPool(stTDLHandle, 0, 2), s32Ret, create_service_fail);
CVI_TDL_SetVpssTimeout(stTDLHandle, 1000);
cvitdl_service_handle_t stServiceHandle = NULL;
GOTO_IF_FAILED(CVI_TDL_Service_CreateHandle(&stServiceHandle, stTDLHandle), s32Ret,
create_service_fail);
GOTO_IF_FAILED(CVI_TDL_OpenModel(stTDLHandle, CVI_TDL_SUPPORTED_MODEL_SCRFDFACE, argv[1]), s32Ret,
setup_tdl_fail);
pthread_t stVencThread, stTDLThread;
SAMPLE_TDL_VENC_THREAD_ARG_S args = {
.pstMWContext = &stMWContext,
.stServiceHandle = stServiceHandle,
};
pthread_create(&stVencThread, NULL, run_venc, &args);
pthread_create(&stTDLThread, NULL, run_tdl_thread, stTDLHandle);
pthread_join(stVencThread, NULL);
pthread_join(stTDLThread, NULL);
setup_tdl_fail:
CVI_TDL_Service_DestroyHandle(stServiceHandle);
create_service_fail:
CVI_TDL_DestroyHandle(stTDLHandle);
create_tdl_fail:
SAMPLE_TDL_Destroy_MW(&stMWContext);
return 0;
}