• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <pthread.h>
21 #include <signal.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <sys/ioctl.h>
26 #include <sys/prctl.h>
27 
28 #include "hi_mipi_tx.h"
29 #include "sdk.h"
30 #include "sample_comm.h"
31 #include "ai_infer_process.h"
32 #include "cnn_trash_classify.h"
33 #include "hand_classify.h"
34 #include "vgs_img.h"
35 #include "osd_img.h"
36 #include "posix_help.h"
37 #include "sample_media_ai.h"
38 
39 #ifdef __cplusplus
40 #if __cplusplus
41 extern "C" {
42 #endif
43 #endif /* End of #ifdef __cplusplus */
44 
45 static HI_BOOL g_bAiProcessStopSignal = HI_FALSE;
46 static HI_U32 g_num = 0;
47 AicMediaInfo g_aicMediaInfo = { 0 };
48 static pthread_t g_aiProcessThread = 0;
49 AiPlugLib g_workPlug = {0};
50 
51 #define HAND_FRM_WIDTH    640
52 #define HAND_FRM_HEIGHT   384
53 #define AI_SAMPLE_CFG_FILE      "./sample_ai.conf"
54 #define BUFFER_SIZE           16    // buffer size
55 
56 #define USLEEP_TIME   1000 // 1000: usleep time, in microseconds
57 #define G_MBUF_LENGTH 50 // 50: length of g_mbuf
58 #define ALIGN_DOWN_SIZE 2
59 
60 #define G_MBUF_ARRAY_SUBSCRIPT_0     0
61 #define G_MBUF_ARRAY_SUBSCRIPT_1     1
62 #define G_MBUF_ARRAY_SUBSCRIPT_2     2
63 #define G_MBUF_ARRAY_SUBSCRIPT_3     3
64 #define G_MBUF_ARRAY_SUBSCRIPT_4     4
65 #define G_MBUF_ARRAY_SUBSCRIPT_5     5
66 #define G_MBUF_ARRAY_SUBSCRIPT_6     6
67 #define G_MBUF_ARRAY_SUBSCRIPT_7     7
68 #define G_MBUF_ARRAY_SUBSCRIPT_8     8
69 #define G_MBUF_ARRAY_SUBSCRIPT_9     9
70 #define G_MBUF_ARRAY_SUBSCRIPT_10    10
71 #define G_MBUF_ARRAY_SUBSCRIPT_11    11
72 #define G_MBUF_ARRAY_SUBSCRIPT_12    12
73 #define G_MBUF_ARRAY_SUBSCRIPT_13    13
74 #define G_MBUF_ARRAY_SUBSCRIPT_14    14
75 #define G_MBUF_ARRAY_SUBSCRIPT_15    15
76 #define G_MBUF_ARRAY_SUBSCRIPT_16    16
77 
78 #define LANE_ID_SUBSCRIPT_0    0
79 #define LANE_ID_SUBSCRIPT_1    1
80 #define LANE_ID_SUBSCRIPT_2    2
81 #define LANE_ID_SUBSCRIPT_3    3
82 
83 static unsigned char g_mBuf[G_MBUF_LENGTH];
84 static SampleVoModeMux g_sampleVoModeMux = {0};
85 static VO_PUB_ATTR_S stVoPubAttr = {0};
86 static VO_VIDEO_LAYER_ATTR_S  stLayerAttr    = {0};
87 static VO_CSC_S               stVideoCSC     = {0};
88 static RECT_S stDefDispRect  = {0, 0, 800, 480};
89 static SIZE_S stDefImageSize = {800, 480};
90 
91 HI_CHAR acThreadName[16] = {0};
92 
93 typedef struct StSampleUserVoConfigs {
94     VO_SYNC_INFO_S stSyncInfo;
95     VO_USER_INTFSYNC_ATTR_S stUserIntfSyncAttr;
96     HI_U32 u32PreDiv;
97     HI_U32 u32DevDiv;
98     HI_U32 u32Framerate;
99     combo_dev_cfg_t stcombo_dev_cfgl;
100 } SAMPLE_USER_VO_CONFIG_S;
101 
SAMPLE_VOU_SYS_Exit(void)102 HI_VOID SAMPLE_VOU_SYS_Exit(void)
103 {
104     HI_MPI_SYS_Exit();
105     HI_MPI_VB_Exit();
106 }
107 
SAMPLE_VO_GetUserLayerAttr(VO_VIDEO_LAYER_ATTR_S * pstLayerAttr,SIZE_S * pstDevSize)108 HI_VOID SAMPLE_VO_GetUserLayerAttr(VO_VIDEO_LAYER_ATTR_S *pstLayerAttr, SIZE_S *pstDevSize)
109 {
110     pstLayerAttr->bClusterMode = HI_FALSE;
111     pstLayerAttr->bDoubleFrame = HI_FALSE;
112     pstLayerAttr->enDstDynamicRange = DYNAMIC_RANGE_SDR8;
113     pstLayerAttr->enPixFormat = PIXEL_FORMAT_YVU_SEMIPLANAR_420;
114 
115     pstLayerAttr->stDispRect.s32X = 0;
116     pstLayerAttr->stDispRect.s32Y = 0;
117     pstLayerAttr->stDispRect.u32Height = pstDevSize->u32Height;
118     pstLayerAttr->stDispRect.u32Width  = pstDevSize->u32Width;
119 
120     pstLayerAttr->stImageSize.u32Height = pstDevSize->u32Height;
121     pstLayerAttr->stImageSize.u32Width = pstDevSize->u32Width;
122 
123     return;
124 }
125 
SAMPLE_VO_GetUserChnAttr(VO_CHN_ATTR_S * pstChnAttr,SIZE_S * pstDevSize,HI_S32 VoChnNum)126 HI_VOID SAMPLE_VO_GetUserChnAttr(VO_CHN_ATTR_S *pstChnAttr, SIZE_S *pstDevSize, HI_S32 VoChnNum)
127 {
128     HI_S32 i;
129     for (i = 0; i < VoChnNum; i++) {
130         pstChnAttr[i].bDeflicker = HI_FALSE;
131         pstChnAttr[i].u32Priority = 0;
132         pstChnAttr[i].stRect.s32X = 0;
133         pstChnAttr[i].stRect.s32Y = 0;
134         pstChnAttr[i].stRect.u32Height = pstDevSize->u32Height;
135         pstChnAttr[i].stRect.u32Width = pstDevSize->u32Width;
136         }
137 
138     return;
139 }
140 
141 /* open mipi_tx device */
SampleOpenMipiTxFd(HI_VOID)142 HI_S32 SampleOpenMipiTxFd(HI_VOID)
143 {
144     HI_S32 fd;
145 
146     fd = open("/dev/hi_mipi_tx", O_RDWR);
147     if (fd < 0) {
148         printf("open hi_mipi_tx dev failed\n");
149     }
150     return fd;
151 }
152 
153 /* close mipi_tx device */
SampleCloseMipiTxFd(HI_S32 fd)154 HI_VOID SampleCloseMipiTxFd(HI_S32 fd)
155 {
156     close(fd);
157     return;
158 }
159 
160 /* get mipi tx config information */
SAMPLE_GetMipiTxConfig(combo_dev_cfg_t * pstMipiTxConfig)161 HI_VOID SAMPLE_GetMipiTxConfig(combo_dev_cfg_t *pstMipiTxConfig)
162 {
163     /* USER NEED SET MIPI DEV CONFIG */
164     pstMipiTxConfig->devno = 0;
165     pstMipiTxConfig->lane_id[LANE_ID_SUBSCRIPT_0] = 0;
166     pstMipiTxConfig->lane_id[LANE_ID_SUBSCRIPT_1] = 1;
167     // -1: 2 lane mode configuration,lane_id[4] = {0, 1, -1, -1}
168     pstMipiTxConfig->lane_id[LANE_ID_SUBSCRIPT_2] = -1;
169     // -1: 2 lane mode configuration,lane_id[4] = {0, 1, -1, -1}
170     pstMipiTxConfig->lane_id[LANE_ID_SUBSCRIPT_3] = -1;
171     pstMipiTxConfig->output_mode = OUTPUT_MODE_DSI_VIDEO;
172     pstMipiTxConfig->output_format = OUT_FORMAT_RGB_24_BIT;
173     pstMipiTxConfig->video_mode = BURST_MODE;
174     pstMipiTxConfig->sync_info.vid_pkt_size = 480; // 480: received packet size
175     pstMipiTxConfig->sync_info.vid_hsa_pixels = 10; // 10: The number of pixels in the input line sync pulse area
176     pstMipiTxConfig->sync_info.vid_hbp_pixels = 50; // 50: Number of pixels in blanking area after input
177     pstMipiTxConfig->sync_info.vid_hline_pixels = 590; // 590: The total number of pixels detected per line
178     pstMipiTxConfig->sync_info.vid_vsa_lines = 4; // 4: Number of frame sync pulse lines detected
179     pstMipiTxConfig->sync_info.vid_vbp_lines = 20; // 20: Number of blanking area lines after frame sync pulse
180     pstMipiTxConfig->sync_info.vid_vfp_lines = 20; // 20: Number of blanking area lines before frame sync pulse
181     pstMipiTxConfig->sync_info.vid_active_lines = 800; // 800: VACTIVE rows
182     pstMipiTxConfig->sync_info.edpi_cmd_size = 0; // 0: Write memory command bytes
183     pstMipiTxConfig->phy_data_rate = 359; // 359: MIPI Tx output rate
184     pstMipiTxConfig->pixel_clk = 29878; // 29878: pixel clock. The unit is KHz
185 
186     return;
187 }
188 
189 /* set mipi tx config information */
SAMPLE_SetMipiTxConfig(HI_S32 fd,combo_dev_cfg_t * pstMipiTxConfig)190 HI_S32 SAMPLE_SetMipiTxConfig(HI_S32 fd, combo_dev_cfg_t *pstMipiTxConfig)
191 {
192     HI_S32 s32Ret = ioctl(fd, HI_MIPI_TX_SET_DEV_CFG, pstMipiTxConfig);
193     if (s32Ret != HI_SUCCESS) {
194         printf("MIPI_TX SET_DEV_CONFIG failed\n");
195         SampleCloseMipiTxFd(fd);
196         return s32Ret;
197     }
198     return s32Ret;
199 }
200 
201 /* set mipi tx device config */
SampleSetMipiTxDevAttr(HI_S32 fd)202 HI_S32 SampleSetMipiTxDevAttr(HI_S32 fd)
203 {
204     HI_S32 s32Ret;
205     combo_dev_cfg_t stMipiTxConfig;
206 
207     /* USER SET MIPI DEV CONFIG */
208     SAMPLE_GetMipiTxConfig(&stMipiTxConfig);
209 
210     /* USER SET MIPI DEV CONFIG */
211     s32Ret = SAMPLE_SetMipiTxConfig(fd, &stMipiTxConfig);
212 
213     return s32Ret;
214 }
215 
216 /* init mipi tx device */
SAMPLE_USER_INIT_MIPITx(HI_S32 fd,cmd_info_t * pcmd_info)217 HI_S32 SAMPLE_USER_INIT_MIPITx(HI_S32 fd, cmd_info_t *pcmd_info)
218 {
219     HI_S32 s32Ret = ioctl(fd, HI_MIPI_TX_SET_CMD, pcmd_info);
220     if (s32Ret !=  HI_SUCCESS) {
221         printf("MIPI_TX SET CMD failed\n");
222         SampleCloseMipiTxFd(fd);
223         return s32Ret;
224     }
225 
226     return HI_SUCCESS;
227 }
228 
229 /* set mipi tx device init screen */
SampleVoInitMipiTxScreen(HI_S32 fd)230 HI_S32 SampleVoInitMipiTxScreen(HI_S32 fd)
231 {
232     HI_S32 s32Ret;
233     cmd_info_t cmd_info;
234     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
235     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xFF;
236     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0x77;
237     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0x01;
238     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_3] = 0x00;
239     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_4] = 0x00;
240     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_5] = 0x13;
241     cmd_info.devno = 0;
242     cmd_info.cmd_size = 6; // 6: command data size
243     cmd_info.data_type = 0x29;
244     cmd_info.cmd = g_mBuf;
245     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
246     if (s32Ret != HI_SUCCESS) {
247         return s32Ret;
248     }
249     usleep(USLEEP_TIME);
250 
251     cmd_info.devno = 0;
252     cmd_info.cmd_size = 0x08ef;
253     cmd_info.data_type = 0x23;
254     cmd_info.cmd = NULL;
255     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
256     if (s32Ret != HI_SUCCESS) {
257         return s32Ret;
258     }
259     usleep(USLEEP_TIME);
260 
261     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
262     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xFF;
263     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0x77;
264     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0x01;
265     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_3] = 0x00;
266     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_4] = 0x00;
267     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_5] = 0x10;
268     cmd_info.devno = 0;
269     cmd_info.cmd_size = 6; // 6: command data size
270     cmd_info.data_type = 0x29;
271     cmd_info.cmd = g_mBuf;
272     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
273     if (s32Ret != HI_SUCCESS) {
274         return s32Ret;
275     }
276     usleep(USLEEP_TIME);
277 
278     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
279     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xC0;
280     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0x63;
281     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0x00;
282     cmd_info.devno = 0;
283     cmd_info.cmd_size = 3; // 3: command data size
284     cmd_info.data_type = 0x29;
285     cmd_info.cmd = g_mBuf;
286     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
287     if (s32Ret != HI_SUCCESS) {
288         return s32Ret;
289     }
290     usleep(USLEEP_TIME);
291 
292     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
293     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xC1;
294     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0x10;
295     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0x02;
296     cmd_info.devno = 0;
297     cmd_info.cmd_size = 3; // 3: command data size
298     cmd_info.data_type = 0x29;
299     cmd_info.cmd = g_mBuf;
300     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
301     if (s32Ret != HI_SUCCESS) {
302         return s32Ret;
303     }
304     usleep(USLEEP_TIME);
305 
306     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
307     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xC2;
308     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0x01;
309     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0x08;
310     cmd_info.devno = 0;
311     cmd_info.cmd_size = 3; // 3: command data size
312     cmd_info.data_type = 0x29;
313     cmd_info.cmd = g_mBuf;
314     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
315     if (s32Ret != HI_SUCCESS) {
316         return s32Ret;
317     }
318     usleep(USLEEP_TIME);
319 
320     cmd_info.devno = 0;
321     cmd_info.cmd_size = 0x18CC;
322     cmd_info.data_type = 0x23;
323     cmd_info.cmd = NULL;
324     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
325     if (s32Ret != HI_SUCCESS) {
326         return s32Ret;
327     }
328     usleep(USLEEP_TIME);
329 
330     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
331     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xB0;
332     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0x40;
333     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0xC9;
334     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_3] = 0x8F;
335     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_4] = 0x0D;
336     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_5] = 0x11;
337     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_6] = 0x07;
338     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_7] = 0x02;
339     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_8] = 0x09;
340     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_9] = 0x09;
341     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_10] = 0x1F;
342     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_11] = 0x04;
343     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_12] = 0x50;
344     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_13] = 0x0F;
345     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_14] = 0xE4;
346     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_15] = 0x29;
347     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_16] = 0xDF;
348     cmd_info.devno = 0;
349     cmd_info.cmd_size = 17; // 17: command data size
350     cmd_info.data_type = 0x29;
351     cmd_info.cmd = g_mBuf;
352     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
353     if (s32Ret != HI_SUCCESS) {
354         return s32Ret;
355     }
356     usleep(USLEEP_TIME);
357 
358     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
359     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xB1;
360     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0x40;
361     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0xCB;
362     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_3] = 0xD3;
363     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_4] = 0x11;
364     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_5] = 0x8F;
365     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_6] = 0x04;
366     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_7] = 0x00;
367     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_8] = 0x08;
368     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_9] = 0x07;
369     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_10] = 0x1C;
370     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_11] = 0x06;
371     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_12] = 0x53;
372     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_13] = 0x12;
373     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_14] = 0x63;
374     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_15] = 0xEB;
375     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_16] = 0xDF;
376     cmd_info.devno = 0;
377     cmd_info.cmd_size = 17; // 17: command data size
378     cmd_info.data_type = 0x29;
379     cmd_info.cmd = g_mBuf;
380     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
381     if (s32Ret != HI_SUCCESS) {
382         return s32Ret;
383     }
384     usleep(USLEEP_TIME);
385 
386     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
387     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xFF;
388     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0x77;
389     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0x01;
390     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_3] = 0x00;
391     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_4] = 0x00;
392     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_5] = 0x11;
393     cmd_info.devno = 0;
394     cmd_info.cmd_size = 6; // 6: command data size
395     cmd_info.data_type = 0x29;
396     cmd_info.cmd = g_mBuf;
397     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
398     if (s32Ret != HI_SUCCESS) {
399         return s32Ret;
400     }
401     usleep(USLEEP_TIME);
402 
403     cmd_info.devno = 0;
404     cmd_info.cmd_size = 0x65b0;
405     cmd_info.data_type = 0x23;
406     cmd_info.cmd = NULL;
407     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
408     if (s32Ret != HI_SUCCESS) {
409         return s32Ret;
410     }
411     usleep(USLEEP_TIME);
412 
413     cmd_info.devno = 0;
414     cmd_info.cmd_size = 0x34b1;
415     cmd_info.data_type = 0x23;
416     cmd_info.cmd = NULL;
417     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
418     if (s32Ret != HI_SUCCESS) {
419         return s32Ret;
420     }
421     usleep(USLEEP_TIME);
422 
423     cmd_info.devno = 0;
424     cmd_info.cmd_size = 0x87b2;
425     cmd_info.data_type = 0x23;
426     cmd_info.cmd = NULL;
427     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
428     if (s32Ret != HI_SUCCESS) {
429         return s32Ret;
430     }
431     usleep(USLEEP_TIME);
432 
433     cmd_info.devno = 0;
434     cmd_info.cmd_size = 0x80b3;
435     cmd_info.data_type = 0x23;
436     cmd_info.cmd = NULL;
437     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
438     if (s32Ret != HI_SUCCESS) {
439         return s32Ret;
440     }
441     usleep(USLEEP_TIME);
442 
443     cmd_info.devno = 0;
444     cmd_info.cmd_size = 0x49b5;
445     cmd_info.data_type = 0x23;
446     cmd_info.cmd = NULL;
447     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
448     if (s32Ret != HI_SUCCESS) {
449         return s32Ret;
450     }
451     usleep(USLEEP_TIME);
452 
453     cmd_info.devno = 0;
454     cmd_info.cmd_size = 0x85b7;
455     cmd_info.data_type = 0x23;
456     cmd_info.cmd = NULL;
457     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
458     if (s32Ret != HI_SUCCESS) {
459         return s32Ret;
460     }
461     usleep(USLEEP_TIME);
462 
463     cmd_info.devno = 0;
464     cmd_info.cmd_size = 0x20b8;
465     cmd_info.data_type = 0x23;
466     cmd_info.cmd = NULL;
467     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
468     if (s32Ret != HI_SUCCESS) {
469         return s32Ret;
470     }
471     usleep(USLEEP_TIME);
472 
473     cmd_info.devno = 0;
474     cmd_info.cmd_size = 0x10b9;
475     cmd_info.data_type = 0x23;
476     cmd_info.cmd = NULL;
477     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
478     if (s32Ret != HI_SUCCESS) {
479         return s32Ret;
480     }
481     usleep(USLEEP_TIME);
482 
483     cmd_info.devno = 0;
484     cmd_info.cmd_size = 0x78c1;
485     cmd_info.data_type = 0x23;
486     cmd_info.cmd = NULL;
487     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
488     if (s32Ret != HI_SUCCESS) {
489         return s32Ret;
490     }
491     usleep(USLEEP_TIME);
492 
493     cmd_info.devno = 0;
494     cmd_info.cmd_size = 0x78c2;
495     cmd_info.data_type = 0x23;
496     cmd_info.cmd = NULL;
497     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
498     if (s32Ret != HI_SUCCESS) {
499         return s32Ret;
500     }
501     usleep(USLEEP_TIME);
502 
503     cmd_info.devno = 0;
504     cmd_info.cmd_size = 0x88d0;
505     cmd_info.data_type = 0x23;
506     cmd_info.cmd = NULL;
507     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
508     if (s32Ret != HI_SUCCESS) {
509         return s32Ret;
510     }
511     usleep(USLEEP_TIME);
512     usleep(100000);  // 100000: The process hangs for a period of time, in microseconds
513 
514     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
515     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xE0;
516     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0x00;
517     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0x19;
518     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_3] = 0x02;
519     cmd_info.devno = 0;
520     cmd_info.cmd_size = 4; // 4: command data size
521     cmd_info.data_type = 0x29;
522     cmd_info.cmd = g_mBuf;
523     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
524     if (s32Ret != HI_SUCCESS) {
525         return s32Ret;
526     }
527     usleep(USLEEP_TIME);
528     usleep(USLEEP_TIME);
529 
530     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
531     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xE1;
532     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0x05;
533     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0xA0;
534     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_3] = 0x07;
535     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_4] = 0xA0;
536     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_5] = 0x04;
537     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_6] = 0xA0;
538     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_7] = 0x06;
539     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_8] = 0xA0;
540     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_9] = 0x00;
541     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_10] = 0x44;
542     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_11] = 0x44;
543     cmd_info.devno = 0;
544     cmd_info.cmd_size = 12; // 12: command data size
545     cmd_info.data_type = 0x29;
546     cmd_info.cmd = g_mBuf;
547     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
548     if (s32Ret != HI_SUCCESS) {
549         return s32Ret;
550     }
551     usleep(USLEEP_TIME);
552 
553     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
554     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xE2;
555     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0x00;
556     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0x00;
557     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_3] = 0x00;
558     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_4] = 0x00;
559     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_5] = 0x00;
560     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_6] = 0x00;
561     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_7] = 0x00;
562     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_8] = 0x00;
563     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_9] = 0x00;
564     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_10] = 0x00;
565     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_11] = 0x00;
566     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_12] = 0x00;
567     cmd_info.devno = 0;
568     cmd_info.cmd_size = 13; // 13: command data size
569     cmd_info.data_type = 0x29;
570     cmd_info.cmd = g_mBuf;
571     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
572     if (s32Ret != HI_SUCCESS) {
573         return s32Ret;
574     }
575     usleep(USLEEP_TIME);
576 
577     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
578     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xE3;
579     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0x00;
580     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0x00;
581     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_3] = 0x33;
582     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_4] = 0x33;
583     cmd_info.devno = 0;
584     cmd_info.cmd_size = 5; // 5: command data size
585     cmd_info.data_type = 0x29;
586     cmd_info.cmd = g_mBuf;
587     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
588     if (s32Ret != HI_SUCCESS) {
589         return s32Ret;
590     }
591     usleep(USLEEP_TIME);
592 
593     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
594     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xE4;
595     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0x44;
596     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0x44;
597     cmd_info.devno = 0;
598     cmd_info.cmd_size = 3; // 3: command data size
599     cmd_info.data_type = 0x29;
600     cmd_info.cmd = g_mBuf;
601     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
602     if (s32Ret != HI_SUCCESS) {
603         return s32Ret;
604     }
605     usleep(USLEEP_TIME);
606 
607     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
608     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xE5;
609     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0x0D;
610     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0x31;
611     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_3] = 0xC8;
612     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_4] = 0xAF;
613     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_5] = 0x0F;
614     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_6] = 0x33;
615     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_7] = 0xC8;
616     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_8] = 0xAF;
617     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_9] = 0x09;
618     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_10] = 0x2D;
619     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_11] = 0xC8;
620     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_12] = 0xAF;
621     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_13] = 0x0B;
622     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_14] = 0x2F;
623     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_15] = 0xC8;
624     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_16] = 0xAF;
625     cmd_info.devno = 0;
626     cmd_info.cmd_size = 17; // 17: command data size
627     cmd_info.data_type = 0x29;
628     cmd_info.cmd = g_mBuf;
629     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
630     if (s32Ret != HI_SUCCESS) {
631         return s32Ret;
632     }
633     usleep(USLEEP_TIME);
634 
635     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
636     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xE6;
637     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0x00;
638     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0x00;
639     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_3] = 0x33;
640     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_4] = 0x33;
641     cmd_info.devno = 0;
642     cmd_info.cmd_size = 5; // 5: command data size
643     cmd_info.data_type = 0x29;
644     cmd_info.cmd = g_mBuf;
645     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
646     if (s32Ret != HI_SUCCESS) {
647         return s32Ret;
648     }
649     usleep(USLEEP_TIME);
650 
651     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
652     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xE7;
653     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0x44;
654     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0x44;
655     cmd_info.devno = 0;
656     cmd_info.cmd_size = 3; // 3: command data size
657     cmd_info.data_type = 0x29;
658     cmd_info.cmd = g_mBuf;
659     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
660     if (s32Ret != HI_SUCCESS) {
661         return s32Ret;
662     }
663     usleep(USLEEP_TIME);
664 
665     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
666     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xE8;
667     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0x0C;
668     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0x30;
669     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_3] = 0xC8;
670     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_4] = 0xAF;
671     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_5] = 0x0E;
672     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_6] = 0x32;
673     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_7] = 0xC8;
674     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_8] = 0xAF;
675     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_9] = 0x08;
676     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_10] = 0x2C;
677     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_11] = 0xC8;
678     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_12] = 0xAF;
679     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_13] = 0x0A;
680     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_14] = 0x2E;
681     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_15] = 0xC8;
682     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_16] = 0xAF;
683     cmd_info.devno = 0;
684     cmd_info.cmd_size = 17; // 17: command data size
685     cmd_info.data_type = 0x29;
686     cmd_info.cmd = g_mBuf;
687     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
688     if (s32Ret != HI_SUCCESS) {
689         return s32Ret;
690     }
691     usleep(USLEEP_TIME);
692 
693     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
694     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xEB;
695     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0x02;
696     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0x00;
697     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_3] = 0xE4;
698     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_4] = 0xE4;
699     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_5] = 0x44;
700     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_6] = 0x00;
701     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_7] = 0x40;
702     cmd_info.devno = 0;
703     cmd_info.cmd_size = 8; // 8: command data size
704     cmd_info.data_type = 0x29;
705     cmd_info.cmd = g_mBuf;
706     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
707     if (s32Ret != HI_SUCCESS) {
708         return s32Ret;
709     }
710     usleep(USLEEP_TIME);
711 
712     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
713     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xEC;
714     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0x3C;
715     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0x00;
716     cmd_info.devno = 0;
717     cmd_info.cmd_size = 3; // 3: command data size
718     cmd_info.data_type = 0x29;
719     cmd_info.cmd = g_mBuf;
720     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
721     if (s32Ret != HI_SUCCESS) {
722         return s32Ret;
723     }
724     usleep(USLEEP_TIME);
725 
726     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
727     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xED;
728     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0xAB;
729     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0x89;
730     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_3] = 0x76;
731     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_4] = 0x54;
732     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_5] = 0x01;
733     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_6] = 0xFF;
734     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_7] = 0xFF;
735     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_8] = 0xFF;
736     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_9] = 0xFF;
737     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_10] = 0xFF;
738     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_11] = 0xFF;
739     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_12] = 0x10;
740     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_13] = 0x45;
741     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_14] = 0x67;
742     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_15] = 0x98;
743     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_16] = 0xBA;
744     cmd_info.devno = 0;
745     cmd_info.cmd_size = 17; // 17: command data size
746     cmd_info.data_type = 0x29;
747     cmd_info.cmd = g_mBuf;
748     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
749     if (s32Ret != HI_SUCCESS) {
750         return s32Ret;
751     }
752     usleep(USLEEP_TIME);
753 
754     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
755     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xEF;
756     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0x08;
757     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0x08;
758     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_3] = 0x08;
759     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_4] = 0x45;
760     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_5] = 0x3F;
761     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_6] = 0x54;
762     cmd_info.devno = 0;
763     cmd_info.cmd_size = 7; // 7: command data size
764     cmd_info.data_type = 0x29;
765     cmd_info.cmd = g_mBuf;
766     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
767     if (s32Ret != HI_SUCCESS) {
768         return s32Ret;
769     }
770     usleep(USLEEP_TIME);
771 
772     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
773     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xFF;
774     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0x77;
775     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0x01;
776     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_3] = 0x00;
777     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_4] = 0x00;
778     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_5] = 0x00;
779     cmd_info.devno = 0;
780     cmd_info.cmd_size = 6; // 6: command data size
781     cmd_info.data_type = 0x29;
782     cmd_info.cmd = g_mBuf;
783     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
784     if (s32Ret != HI_SUCCESS) {
785         return s32Ret;
786     }
787     usleep(USLEEP_TIME);
788 
789     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
790     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xFF;
791     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0x77;
792     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0x01;
793     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_3] = 0x00;
794     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_4] = 0x00;
795     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_5] = 0x13;
796     cmd_info.devno = 0;
797     cmd_info.cmd_size = 6; // 6: command data size
798     cmd_info.data_type = 0x29;
799     cmd_info.cmd = g_mBuf;
800     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
801     if (s32Ret != HI_SUCCESS) {
802         return s32Ret;
803     }
804     usleep(USLEEP_TIME);
805 
806     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
807     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xE8;
808     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0x00;
809     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0x0E;
810     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_3] = 0x11;
811     cmd_info.devno = 0;
812     cmd_info.cmd_size = 4; // 4: command data size
813     cmd_info.data_type = 0x29;
814     cmd_info.cmd = g_mBuf;
815     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
816     if (s32Ret != HI_SUCCESS) {
817         return s32Ret;
818     }
819     usleep(USLEEP_TIME);
820     usleep(120000); // 120000: The process hangs for a period of time, in microseconds
821 
822     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
823     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xE8;
824     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0x00;
825     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0x0C;
826     cmd_info.devno = 0;
827     cmd_info.cmd_size = 3; // 3: command data size
828     cmd_info.data_type = 0x29;
829     cmd_info.cmd = g_mBuf;
830     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
831     if (s32Ret != HI_SUCCESS) {
832         return s32Ret;
833     }
834     usleep(USLEEP_TIME);
835     usleep(10000); // 10000: The process hangs for a period of time, in microseconds
836 
837     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
838     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xE8;
839     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0x00;
840     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0x00;
841     cmd_info.devno = 0;
842     cmd_info.cmd_size = 3; // 3: command data size
843     cmd_info.data_type = 0x29;
844     cmd_info.cmd = g_mBuf;
845     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
846     if (s32Ret != HI_SUCCESS) {
847         return s32Ret;
848     }
849     usleep(USLEEP_TIME);
850     usleep(10000); // 10000: The process hangs for a period of time, in microseconds
851 
852     memset_s(g_mBuf, G_MBUF_LENGTH, 0, G_MBUF_LENGTH);
853     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_0] = 0xFF;
854     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_1] = 0x77;
855     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_2] = 0x01;
856     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_3] = 0x00;
857     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_4] = 0x00;
858     g_mBuf[G_MBUF_ARRAY_SUBSCRIPT_5] = 0x00;
859     cmd_info.devno = 0;
860     cmd_info.cmd_size = 6; // 6: command data size
861     cmd_info.data_type = 0x29;
862     cmd_info.cmd = g_mBuf;
863     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
864     if (s32Ret != HI_SUCCESS) {
865         return s32Ret;
866     }
867     usleep(USLEEP_TIME);
868     usleep(10000); // 10000: The process hangs for a period of time, in microseconds
869 
870     cmd_info.devno = 0;
871     cmd_info.cmd_size = 0x11;
872     cmd_info.data_type = 0x05;
873     cmd_info.cmd = NULL;
874     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
875     if (s32Ret != HI_SUCCESS) {
876         return s32Ret;
877     }
878     usleep(USLEEP_TIME);
879     usleep(150000); // 150000: The process hangs for a period of time, in microseconds
880 
881     cmd_info.devno = 0;
882     cmd_info.cmd_size = 0x29;
883     cmd_info.data_type = 0x05;
884     cmd_info.cmd = NULL;
885     s32Ret = SAMPLE_USER_INIT_MIPITx(fd, &cmd_info);
886     if (s32Ret != HI_SUCCESS) {
887         return s32Ret;
888     }
889     usleep(USLEEP_TIME);
890     usleep(50000); // 50000: The process hangs for a period of time, in microseconds
891 
892     return HI_SUCCESS;
893 }
894 
895 /* enable mipi tx */
SAMPLE_VO_ENABLE_MIPITx(HI_S32 fd)896 HI_S32 SAMPLE_VO_ENABLE_MIPITx(HI_S32 fd)
897 {
898     HI_S32 s32Ret = ioctl(fd, HI_MIPI_TX_ENABLE);
899     if (s32Ret != HI_SUCCESS) {
900         printf("MIPI_TX enable failed\n");
901         return s32Ret;
902     }
903 
904     return s32Ret;
905 }
906 
907 /* disable mipi tx */
SAMPLE_VO_DISABLE_MIPITx(HI_S32 fd)908 HI_S32 SAMPLE_VO_DISABLE_MIPITx(HI_S32 fd)
909 {
910     HI_S32 s32Ret = ioctl(fd, HI_MIPI_TX_DISABLE);
911     if (s32Ret != HI_SUCCESS) {
912         printf("MIPI_TX disable failed\n");
913         return s32Ret;
914     }
915 
916     return s32Ret;
917 }
918 
919 /* onfig mipi */
SAMPLE_VO_CONFIG_MIPI(HI_S32 * mipiFD)920 HI_S32 SAMPLE_VO_CONFIG_MIPI(HI_S32* mipiFD)
921 {
922     HI_S32 s32Ret;
923     /* SET MIPI BAKCLIGHT */
924     HI_S32  fd;
925     /* CONFIG MIPI PINUMX */
926 
927     /* Reset MIPI */
928     /* OPEN MIPI FD */
929     fd = SampleOpenMipiTxFd();
930     if (fd < 0) {
931         return HI_FAILURE;
932     }
933 	*mipiFD = fd;
934 
935     /* SET MIPI Tx Dev ATTR */
936     s32Ret = SampleSetMipiTxDevAttr(fd);
937     if (s32Ret != HI_SUCCESS) {
938         return s32Ret;
939     }
940 
941     usleep(10000); // 10000: The process hangs for a period of time, in microseconds
942     system("cd /sys/class/gpio/;echo 5 > export;echo out > gpio5/direction;echo 1 > gpio5/value");
943     usleep(200000); // 200000: The process hangs for a period of time, in microseconds
944     system("echo 0 > /sys/class/gpio/gpio5/value");
945     usleep(200000); // 200000: The process hangs for a period of time, in microseconds
946     system("echo 1 > /sys/class/gpio/gpio5/value");
947     usleep(20000); // 20000: The process hangs for a period of time, in microseconds
948 
949     /* CONFIG MIPI Tx INITIALIZATION SEQUENCE */
950     s32Ret = SampleVoInitMipiTxScreen(fd);
951     if (s32Ret != HI_SUCCESS) {
952         return s32Ret;
953     }
954 
955     /* ENABLE MIPI Tx DEV */
956     s32Ret = SAMPLE_VO_ENABLE_MIPITx(fd);
957     if (s32Ret != HI_SUCCESS) {
958         return s32Ret;
959     }
960 
961     return s32Ret;
962 }
963 
964 /* get mipi device Height and width */
SampleCommVoGetWhMipi(VO_INTF_SYNC_E enIntfSync,HI_U32 * pu32W,HI_U32 * pu32H,HI_U32 * pu32Frm)965 HI_S32 SampleCommVoGetWhMipi(VO_INTF_SYNC_E enIntfSync, HI_U32* pu32W, HI_U32* pu32H, HI_U32* pu32Frm)
966 {
967     switch (enIntfSync) {
968         case VO_OUTPUT_1080P24:
969             *pu32W = 1920; // 1920: VO_OUTPUT_1080P24-Width
970             *pu32H = 1080; // 1080: VO_OUTPUT_1080P24-Height
971             *pu32Frm = 24; // 24: VO_OUTPUT_1080P24-Frame rate
972             break;
973         case VO_OUTPUT_1080P25:
974             *pu32W = 1920; // 1920: VO_OUTPUT_1080P25-Width
975             *pu32H = 1080; // 1080: VO_OUTPUT_1080P25-Height
976             *pu32Frm = 25; // 25: VO_OUTPUT_1080P25-Frame rate
977             break;
978         case VO_OUTPUT_1080P30:
979             *pu32W = 1920; // 1920: VO_OUTPUT_1080P30-Width
980             *pu32H = 1080; // 1080: VO_OUTPUT_1080P30-Height
981             *pu32Frm = 30; // 30: VO_OUTPUT_1080P30-Frame rate
982             break;
983         case VO_OUTPUT_720P50:
984             *pu32W = 1280; // 1280: VO_OUTPUT_720P50-Width
985             *pu32H = 720; // 720: VO_OUTPUT_720P50-Height
986             *pu32Frm = 50; // 50: VO_OUTPUT_720P50-Frame rate
987             break;
988         case VO_OUTPUT_720P60:
989             *pu32W = 1280; // 1280: VO_OUTPUT_720P60-Width
990             *pu32H = 720; // 720: VO_OUTPUT_720P60-Height
991             *pu32Frm = 60; // 60: VO_OUTPUT_720P60-Frame rate
992             break;
993         case VO_OUTPUT_1080P50:
994             *pu32W = 1920; // 1920: VO_OUTPUT_1080P50-Width
995             *pu32H = 1080; // 1080: VO_OUTPUT_1080P50-Height
996             *pu32Frm = 50; // 50: VO_OUTPUT_1080P50-Frame rate
997             break;
998         case VO_OUTPUT_1080P60:
999             *pu32W = 1920; // 1920: VO_OUTPUT_1080P60-Width
1000             *pu32H = 1080; // 1080: VO_OUTPUT_1080P60-Height
1001             *pu32Frm = 60; // 60: VO_OUTPUT_1080P60-Frame rate
1002             break;
1003         case VO_OUTPUT_USER:
1004             *pu32W = 800; // 800: VO_OUTPUT_USER-Width
1005             *pu32H = 480; // 480: VO_OUTPUT_USER-Height
1006             *pu32Frm = 60; // 60: VO_OUTPUT_USER-Frame rate
1007             break;
1008         default:
1009             SAMPLE_PRT("vo enIntfSync %d not support, please config self!\n", enIntfSync);
1010             return HI_FAILURE;
1011     }
1012 
1013     return HI_SUCCESS;
1014 }
1015 
SampleCommVoStartDevMipi(VO_DEV VoDev,VO_PUB_ATTR_S * pstPubAttr)1016 HI_S32 SampleCommVoStartDevMipi(VO_DEV VoDev, VO_PUB_ATTR_S* pstPubAttr)
1017 {
1018     HI_S32 s32Ret;
1019     VO_USER_INTFSYNC_INFO_S stUserInfo = {0};
1020 
1021     stUserInfo.bClkReverse = HI_TRUE;
1022     stUserInfo.u32DevDiv = 1;
1023     stUserInfo.u32PreDiv = 1;
1024     stUserInfo.stUserIntfSyncAttr.enClkSource = VO_CLK_SOURCE_PLL;
1025     stUserInfo.stUserIntfSyncAttr.stUserSyncPll.u32Fbdiv = 244; // 244: PLL integer frequency multiplier coefficient
1026     stUserInfo.stUserIntfSyncAttr.stUserSyncPll.u32Frac = 0x1A36;
1027     stUserInfo.stUserIntfSyncAttr.stUserSyncPll.u32Refdiv = 4; // 4: PLL reference clock frequency division coefficient
1028     // 7: PLL first stage output frequency division coefficient
1029     stUserInfo.stUserIntfSyncAttr.stUserSyncPll.u32Postdiv1 = 7;
1030     // 7: PLL second stage output frequency division coefficient
1031     stUserInfo.stUserIntfSyncAttr.stUserSyncPll.u32Postdiv2 = 7;
1032     HI_U32 u32Framerate = 60; // 60: device frame rate
1033 
1034     /* Set the common properties of the video output device */
1035     s32Ret = HI_MPI_VO_SetPubAttr(VoDev, pstPubAttr);
1036     if (s32Ret != HI_SUCCESS) {
1037         SAMPLE_PRT("failed with %#x!\n", s32Ret);
1038         return HI_FAILURE;
1039     }
1040 
1041     /* Set the device frame rate under the device user timing */
1042     s32Ret = HI_MPI_VO_SetDevFrameRate(VoDev, u32Framerate);
1043     if (s32Ret != HI_SUCCESS) {
1044         SAMPLE_PRT("failed with %#x!\n", s32Ret);
1045         return HI_FAILURE;
1046     }
1047 
1048     /* Set user interface timing information */
1049     s32Ret = HI_MPI_VO_SetUserIntfSyncInfo(VoDev, &stUserInfo);
1050     if (s32Ret != HI_SUCCESS) {
1051         SAMPLE_PRT("failed with %#x!\n", s32Ret);
1052         return HI_FAILURE;
1053     }
1054 
1055     /* Enable video output device */
1056     s32Ret = HI_MPI_VO_Enable(VoDev);
1057     if (s32Ret != HI_SUCCESS) {
1058         SAMPLE_PRT("failed with %#x!\n", s32Ret);
1059         return HI_FAILURE;
1060     }
1061 
1062     return s32Ret;
1063 }
1064 
SampleCommVoStartChnModeMux(SAMPLE_VO_MODE_E enMode)1065 HI_S32 SampleCommVoStartChnModeMux(SAMPLE_VO_MODE_E enMode)
1066 {
1067     int s32Ret;
1068     switch (enMode) {
1069         case VO_MODE_1MUX:
1070             g_sampleVoModeMux.u32WndNum = 1;
1071             g_sampleVoModeMux.u32Square = 1;
1072             break;
1073         case VO_MODE_2MUX:
1074             g_sampleVoModeMux.u32WndNum = 2; // 2: 2MUX-WndNum
1075             g_sampleVoModeMux.u32Square = 2; // 2: 2MUX-Square
1076             break;
1077         case VO_MODE_4MUX:
1078             g_sampleVoModeMux.u32WndNum = 4; // 4: 4MUX-WndNum
1079             g_sampleVoModeMux.u32Square = 2; // 2: 4MUX-Square
1080             break;
1081         case VO_MODE_8MUX:
1082             g_sampleVoModeMux.u32WndNum = 8; // 8: 8MUX-WndNum
1083             g_sampleVoModeMux.u32Square = 3; // 3: 8MUX-Square
1084             break;
1085         case VO_MODE_9MUX:
1086             g_sampleVoModeMux.u32WndNum = 9; // 9: 9MUX-WndNum
1087             g_sampleVoModeMux.u32Square = 3; // 3: 9MUX-Square
1088             break;
1089         case VO_MODE_16MUX:
1090             g_sampleVoModeMux.u32WndNum = 16; // 16: 16MUX-WndNum
1091             g_sampleVoModeMux.u32Square = 4; // 4: 16MUX-Square
1092             break;
1093         case VO_MODE_25MUX:
1094             g_sampleVoModeMux.u32WndNum = 25; // 25: 25MUX-WndNum
1095             g_sampleVoModeMux.u32Square = 5; // 5: 25MUX-Square
1096             break;
1097         case VO_MODE_36MUX:
1098             g_sampleVoModeMux.u32WndNum = 36; // 36: 36MUX-WndNum
1099             g_sampleVoModeMux.u32Square = 6; // 6: 36MUX-Square
1100             break;
1101         case VO_MODE_49MUX:
1102             g_sampleVoModeMux.u32WndNum = 49; // 49: 49MUX-WndNum
1103             g_sampleVoModeMux.u32Square = 7; // 7: 49MUX-Square
1104             break;
1105         case VO_MODE_2X4:
1106             g_sampleVoModeMux.u32WndNum = 8; // 8: 2X4-WndNum
1107             g_sampleVoModeMux.u32Square = 3; // 3: 2X4-Square
1108             g_sampleVoModeMux.u32Row    = 4; // 4: 2X4-Row
1109             g_sampleVoModeMux.u32Col    = 2; // 2: 2X4-Col
1110             break;
1111         default:
1112             SAMPLE_PRT("failed with %#x!\n", s32Ret);
1113             return HI_FAILURE;
1114     }
1115 
1116     return HI_SUCCESS;
1117 }
1118 
SampleCommVoStartChnMipi(VO_LAYER VoLayer,SAMPLE_VO_MODE_E enMode)1119 HI_S32 SampleCommVoStartChnMipi(VO_LAYER VoLayer, SAMPLE_VO_MODE_E enMode)
1120 {
1121     HI_S32 i;
1122     HI_S32 s32Ret    = HI_SUCCESS;
1123     HI_U32 u32Width  = 0;
1124     HI_U32 u32Height = 0;
1125     VO_CHN_ATTR_S         stChnAttr;
1126     VO_VIDEO_LAYER_ATTR_S stLayerAttr;
1127 
1128     s32Ret = SampleCommVoStartChnModeMux(enMode);
1129     SAMPLE_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, "for SampleCommVoStartChnModeMux FAIL, s32Ret=%x\n", s32Ret);
1130 
1131     /* Get video layer properties */
1132     s32Ret = HI_MPI_VO_GetVideoLayerAttr(VoLayer, &stLayerAttr);
1133     SAMPLE_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, "for HI_MPI_VO_GetVideoLayerAttr FAIL, s32Ret=%x\n", s32Ret);
1134     u32Width  = stLayerAttr.stImageSize.u32Width;
1135     u32Height = stLayerAttr.stImageSize.u32Height;
1136     SAMPLE_PRT("enMode:%d, u32Width:%d, u32Height:%d, u32Square:%d\n", enMode,
1137         u32Width, u32Height, g_sampleVoModeMux.u32Square);
1138 
1139     for (i = 0; i < g_sampleVoModeMux.u32WndNum; i++) {
1140         if (enMode == VO_MODE_1MUX  || enMode == VO_MODE_2MUX  || enMode == VO_MODE_4MUX  ||
1141             enMode == VO_MODE_8MUX  || enMode == VO_MODE_9MUX  || enMode == VO_MODE_16MUX ||
1142             enMode == VO_MODE_25MUX || enMode == VO_MODE_36MUX || enMode == VO_MODE_49MUX) {
1143             stChnAttr.stRect.s32X       = HI_ALIGN_DOWN((u32Width / g_sampleVoModeMux.u32Square) *
1144                 (i % g_sampleVoModeMux.u32Square), ALIGN_DOWN_SIZE);
1145             stChnAttr.stRect.s32Y       = HI_ALIGN_DOWN((u32Height / g_sampleVoModeMux.u32Square) *
1146                 (i / g_sampleVoModeMux.u32Square), ALIGN_DOWN_SIZE);
1147             stChnAttr.stRect.u32Width   = HI_ALIGN_DOWN(u32Width / g_sampleVoModeMux.u32Square, ALIGN_DOWN_SIZE);
1148             stChnAttr.stRect.u32Height  = HI_ALIGN_DOWN(u32Height / g_sampleVoModeMux.u32Square, ALIGN_DOWN_SIZE);
1149             stChnAttr.u32Priority       = 0;
1150             stChnAttr.bDeflicker        = HI_FALSE;
1151         } else if (enMode == VO_MODE_2X4) {
1152             stChnAttr.stRect.s32X       = HI_ALIGN_DOWN((u32Width / g_sampleVoModeMux.u32Col) *
1153                 (i % g_sampleVoModeMux.u32Col), ALIGN_DOWN_SIZE);
1154             stChnAttr.stRect.s32Y       = HI_ALIGN_DOWN((u32Height / g_sampleVoModeMux.u32Row) *
1155                 (i / g_sampleVoModeMux.u32Col), ALIGN_DOWN_SIZE);
1156             stChnAttr.stRect.u32Width   = HI_ALIGN_DOWN(u32Width / g_sampleVoModeMux.u32Col, ALIGN_DOWN_SIZE);
1157             stChnAttr.stRect.u32Height  = HI_ALIGN_DOWN(u32Height / g_sampleVoModeMux.u32Row, ALIGN_DOWN_SIZE);
1158             stChnAttr.u32Priority       = 0;
1159             stChnAttr.bDeflicker        = HI_FALSE;
1160         }
1161 
1162         /* Set properties for the specified video output channel */
1163         s32Ret = HI_MPI_VO_SetChnAttr(VoLayer, i, &stChnAttr);
1164         SAMPLE_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, "for HI_MPI_VO_SetChnAttr FAIL, s32Ret=%x\n", s32Ret);
1165 
1166         /* Set video output channel rotation angle */
1167         s32Ret = HI_MPI_VO_SetChnRotation(VoLayer, i, ROTATION_90);
1168         SAMPLE_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, "for HI_MPI_VO_SetChnRotation FAIL, s32Ret=%x\n", s32Ret);
1169 
1170         /* Enables the specified video output channel */
1171         s32Ret = HI_MPI_VO_EnableChn(VoLayer, i);
1172         SAMPLE_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, "for HI_MPI_VO_EnableChn FAIL, s32Ret=%x\n", s32Ret);
1173     }
1174 
1175     return HI_SUCCESS;
1176 }
1177 
StVoPubAttrCfg(SAMPLE_VO_CONFIG_S * pstVoConfig)1178 static HI_VOID StVoPubAttrCfg(SAMPLE_VO_CONFIG_S *pstVoConfig)
1179 {
1180     HI_ASSERT(pstVoConfig);
1181     /* Set and start VO device VoDev */
1182     stVoPubAttr.enIntfType  = VO_INTF_MIPI;
1183     stVoPubAttr.enIntfSync  = VO_OUTPUT_USER;
1184     stVoPubAttr.stSyncInfo.bSynm = 0;
1185     stVoPubAttr.stSyncInfo.bIop = 1;
1186     stVoPubAttr.stSyncInfo.u8Intfb = 0;
1187 
1188     stVoPubAttr.stSyncInfo.u16Hmid = 1;
1189     stVoPubAttr.stSyncInfo.u16Bvact = 1;
1190     stVoPubAttr.stSyncInfo.u16Bvbb = 1;
1191     stVoPubAttr.stSyncInfo.u16Bvfb = 1;
1192 
1193     stVoPubAttr.stSyncInfo.bIdv = 0;
1194     stVoPubAttr.stSyncInfo.bIhs = 0;
1195     stVoPubAttr.stSyncInfo.bIvs = 0;
1196 
1197     stVoPubAttr.stSyncInfo.u16Hact = 480; // 480: Horizontal effective area. Unit: pixel
1198     stVoPubAttr.stSyncInfo.u16Hbb = 60; // 60: Horizontal blanking of the rear shoulder. Unit: pixel
1199     stVoPubAttr.stSyncInfo.u16Hfb = 50; // 50: Horizontal blanking of the front shoulder. Unit: pixel
1200     stVoPubAttr.stSyncInfo.u16Hpw = 10; // 10: The width of the horizontal sync signal. Unit: pixel
1201     stVoPubAttr.stSyncInfo.u16Vact = 800; // 800: Vertical effective area. Unit: line
1202     stVoPubAttr.stSyncInfo.u16Vbb = 24; // 24: Vertical blanking of the rear shoulder.  Unit: line
1203     stVoPubAttr.stSyncInfo.u16Vfb = 20; // 20: Vertical blanking of the front shoulder.  Unit: line
1204     stVoPubAttr.stSyncInfo.u16Vpw = 4; // 4: The width of the vertical sync signal. Unit: line
1205     stVoPubAttr.u32BgColor  = pstVoConfig->u32BgColor;
1206 }
1207 
StLayerAttrCfg(SAMPLE_VO_CONFIG_S * pstVoConfig)1208 static HI_VOID StLayerAttrCfg(SAMPLE_VO_CONFIG_S *pstVoConfig)
1209 {
1210     HI_ASSERT(pstVoConfig);
1211     stLayerAttr.bClusterMode     = HI_FALSE;
1212     stLayerAttr.bDoubleFrame    = HI_FALSE;
1213     stLayerAttr.enPixFormat       = pstVoConfig->enPixFormat;
1214 
1215     stLayerAttr.stDispRect.s32X = 0;
1216     stLayerAttr.stDispRect.s32Y = 0;
1217     stLayerAttr.enDstDynamicRange     = pstVoConfig->enDstDynamicRange;
1218 }
1219 
1220 /* start vo to mipi lcd */
SampleCommVoStartMipi(SAMPLE_VO_CONFIG_S * pstVoConfig)1221 HI_S32 SampleCommVoStartMipi(SAMPLE_VO_CONFIG_S *pstVoConfig)
1222 {
1223     HI_S32 s32Ret;
1224 
1225     HI_ASSERT(pstVoConfig);
1226     StVoPubAttrCfg(pstVoConfig);
1227     s32Ret = SampleCommVoStartDevMipi(pstVoConfig->VoDev, &stVoPubAttr);
1228     SAMPLE_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, "StartDevMipi FAIL, ret=%x\n", s32Ret);
1229     /* Set and start layer VoDev */
1230     s32Ret = SampleCommVoGetWhMipi(stVoPubAttr.enIntfSync,
1231         &stLayerAttr.stDispRect.u32Width, &stLayerAttr.stDispRect.u32Height, &stLayerAttr.u32DispFrmRt);
1232     SAMPLE_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, FAIL, "VoGetWhMipi fail, err(%#x)\n", s32Ret);
1233 
1234     StLayerAttrCfg(pstVoConfig);
1235 
1236     /* Set display rectangle if changed */
1237     if (memcmp(&pstVoConfig->stDispRect, &stDefDispRect, sizeof(RECT_S)) != 0) {
1238         memcpy_s(&stLayerAttr.stDispRect, sizeof(stLayerAttr.stDispRect),
1239             &pstVoConfig->stDispRect, sizeof(RECT_S));
1240     }
1241 
1242     /* Set image size if changed */
1243     if (memcmp(&pstVoConfig->stImageSize, &stDefImageSize, sizeof(SIZE_S)) != 0) {
1244         memcpy_s(&stLayerAttr.stImageSize, sizeof(stLayerAttr.stImageSize),
1245             &pstVoConfig->stImageSize, sizeof(SIZE_S));
1246     }
1247     stLayerAttr.stImageSize.u32Width  = stLayerAttr.stDispRect.u32Width = 480; // 480: video layer canvas Width
1248     stLayerAttr.stImageSize.u32Height = stLayerAttr.stDispRect.u32Height = 800; // 800: video layer canvas Height
1249 
1250     if (pstVoConfig->u32DisBufLen) {
1251         /* Set buffer length */
1252         s32Ret = HI_MPI_VO_SetDisplayBufLen(pstVoConfig->VoDev, pstVoConfig->u32DisBufLen);
1253         SAMPLE_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, FAIL, "HI_MPI_VO_SetDisplayBufLen fail, err(%#x)\n", s32Ret);
1254     }
1255     if (VO_PART_MODE_MULTI == pstVoConfig->enVoPartMode) {
1256         /* Set the segmentation mode of the video layer */
1257         s32Ret = HI_MPI_VO_SetVideoLayerPartitionMode(pstVoConfig->VoDev, pstVoConfig->enVoPartMode);
1258         SAMPLE_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, FAIL, "SetVideoLayerMode fail, err(%#x)\n", s32Ret);
1259     }
1260 
1261     s32Ret = SAMPLE_COMM_VO_StartLayer(pstVoConfig->VoDev, &stLayerAttr); // start layer
1262     SAMPLE_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, FAIL, "VO_StartLayer fail, err(%#x)\n", s32Ret);
1263 
1264     if (VO_INTF_MIPI == pstVoConfig->enVoIntfType) {
1265         s32Ret = HI_MPI_VO_GetVideoLayerCSC(pstVoConfig->VoDev, &stVideoCSC); // get video layerCSC
1266         SAMPLE_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, FAIL, "GetVideoLayerCSC fail, err(%#x)\n", s32Ret);
1267         stVideoCSC.enCscMatrix = VO_CSC_MATRIX_BT709_TO_RGB_PC;
1268         s32Ret = HI_MPI_VO_SetVideoLayerCSC(pstVoConfig->VoDev, &stVideoCSC); // Set video layer CSC
1269         SAMPLE_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, FAIL, "SetVideoLayerCSC fail, err(%#x)\n", s32Ret);
1270     }
1271 
1272     s32Ret = SampleCommVoStartChnMipi(pstVoConfig->VoDev, pstVoConfig->enVoMode); // start vo channels
1273     SAMPLE_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, FAIL1, "VoStartChnMipi fail, err(%#x)\n", s32Ret);
1274     return HI_SUCCESS;
1275 
1276 FAIL1:
1277     SAMPLE_COMM_VO_StopLayer(pstVoConfig->VoDev);
1278 FAIL:
1279     SAMPLE_COMM_VO_StopDev(pstVoConfig->VoDev);
1280     return s32Ret;
1281 }
1282 
1283 /* init ViCfg */
ViCfgInit(ViCfg * self)1284 void ViCfgInit(ViCfg* self)
1285 {
1286     HI_ASSERT(self);
1287     if (memset_s(self, sizeof(*self), 0, sizeof(*self)) != EOK) {
1288         HI_ASSERT(0);
1289     }
1290 
1291     SAMPLE_COMM_VI_GetSensorInfo(self);
1292     self->s32WorkingViNum = 1;
1293     self->as32WorkingViId[0] = 0;
1294 
1295     self->astViInfo[0].stSnsInfo.MipiDev =
1296         SAMPLE_COMM_VI_GetComboDevBySensor(self->astViInfo[0].stSnsInfo.enSnsType, 0);
1297     self->astViInfo[0].stSnsInfo.s32BusId = 0;
1298 }
1299 
1300 /* Set VI DEV information */
ViCfgSetDev(ViCfg * self,int devId,WDR_MODE_E wdrMode)1301 void ViCfgSetDev(ViCfg* self, int devId, WDR_MODE_E wdrMode)
1302 {
1303     HI_ASSERT(self);
1304     HI_ASSERT((int)wdrMode < WDR_MODE_BUTT);
1305 
1306     self->astViInfo[0].stDevInfo.ViDev = devId;
1307     self->astViInfo[0].stDevInfo.enWDRMode = (int)wdrMode < 0 ? WDR_MODE_NONE : wdrMode;
1308 }
1309 
1310 /* Set the PIPE information of the VI */
ViCfgSetPipe(ViCfg * self,int pipe0Id,int pipe1Id,int pipe2Id,int pipe3Id)1311 void ViCfgSetPipe(ViCfg* self, int pipe0Id, int pipe1Id, int pipe2Id, int pipe3Id)
1312 {
1313     HI_ASSERT(self);
1314 
1315     self->astViInfo[0].stPipeInfo.aPipe[APIPE0] = pipe0Id;
1316     self->astViInfo[0].stPipeInfo.aPipe[APIPE1] = pipe1Id;
1317     self->astViInfo[0].stPipeInfo.aPipe[APIPE2] = pipe2Id;
1318     self->astViInfo[0].stPipeInfo.aPipe[APIPE3] = pipe3Id;
1319 }
1320 
1321 /* Set up the VI channel */
ViCfgSetChn(ViCfg * self,int chnId,PIXEL_FORMAT_E pixFormat,VIDEO_FORMAT_E videoFormat,DYNAMIC_RANGE_E dynamicRange)1322 void ViCfgSetChn(ViCfg* self, int chnId, PIXEL_FORMAT_E pixFormat,
1323     VIDEO_FORMAT_E videoFormat, DYNAMIC_RANGE_E dynamicRange)
1324 {
1325     HI_ASSERT(self);
1326     HI_ASSERT((int)pixFormat < PIXEL_FORMAT_BUTT);
1327     HI_ASSERT((int)videoFormat < VIDEO_FORMAT_BUTT);
1328     HI_ASSERT((int)dynamicRange < DYNAMIC_RANGE_BUTT);
1329 
1330     self->astViInfo[0].stChnInfo.ViChn = chnId;
1331     self->astViInfo[0].stChnInfo.enPixFormat =
1332         (int)pixFormat < 0 ? PIXEL_FORMAT_YVU_SEMIPLANAR_420 : pixFormat;
1333     self->astViInfo[0].stChnInfo.enVideoFormat =
1334         (int)videoFormat < 0 ? VIDEO_FORMAT_LINEAR : videoFormat;
1335     self->astViInfo[0].stChnInfo.enDynamicRange =
1336         (int)dynamicRange < 0 ? DYNAMIC_RANGE_SDR8 : dynamicRange;
1337 }
1338 
1339 /* Initialize VpssCfg */
VpssCfgInit(VpssCfg * self)1340 void VpssCfgInit(VpssCfg* self)
1341 {
1342     HI_ASSERT(self);
1343     if (memset_s(self, sizeof(*self), 0, sizeof(*self)) != EOK) {
1344         HI_ASSERT(0);
1345     }
1346     self->grpId = -1;
1347     self->chnNum = 0;
1348 }
1349 
1350 /* Set up VPSS Group */
VpssCfgSetGrp(VpssCfg * self,int grpId,const VPSS_GRP_ATTR_S * grpAttr,int maxWidth,int maxHeight)1351 void VpssCfgSetGrp(VpssCfg* self,
1352     int grpId, const VPSS_GRP_ATTR_S* grpAttr, int maxWidth, int maxHeight)
1353 {
1354     HI_ASSERT(self);
1355     HI_ASSERT(grpId >= 0);
1356 
1357     self->grpId = grpId;
1358 
1359     if (grpAttr) {
1360         self->grpAttr = *grpAttr;
1361     } else { // Set as default
1362         self->grpAttr.u32MaxW = maxWidth < 0 ? 0 : maxWidth;
1363         self->grpAttr.u32MaxH = maxHeight < 0 ? 0 : maxHeight;
1364         self->grpAttr.enPixelFormat = PIXEL_FORMAT_YVU_SEMIPLANAR_420;
1365         self->grpAttr.enDynamicRange = DYNAMIC_RANGE_SDR8;
1366         self->grpAttr.stFrameRate.s32SrcFrameRate = -1;
1367         self->grpAttr.stFrameRate.s32DstFrameRate = -1;
1368         self->grpAttr.bNrEn = HI_TRUE;
1369     }
1370 }
1371 
1372 /* Add a VPSS channel */
VpssCfgAddChn(VpssCfg * self,int chnId,const VPSS_CHN_ATTR_S * chnAttr,int width,int height)1373 VPSS_CHN_ATTR_S* VpssCfgAddChn(VpssCfg* self,
1374     int chnId, const VPSS_CHN_ATTR_S* chnAttr, int width, int height)
1375 {
1376     HI_ASSERT(self);
1377     HI_ASSERT(self->chnNum < (sizeof(self->chnCfgs) / sizeof((self->chnCfgs)[0])));
1378     static const uint32_t depthDef = 2;
1379     VpssChnCfg *chnCfg = &self->chnCfgs[self->chnNum];
1380 
1381     if (chnAttr) {
1382         chnCfg->attr = *chnAttr;
1383     } else { // Set as default
1384         chnCfg->attr.u32Width = width < 0 ? 0 : width;
1385         chnCfg->attr.u32Height = height < 0 ? 0 : height;
1386         chnCfg->attr.enChnMode = VPSS_CHN_MODE_USER;
1387         chnCfg->attr.enVideoFormat = VIDEO_FORMAT_LINEAR;
1388         chnCfg->attr.enPixelFormat = PIXEL_FORMAT_YVU_SEMIPLANAR_420;
1389         chnCfg->attr.enDynamicRange = DYNAMIC_RANGE_SDR8;
1390         chnCfg->attr.enCompressMode = COMPRESS_MODE_NONE;
1391         chnCfg->attr.stFrameRate.s32SrcFrameRate = -1;
1392         chnCfg->attr.stFrameRate.s32DstFrameRate = -1;
1393         chnCfg->attr.u32Depth = depthDef;
1394         chnCfg->attr.bMirror = HI_FALSE;
1395         chnCfg->attr.bFlip = HI_FALSE;
1396         chnCfg->attr.stAspectRatio.enMode = ASPECT_RATIO_NONE;
1397     }
1398     chnCfg->id = chnId;
1399 
1400     self->chnNum++;
1401     return &chnCfg->attr;
1402 }
1403 
1404 /* Start VPSS according to VpssCfg */
VpssStart(const VpssCfg * cfg)1405 int VpssStart(const VpssCfg* cfg)
1406 {
1407     HI_ASSERT(cfg);
1408     VPSS_GRP grpId = cfg->grpId;
1409     HI_S32 ret;
1410 
1411     ret = HI_MPI_VPSS_CreateGrp(grpId, &cfg->grpAttr);
1412     if (ret != 0) {
1413         SAMPLE_PRT("HI_MPI_VPSS_CreateGrp(%d) FAIL, ret=%#x\n", grpId, ret);
1414         return ret;
1415     }
1416 
1417     for (int i = 0; i < cfg->chnNum; i++) {
1418         ret = HI_MPI_VPSS_SetChnAttr(grpId, cfg->chnCfgs[i].id, &cfg->chnCfgs[i].attr);
1419         if (ret != 0) {
1420             SAMPLE_PRT("HI_MPI_VPSS_SetChnAttr(%d) FAIL, ret=%#x\n", cfg->chnCfgs[i].id, ret);
1421             return ret;
1422         }
1423 
1424         ret = HI_MPI_VPSS_EnableChn(grpId, cfg->chnCfgs[i].id);
1425         if (ret != 0) {
1426             SAMPLE_PRT("HI_MPI_VPSS_EnableChn(%d) FAIL, ret=%#x\n", cfg->chnCfgs[i].id, ret);
1427             return ret;
1428         }
1429     }
1430 
1431     ret = HI_MPI_VPSS_StartGrp(grpId);
1432     if (ret != 0) {
1433         SAMPLE_PRT("HI_MPI_VPSS_StartGrp(%d) FAIL, ret=%#x\n", grpId, ret);
1434         return ret;
1435     }
1436     return HI_SUCCESS;
1437 }
1438 
1439 /* Terminate VIs started with ViCfg */
ViStop(const ViCfg * viCfg)1440 int ViStop(const ViCfg* viCfg)
1441 {
1442     return SAMPLE_COMM_VI_StopVi((ViCfg*)viCfg);
1443 }
1444 
1445 /* Terminate VPSS started with VpssCfg */
VpssStop(const VpssCfg * cfg)1446 int VpssStop(const VpssCfg* cfg)
1447 {
1448     HI_ASSERT(cfg);
1449     VPSS_GRP grpId = cfg->grpId;
1450     HI_S32 ret;
1451 
1452     for (int i = 0; i < cfg->chnNum; i++) {
1453         ret = HI_MPI_VPSS_DisableChn(grpId, cfg->chnCfgs[i].id);
1454         if (ret != 0) {
1455             SAMPLE_PRT("HI_MPI_VPSS_DisableChn(%d, %d) FAIL, ret=%#x\n", grpId, cfg->chnCfgs[i].id, ret);
1456             return ret;
1457         }
1458     }
1459 
1460     ret = HI_MPI_VPSS_StopGrp(grpId);
1461     if (ret != 0) {
1462         SAMPLE_PRT("HI_MPI_VPSS_StopGrp(%d) FAIL, ret=%#x\n", grpId, ret);
1463         return ret;
1464     }
1465 
1466     ret = HI_MPI_VPSS_DestroyGrp(grpId);
1467     if (ret != 0) {
1468         SAMPLE_PRT("HI_MPI_VPSS_DestroyGrp(%d) FAIL, ret=%#x\n", grpId, ret);
1469         return ret;
1470     }
1471     return HI_SUCCESS;
1472 }
1473 
1474 /* Start VI according to ViCfg */
ViStart(const ViCfg * viCfg)1475 int ViStart(const ViCfg* viCfg)
1476 {
1477     static const uint32_t frmRateDef = 30;
1478     SAMPLE_SNS_TYPE_E snsType = viCfg->astViInfo[0].stSnsInfo.enSnsType;
1479     ISP_CTRL_PARAM_S ispCtrlParam;
1480     HI_U32 frmRate;
1481     HI_S32 ret;
1482 
1483     ret = SAMPLE_COMM_VI_SetParam((ViCfg*)viCfg);
1484     if (ret != 0) {
1485         SAMPLE_PRT("SAMPLE_COMM_VI_SetParam FAIL, ret=%#x\n", ret);
1486         return ret;
1487     }
1488 
1489     // Set ISP statistics interval, from histm.sample_enc
1490     SAMPLE_COMM_VI_GetFrameRateBySensor(snsType, &frmRate);
1491     ret = HI_MPI_ISP_GetCtrlParam(viCfg->astViInfo[0].stPipeInfo.aPipe[0], &ispCtrlParam);
1492     if (ret != 0) {
1493         SAMPLE_PRT("HI_MPI_ISP_GetCtrlParam FAIL, ret=%#x\n", ret);
1494         return ret;
1495     }
1496 
1497     ispCtrlParam.u32StatIntvl = frmRate / frmRateDef;
1498     ret = HI_MPI_ISP_SetCtrlParam(viCfg->astViInfo[0].stPipeInfo.aPipe[0], &ispCtrlParam);
1499     if (ret != 0) {
1500         SAMPLE_PRT("HI_MPI_ISP_SetCtrlParam FAIL, ret=%#x\n", ret);
1501         return ret;
1502     }
1503 
1504     ret = SAMPLE_COMM_VI_StartVi((ViCfg*)viCfg);
1505     if (ret != 0) {
1506         SAMPLE_PRT("SAMPLE_COMM_VI_StartVi FAIL, ret=%#x\n", ret);
1507         return ret;
1508     }
1509 
1510     return HI_SUCCESS;
1511 }
1512 
1513 /* bind VI to VPSS */
ViBindVpss(VI_PIPE viPipe,VI_CHN viChn,VPSS_GRP vpssGrp)1514 int ViBindVpss(VI_PIPE viPipe, VI_CHN viChn, VPSS_GRP vpssGrp)
1515 {
1516     MPP_CHN_S srcChn;
1517     MPP_CHN_S dstChn;
1518 
1519     srcChn.enModId = HI_ID_VI;
1520     srcChn.s32DevId = viPipe;
1521     srcChn.s32ChnId = viChn;
1522 
1523     dstChn.enModId = HI_ID_VPSS;
1524     dstChn.s32DevId = vpssGrp;
1525     dstChn.s32ChnId = 0;
1526 
1527     int ret = HI_MPI_SYS_Bind(&srcChn, &dstChn);
1528     if (ret != 0) {
1529         SAMPLE_PRT("HI_MPI_SYS_Bind(VI:%d, VPSS:%d) FAIL, ret=%#x\n", viChn, vpssGrp, ret);
1530         return ret;
1531     }
1532 
1533     return ret;
1534 }
1535 
1536 /* new MppSess */
MppSessNew(void)1537 static MppSess* MppSessNew(void)
1538 {
1539     MppSess *sess = (MppSess*)malloc(sizeof(*sess));
1540     if (sess == NULL) {
1541         SAMPLE_PRT("%s: malloc failed!\n", __FUNCTION__);
1542         HI_ASSERT(sess);
1543     }
1544 
1545     if (memset_s(sess, sizeof(*sess), 0, sizeof(*sess)) != EOK) {
1546         HI_ASSERT(0);
1547     }
1548 
1549     sess->vpssGrp = -1;
1550     sess->vpssChn0 = -1;
1551     sess->vpssChn1 = -1;
1552 
1553     return sess;
1554 }
1555 
1556 /* Create and start {VI->VPSS}MppSess */
ViVpssCreate(MppSess ** sess,const ViCfg * viCfg,const VpssCfg * vpssCfg)1557 int ViVpssCreate(MppSess** sess, const ViCfg* viCfg, const VpssCfg* vpssCfg)
1558 {
1559     HI_ASSERT(sess && viCfg && vpssCfg);
1560     *sess = NULL;
1561     int res;
1562     int ret;
1563 
1564     ret = ViStart(viCfg);
1565     SAMPLE_CHECK_EXPR_GOTO(ret != HI_SUCCESS, FAIL1,
1566         "vi start fail, err(%#x)\n", ret);
1567 
1568     ret = VpssStart(vpssCfg);
1569     SAMPLE_CHECK_EXPR_GOTO(ret != HI_SUCCESS, FAIL2,
1570         "vpss start fail, err(%#x)\n", ret);
1571 
1572     VI_PIPE pipeId = viCfg->astViInfo[0].stPipeInfo.aPipe[0];
1573     VI_CHN chnId = viCfg->astViInfo[0].stChnInfo.ViChn;
1574     ret = ViBindVpss(pipeId, chnId, vpssCfg->grpId);
1575     SAMPLE_CHECK_EXPR_GOTO(ret != HI_SUCCESS, FAIL3,
1576         "vi bind vpss fail, err(%#x)\n", ret);
1577 
1578     MppSess *self = MppSessNew(); // todo:realease malloc
1579     *sess = self;
1580     self->viCfg = *viCfg;
1581     self->vpssCfg = *vpssCfg;
1582     self->used |= MPP_VI;
1583     self->used |= MPP_VPSS;
1584     self->vpssGrp = vpssCfg->grpId;
1585     self->vpssChn0 = vpssCfg->chnCfgs[0].id;
1586     self->vpssChn1 = vpssCfg->chnNum > 1 ? vpssCfg->chnCfgs[1].id : -1;
1587     return 0;
1588 
1589     FAIL3:
1590         res = VpssStop(vpssCfg);
1591         SAMPLE_PRT("ViVpssCreate\n");
1592         if (res != 0) {
1593             SAMPLE_PRT("VpssStop FAIL, ret=%#x\n", res);
1594         }
1595     FAIL2:
1596         ViStop(viCfg);
1597     FAIL1:
1598         return ret;
1599 }
1600 
CnnTrashClassifyAiProcess(VIDEO_FRAME_INFO_S frm)1601 static HI_VOID CnnTrashClassifyAiProcess(VIDEO_FRAME_INFO_S frm)
1602 {
1603     int ret;
1604     if (GetCfgBool("trash_classify_switch:support_trash_classify", true)) {
1605         if (g_workPlug.model == 0) {
1606             HI_ASSERT(!g_aicMediaInfo.osds);
1607             g_aicMediaInfo.osds = OsdsCreate(HI_OSD_BINDMOD_VPSS, g_aicMediaInfo.vpssGrp, g_aicMediaInfo.vpssChn0);
1608             HI_ASSERT(g_aicMediaInfo.osds);
1609             ret = CnnTrashClassifyLoadModel(&g_workPlug.model, g_aicMediaInfo.osds);
1610             if (ret < 0) {
1611                 g_workPlug.model = 0;
1612                 SAMPLE_CHECK_EXPR_GOTO(ret < 0, TRASH_RELEASE,
1613                     "load cnn trash classify model err(%#x)\n", ret);
1614             }
1615         }
1616         VIDEO_FRAME_INFO_S *resFrm = NULL;
1617         ret = CnnTrashClassifyCal(g_workPlug.model, &frm, resFrm);
1618         SAMPLE_CHECK_EXPR_GOTO(ret < 0, TRASH_RELEASE,
1619             "trash classify plug cal FAIL, ret=%#x\n", ret);
1620     }
1621 
1622     TRASH_RELEASE:
1623         ret = HI_MPI_VPSS_ReleaseChnFrame(g_aicMediaInfo.vpssGrp, g_aicMediaInfo.vpssChn0, &frm);
1624         if (ret != HI_SUCCESS) {
1625             SAMPLE_PRT("Error(%#x),HI_MPI_VPSS_ReleaseChnFrame failed,Grp(%d) chn(%d)!\n",
1626                 ret, g_aicMediaInfo.vpssGrp, g_aicMediaInfo.vpssChn0);
1627         }
1628 }
1629 
GetVpssChnFrameCnnTrashClassify(HI_VOID * arg)1630 static HI_VOID* GetVpssChnFrameCnnTrashClassify(HI_VOID* arg)
1631 {
1632     int ret;
1633     VIDEO_FRAME_INFO_S frm;
1634     HI_S32 s32MilliSec = 20000;
1635 
1636     while (HI_FALSE == g_bAiProcessStopSignal) {
1637         ret = HI_MPI_VPSS_GetChnFrame(g_aicMediaInfo.vpssGrp, g_aicMediaInfo.vpssChn0, &frm, s32MilliSec);
1638         if (ret != 0) {
1639             SAMPLE_PRT("HI_MPI_VPSS_GetChnFrame FAIL, err=%#x, grp=%d, chn=%d\n",
1640                 ret, g_aicMediaInfo.vpssGrp, g_aicMediaInfo.vpssChn0);
1641             ret = HI_MPI_VPSS_ReleaseChnFrame(g_aicMediaInfo.vpssGrp, g_aicMediaInfo.vpssChn0, &frm);
1642             if (ret != HI_SUCCESS) {
1643                 SAMPLE_PRT("Error(%#x),HI_MPI_VPSS_ReleaseChnFrame failed,Grp(%d) chn(%d)!\n",
1644                     ret, g_aicMediaInfo.vpssGrp, g_aicMediaInfo.vpssChn0);
1645             }
1646             continue;
1647         }
1648         SAMPLE_PRT("get vpss frame success, weight:%d, height:%d\n", frm.stVFrame.u32Width, frm.stVFrame.u32Height);
1649 
1650         if (g_num == 0) {
1651             ConfBaseInit(AI_SAMPLE_CFG_FILE);
1652             g_num++;
1653         }
1654         CnnTrashClassifyAiProcess(frm);
1655     }
1656 
1657     return HI_NULL;
1658 }
1659 
HandClassifyAiProcess(VIDEO_FRAME_INFO_S frm,VO_LAYER voLayer,VO_CHN voChn)1660 static HI_VOID HandClassifyAiProcess(VIDEO_FRAME_INFO_S frm, VO_LAYER voLayer, VO_CHN voChn)
1661 {
1662     int ret;
1663     if (GetCfgBool("hand_classify_switch:support_hand_classify", true)) {
1664         if (g_workPlug.model == 0) {
1665             ret = Yolo2HandDetectResnetClassifyLoad(&g_workPlug.model);
1666             if (ret < 0) {
1667                 g_workPlug.model = 0;
1668                 SAMPLE_CHECK_EXPR_GOTO(ret < 0, HAND_RELEASE,
1669                     "load hand classify model err, ret=%#x\n", ret);
1670             }
1671         }
1672 
1673         VIDEO_FRAME_INFO_S resizeFrm;
1674         ret = MppFrmResize(&frm, &resizeFrm, HAND_FRM_WIDTH, HAND_FRM_HEIGHT);
1675         ret = Yolo2HandDetectResnetClassifyCal(g_workPlug.model, &resizeFrm, &frm);
1676         SAMPLE_CHECK_EXPR_GOTO(ret < 0, HAND_RELEASE,
1677             "hand classify plug cal FAIL, ret=%#x\n", ret);
1678 
1679         ret = HI_MPI_VO_SendFrame(voLayer, voChn, &frm, 0);
1680         SAMPLE_CHECK_EXPR_GOTO(ret != HI_SUCCESS, HAND_RELEASE,
1681             "HI_MPI_VO_SendFrame fail, Error(%#x)\n", ret);
1682 
1683         MppFrmDestroy(&resizeFrm);
1684     }
1685 
1686     HAND_RELEASE:
1687         ret = HI_MPI_VPSS_ReleaseChnFrame(g_aicMediaInfo.vpssGrp, g_aicMediaInfo.vpssChn0, &frm);
1688         if (ret != HI_SUCCESS) {
1689             SAMPLE_PRT("Error(%#x),HI_MPI_VPSS_ReleaseChnFrame failed,Grp(%d) chn(%d)!\n",
1690                 ret, g_aicMediaInfo.vpssGrp, g_aicMediaInfo.vpssChn0);
1691         }
1692 }
1693 
GetVpssChnFrameHandClassify(HI_VOID * arg)1694 static HI_VOID* GetVpssChnFrameHandClassify(HI_VOID* arg)
1695 {
1696     int ret;
1697     VIDEO_FRAME_INFO_S frm;
1698     HI_S32 s32MilliSec = 2000;
1699     VO_LAYER voLayer = 0;
1700     VO_CHN voChn = 0;
1701 
1702     SAMPLE_PRT("vpssGrp:%d, vpssChn0:%d\n", g_aicMediaInfo.vpssGrp, g_aicMediaInfo.vpssChn0);
1703 
1704     while (HI_FALSE == g_bAiProcessStopSignal) {
1705         ret = HI_MPI_VPSS_GetChnFrame(g_aicMediaInfo.vpssGrp, g_aicMediaInfo.vpssChn0, &frm, s32MilliSec);
1706         if (ret != 0) {
1707             SAMPLE_PRT("HI_MPI_VPSS_GetChnFrame FAIL, err=%#x, grp=%d, chn=%d\n",
1708                 ret, g_aicMediaInfo.vpssGrp, g_aicMediaInfo.vpssChn0);
1709             ret = HI_MPI_VPSS_ReleaseChnFrame(g_aicMediaInfo.vpssGrp, g_aicMediaInfo.vpssChn0, &frm);
1710             if (ret != HI_SUCCESS) {
1711                 SAMPLE_PRT("Error(%#x),HI_MPI_VPSS_ReleaseChnFrame failed,Grp(%d) chn(%d)!\n",
1712                     ret, g_aicMediaInfo.vpssGrp, g_aicMediaInfo.vpssChn0);
1713             }
1714             continue;
1715         }
1716         SAMPLE_PRT("get vpss frame success, weight:%d, height:%d\n", frm.stVFrame.u32Width, frm.stVFrame.u32Height);
1717 
1718         if (g_num == 0) {
1719             ConfBaseInit(AI_SAMPLE_CFG_FILE);
1720             g_num++;
1721         }
1722         HandClassifyAiProcess(frm, voLayer, voChn);
1723     }
1724 
1725     return HI_NULL;
1726 }
1727 
Pause(HI_VOID)1728 static HI_VOID Pause(HI_VOID)
1729 {
1730     printf("---------------press Enter key to exit!---------------\n");
1731     (void)getchar();
1732 }
1733 
ViPramCfg(HI_VOID)1734 HI_VOID ViPramCfg(HI_VOID)
1735 {
1736     ViCfgInit(&g_aicMediaInfo.viCfg);
1737     ViCfgSetDev(&g_aicMediaInfo.viCfg, 0, -1);
1738     ViCfgSetPipe(&g_aicMediaInfo.viCfg, 0, -1, -1, -1);
1739     g_aicMediaInfo.viCfg.astViInfo[0].stPipeInfo.enMastPipeMode = 0;
1740     ViCfgSetChn(&g_aicMediaInfo.viCfg, 0, -1, -1, -1);
1741     g_aicMediaInfo.viCfg.astViInfo[0].stChnInfo.enCompressMode = 1;
1742 }
1743 
StVbParamCfg(VbCfg * self)1744 static HI_VOID StVbParamCfg(VbCfg *self)
1745 {
1746     memset_s(&g_aicMediaInfo.vbCfg, sizeof(VB_CONFIG_S), 0, sizeof(VB_CONFIG_S));
1747     // 2: The number of buffer pools that can be accommodated in the entire system
1748     self->u32MaxPoolCnt              = 2;
1749 
1750     /* get picture buffer size */
1751     g_aicMediaInfo.u32BlkSize = COMMON_GetPicBufferSize(g_aicMediaInfo.stSize.u32Width, g_aicMediaInfo.stSize.u32Height,
1752         SAMPLE_PIXEL_FORMAT, DATA_BITWIDTH_8, COMPRESS_MODE_SEG, DEFAULT_ALIGN);
1753     self->astCommPool[0].u64BlkSize  = g_aicMediaInfo.u32BlkSize;
1754     // 10: Number of cache blocks per cache pool. Value range: (0, 10240]
1755     self->astCommPool[0].u32BlkCnt   = 10;
1756 
1757     /* get raw buffer size */
1758     g_aicMediaInfo.u32BlkSize = VI_GetRawBufferSize(g_aicMediaInfo.stSize.u32Width, g_aicMediaInfo.stSize.u32Height,
1759         PIXEL_FORMAT_RGB_BAYER_16BPP, COMPRESS_MODE_NONE, DEFAULT_ALIGN);
1760     self->astCommPool[1].u64BlkSize  = g_aicMediaInfo.u32BlkSize;
1761     // 4: Number of cache blocks per cache pool. Value range: (0, 10240]
1762     self->astCommPool[1].u32BlkCnt   = 4;
1763 }
1764 
StVoParamCfg(VoCfg * self)1765 static HI_VOID StVoParamCfg(VoCfg *self)
1766 {
1767     SAMPLE_COMM_VO_GetDefConfig(self);
1768     self->enDstDynamicRange = DYNAMIC_RANGE_SDR8;
1769 
1770     self->enVoIntfType = VO_INTF_MIPI; /* set VO int type */
1771     self->enIntfSync = VO_OUTPUT_USER; /* set VO output information */
1772 
1773     self->enPicSize = g_aicMediaInfo.enPicSize;
1774 }
1775 
VpssParamCfg(HI_VOID)1776 static HI_VOID VpssParamCfg(HI_VOID)
1777 {
1778     VpssCfgInit(&g_aicMediaInfo.vpssCfg);
1779     VpssCfgSetGrp(&g_aicMediaInfo.vpssCfg, AIC_VPSS_GRP, NULL,
1780         g_aicMediaInfo.stSize.u32Width, g_aicMediaInfo.stSize.u32Width);
1781     g_aicMediaInfo.vpssCfg.grpAttr.enPixelFormat = PIXEL_FORMAT_YVU_SEMIPLANAR_420;
1782     VpssCfgAddChn(&g_aicMediaInfo.vpssCfg, AIC_VPSS_ZOUT_CHN, NULL, AICSTART_VI_OUTWIDTH, AICSTART_VI_OUTHEIGHT);
1783     HI_ASSERT(!g_aicMediaInfo.viSess);
1784 }
1785 
CnnTrashAiThreadProcess(HI_VOID)1786 static HI_S32 CnnTrashAiThreadProcess(HI_VOID)
1787 {
1788     HI_S32 s32Ret;
1789     if (snprintf_s(acThreadName, BUFFER_SIZE, BUFFER_SIZE - 1, "AIProcess") < 0) {
1790         HI_ASSERT(0);
1791     }
1792     prctl(PR_SET_NAME, (unsigned long)acThreadName, 0, 0, 0);
1793     s32Ret = pthread_create(&g_aiProcessThread, NULL, GetVpssChnFrameCnnTrashClassify, NULL);
1794 
1795     return s32Ret;
1796 }
1797 
HandClassifyAiThreadProcess(HI_VOID)1798 static HI_S32 HandClassifyAiThreadProcess(HI_VOID)
1799 {
1800     HI_S32 s32Ret;
1801     if (snprintf_s(acThreadName, BUFFER_SIZE, BUFFER_SIZE - 1, "AIProcess") < 0) {
1802         HI_ASSERT(0);
1803     }
1804     prctl(PR_SET_NAME, (unsigned long)acThreadName, 0, 0, 0);
1805     s32Ret = pthread_create(&g_aiProcessThread, NULL, GetVpssChnFrameHandClassify, NULL);
1806 
1807     return s32Ret;
1808 }
1809 
PauseDoUnloadCnnModel(HI_VOID)1810 static HI_S32 PauseDoUnloadCnnModel(HI_VOID)
1811 {
1812     HI_S32 s32Ret = HI_SUCCESS;
1813 
1814     if (GetCfgBool("trash_classify_switch:support_trash_classify", true)) {
1815         if (memset_s(&g_workPlug, sizeof(g_workPlug), 0x00, sizeof(g_workPlug)) != EOK) {
1816             HI_ASSERT(0);
1817         }
1818         /* when pause operation, should unload model */
1819         s32Ret = CnnTrashClassifyUnloadModel(g_workPlug.model);
1820         SAMPLE_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, "unload cnn trash classify model err:%x\n", s32Ret);
1821         ConfBaseExt();
1822         g_num = 0;
1823         OsdsClear(g_aicMediaInfo.osds);
1824         OsdsDestroy(g_aicMediaInfo.osds);
1825     }
1826 
1827     return s32Ret;
1828 }
1829 
PauseDoUnloadHandClassifyModel(HI_VOID)1830 static HI_S32 PauseDoUnloadHandClassifyModel(HI_VOID)
1831 {
1832     HI_S32 s32Ret = HI_SUCCESS;
1833 
1834     if (GetCfgBool("hand_classify_switch:support_hand_classify", true)) {
1835         if (memset_s(&g_workPlug, sizeof(g_workPlug), 0x00, sizeof(g_workPlug)) != EOK) {
1836             HI_ASSERT(0);
1837         }
1838         /* when pause operation, should unload model */
1839         s32Ret = Yolo2HandDetectResnetClassifyUnload(g_workPlug.model);
1840         SAMPLE_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, "unload hand model err:%x\n", s32Ret);
1841         ConfBaseExt();
1842         g_num = 0;
1843     }
1844 
1845     return s32Ret;
1846 }
1847 
1848 /*
1849  * Display the data collected by sensor to LCD screen
1850  * VI->VPSS->VO->MIPI
1851  */
SAMPLE_MEDIA_CNN_TRASH_CLASSIFY(HI_VOID)1852 HI_S32 SAMPLE_MEDIA_CNN_TRASH_CLASSIFY(HI_VOID)
1853 {
1854     HI_S32             s32Ret;
1855     HI_S32             fd = 0;
1856 
1857     /* config vi */
1858     ViPramCfg();
1859 
1860     /* get picture size */
1861     s32Ret = SAMPLE_COMM_VI_GetSizeBySensor(g_aicMediaInfo.viCfg.astViInfo[0].stSnsInfo.enSnsType,
1862         &g_aicMediaInfo.enPicSize);
1863     SAMPLE_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, "get pic size by sensor fail, s32Ret=%#x\n", s32Ret);
1864 
1865     /* get picture size(w*h), according enPicSize */
1866     s32Ret = SAMPLE_COMM_SYS_GetPicSize(g_aicMediaInfo.enPicSize, &g_aicMediaInfo.stSize);
1867     SAMPLE_PRT("AIC: snsMaxSize=%ux%u\n", g_aicMediaInfo.stSize.u32Width, g_aicMediaInfo.stSize.u32Height);
1868     SAMPLE_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, "get picture size failed, s32Ret=%#x\n", s32Ret);
1869 
1870     /* config vb */
1871     StVbParamCfg(&g_aicMediaInfo.vbCfg);
1872 
1873     /* vb init & MPI system init */
1874     s32Ret = SAMPLE_COMM_SYS_Init(&g_aicMediaInfo.vbCfg);
1875     SAMPLE_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, "system init failed, s32Ret=%#x\n", s32Ret);
1876 
1877     /* set VO config to mipi, get mipi device */
1878     s32Ret = SAMPLE_VO_CONFIG_MIPI(&fd);
1879     SAMPLE_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, EXIT, "CONFIG MIPI FAIL.s32Ret:0x%x\n", s32Ret);
1880 
1881     /* config vpss */
1882     VpssParamCfg();
1883     s32Ret = ViVpssCreate(&g_aicMediaInfo.viSess, &g_aicMediaInfo.viCfg, &g_aicMediaInfo.vpssCfg);
1884     SAMPLE_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, EXIT1, "ViVpss Sess create FAIL, ret=%#x\n", s32Ret);
1885     g_aicMediaInfo.vpssGrp = AIC_VPSS_GRP;
1886     g_aicMediaInfo.vpssChn0 = AIC_VPSS_ZOUT_CHN;
1887 
1888     /* config vo */
1889     StVoParamCfg(&g_aicMediaInfo.voCfg);
1890 
1891     /* start vo */
1892     s32Ret = SampleCommVoStartMipi(&g_aicMediaInfo.voCfg);
1893     SAMPLE_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, EXIT1, "start vo FAIL. s32Ret: 0x%x\n", s32Ret);
1894 
1895     /* vpss bind vo */
1896     s32Ret = SAMPLE_COMM_VPSS_Bind_VO(g_aicMediaInfo.vpssGrp, g_aicMediaInfo.vpssChn0, g_aicMediaInfo.voCfg.VoDev, 0);
1897     SAMPLE_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, EXIT2, "vo bind vpss FAIL. s32Ret: 0x%x\n", s32Ret);
1898     SAMPLE_PRT("vpssGrp:%d, vpssChn:%d\n", g_aicMediaInfo.vpssGrp, g_aicMediaInfo.vpssChn0);
1899 
1900     /* create work thread to run ai */
1901     s32Ret = CnnTrashAiThreadProcess();
1902     SAMPLE_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, "ai proccess thread creat fail:%s\n", strerror(s32Ret));
1903     Pause();
1904     g_bAiProcessStopSignal = HI_TRUE;
1905     // Waiting for the end of a thread, the operation of synchronization between threads
1906     pthread_join(g_aiProcessThread, NULL);
1907     g_aiProcessThread = 0;
1908     PauseDoUnloadCnnModel();
1909 
1910     SAMPLE_COMM_VPSS_UnBind_VO(g_aicMediaInfo.vpssGrp, g_aicMediaInfo.vpssChn0, g_aicMediaInfo.voCfg.VoDev, 0);
1911     SAMPLE_VO_DISABLE_MIPITx(fd);
1912     SampleCloseMipiTxFd(fd);
1913     system("echo 0 > /sys/class/gpio/gpio55/value");
1914 
1915 EXIT2:
1916     SAMPLE_COMM_VO_StopVO(&g_aicMediaInfo.voCfg);
1917 EXIT1:
1918     VpssStop(&g_aicMediaInfo.vpssCfg);
1919     SAMPLE_COMM_VI_UnBind_VPSS(g_aicMediaInfo.viCfg.astViInfo[0].stPipeInfo.aPipe[0],
1920         g_aicMediaInfo.viCfg.astViInfo[0].stChnInfo.ViChn, g_aicMediaInfo.vpssGrp);
1921     ViStop(&g_aicMediaInfo.viCfg);
1922     free(g_aicMediaInfo.viSess);
1923 EXIT:
1924     SAMPLE_COMM_SYS_Exit();
1925     return s32Ret;
1926 }
1927 
1928 /*
1929  * Display the data collected by sensor to LCD screen
1930  * VI->VPSS->VO->MIPI
1931  */
SAMPLE_MEDIA_HAND_CLASSIFY(HI_VOID)1932 HI_S32 SAMPLE_MEDIA_HAND_CLASSIFY(HI_VOID)
1933 {
1934     HI_S32             s32Ret;
1935     HI_S32             fd = 0;
1936 
1937     /* config vi */
1938     ViPramCfg();
1939 
1940     /* get picture size */
1941     s32Ret = SAMPLE_COMM_VI_GetSizeBySensor(g_aicMediaInfo.viCfg.astViInfo[0].stSnsInfo.enSnsType,
1942         &g_aicMediaInfo.enPicSize);
1943     SAMPLE_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, "get pic size by sensor fail, s32Ret=%#x\n", s32Ret);
1944 
1945     /* get picture size(w*h), according enPicSize */
1946     s32Ret = SAMPLE_COMM_SYS_GetPicSize(g_aicMediaInfo.enPicSize, &g_aicMediaInfo.stSize);
1947     SAMPLE_PRT("AIC: snsMaxSize=%ux%u\n", g_aicMediaInfo.stSize.u32Width, g_aicMediaInfo.stSize.u32Height);
1948     SAMPLE_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, "get picture size failed, s32Ret=%#x\n", s32Ret);
1949 
1950     /* config vb */
1951     StVbParamCfg(&g_aicMediaInfo.vbCfg);
1952 
1953     /* vb init & MPI system init */
1954     s32Ret = SAMPLE_COMM_SYS_Init(&g_aicMediaInfo.vbCfg);
1955     SAMPLE_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, "system init failed, s32Ret=%#x\n", s32Ret);
1956 
1957     /* set VO config to mipi, get mipi device */
1958     s32Ret = SAMPLE_VO_CONFIG_MIPI(&fd);
1959     SAMPLE_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, EXIT, "CONFIG MIPI FAIL.s32Ret:0x%x\n", s32Ret);
1960 
1961     /* config vpss */
1962     VpssParamCfg();
1963     s32Ret = ViVpssCreate(&g_aicMediaInfo.viSess, &g_aicMediaInfo.viCfg, &g_aicMediaInfo.vpssCfg);
1964     SAMPLE_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, EXIT1, "ViVpss Sess create FAIL, ret=%#x\n", s32Ret);
1965     g_aicMediaInfo.vpssGrp = AIC_VPSS_GRP;
1966     g_aicMediaInfo.vpssChn0 = AIC_VPSS_ZOUT_CHN;
1967 
1968     /* config vo */
1969     StVoParamCfg(&g_aicMediaInfo.voCfg);
1970 
1971     /* start vo */
1972     s32Ret = SampleCommVoStartMipi(&g_aicMediaInfo.voCfg);
1973     SAMPLE_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, EXIT1, "start vo FAIL. s32Ret: 0x%x\n", s32Ret);
1974 
1975     /* vpss bind vo */
1976     s32Ret = SAMPLE_COMM_VPSS_Bind_VO(g_aicMediaInfo.vpssGrp, g_aicMediaInfo.vpssChn0, g_aicMediaInfo.voCfg.VoDev, 0);
1977     SAMPLE_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, EXIT2, "vo bind vpss FAIL. s32Ret: 0x%x\n", s32Ret);
1978     SAMPLE_PRT("vpssGrp:%d, vpssChn:%d\n", g_aicMediaInfo.vpssGrp, g_aicMediaInfo.vpssChn0);
1979 
1980     /* create work thread to run ai */
1981     s32Ret = HandClassifyAiThreadProcess();
1982     SAMPLE_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, "ai proccess thread creat fail:%s\n", strerror(s32Ret));
1983 
1984     Pause();
1985     g_bAiProcessStopSignal = HI_TRUE;
1986     // Waiting for the end of a thread, the operation of synchronization between threads
1987     pthread_join(g_aiProcessThread, NULL);
1988     g_aiProcessThread = 0;
1989     PauseDoUnloadHandClassifyModel();
1990 
1991     SAMPLE_COMM_VPSS_UnBind_VO(g_aicMediaInfo.vpssGrp, g_aicMediaInfo.vpssChn0, g_aicMediaInfo.voCfg.VoDev, 0);
1992     SAMPLE_VO_DISABLE_MIPITx(fd);
1993     SampleCloseMipiTxFd(fd);
1994     system("echo 0 > /sys/class/gpio/gpio55/value");
1995 
1996 EXIT2:
1997     SAMPLE_COMM_VO_StopVO(&g_aicMediaInfo.voCfg);
1998 EXIT1:
1999     VpssStop(&g_aicMediaInfo.vpssCfg);
2000     SAMPLE_COMM_VI_UnBind_VPSS(g_aicMediaInfo.viCfg.astViInfo[0].stPipeInfo.aPipe[0],
2001         g_aicMediaInfo.viCfg.astViInfo[0].stChnInfo.ViChn, g_aicMediaInfo.vpssGrp);
2002     ViStop(&g_aicMediaInfo.viCfg);
2003     free(g_aicMediaInfo.viSess);
2004 EXIT:
2005     SAMPLE_COMM_SYS_Exit();
2006     return s32Ret;
2007 }
2008 
2009 #ifdef __cplusplus
2010 #if __cplusplus
2011 }
2012 #endif
2013 #endif /* End of #ifdef __cplusplus */
2014