xref: /OK3568_Linux_fs/external/rockit/mpi/example/common/test_comm_vpss.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright 2021 Rockchip Electronics Co. LTD
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifdef __cplusplus
17 #if __cplusplus
18 extern "C" {
19 #endif
20 #endif /* End of #ifdef __cplusplus */
21 
22 #include <pthread.h>
23 #include <string.h>
24 #include <unistd.h>
25 
26 #include "test_comm_vpss.h"
27 #include "test_comm_imgproc.h"
28 
29 #include "rk_mpi_vpss.h"
30 #include "rk_mpi_cal.h"
31 #include "rk_mpi_mb.h"
32 #include "rk_mpi_mmz.h"
33 #include "rk_mpi_sys.h"
34 #include "rk_comm_vpss.h"
35 #include "rk_common.h"
36 #include "rk_debug.h"
37 
38 typedef struct test_vpss_proc_s {
39      RK_BOOL bThreadStart;
40      FILE *srcFp;
41      FILE *dstSaveFp;
42      VPSS_GRP VpssGrp;
43      RK_U32 u32VpssChnNum;
44      RK_U32 u32SendFrameRate;
45      pthread_t VpssProcPid;
46      TEST_VPSS_PROC_CTX_S stProcCtx;
47 } TEST_VPSS_PROC_S;
48 
49 static TEST_VPSS_PROC_S gProcThread[VPSS_MAX_GRP_NUM];
50 
TEST_VPSS_Start(VPSS_GRP VpssGrp,RK_U32 u32ChnNum,VPSS_GRP_ATTR_S * pstVpssGrpAttr,VPSS_CHN_ATTR_S * pstVpssChnAttr)51 RK_S32 TEST_VPSS_Start(
52         VPSS_GRP VpssGrp,
53         RK_U32 u32ChnNum,
54         VPSS_GRP_ATTR_S *pstVpssGrpAttr,
55         VPSS_CHN_ATTR_S *pstVpssChnAttr) {
56     VPSS_CHN VpssChn = 0;
57     RK_S32 s32Ret = RK_SUCCESS;
58 
59     s32Ret = RK_MPI_VPSS_CreateGrp(VpssGrp, pstVpssGrpAttr);
60     if (s32Ret != RK_SUCCESS) {
61         RK_LOGE("RK_MPI_VPSS_CreateGrp(grp:%d) failed with %#x!", VpssGrp, s32Ret);
62         return s32Ret;
63     }
64 
65     for (VpssChn = 0; VpssChn < u32ChnNum; VpssChn++) {
66         s32Ret = RK_MPI_VPSS_SetChnAttr(VpssGrp, VpssChn, &pstVpssChnAttr[VpssChn]);
67         if (s32Ret != RK_SUCCESS) {
68             RK_LOGE("RK_MPI_VPSS_SetChnAttr failed with %#x", s32Ret);
69             return s32Ret;
70         }
71 
72         s32Ret = RK_MPI_VPSS_EnableChn(VpssGrp, VpssChn);
73         if (s32Ret != RK_SUCCESS) {
74             RK_LOGE("RK_MPI_VPSS_EnableChn failed with %#x", s32Ret);
75             return s32Ret;
76         }
77     }
78 
79     s32Ret = RK_MPI_VPSS_StartGrp(VpssGrp);
80     if (s32Ret != RK_SUCCESS) {
81         RK_LOGE("RK_MPI_VPSS_StartGrp failed with %#x", s32Ret);
82         return s32Ret;
83     }
84     s32Ret = RK_MPI_VPSS_ResetGrp(VpssGrp);
85     if (s32Ret != RK_SUCCESS) {
86         RK_LOGE("RK_MPI_VPSS_ResetGrp failed with %#x", s32Ret);
87         return s32Ret;
88     }
89 
90     return s32Ret;
91 }
92 
TEST_VPSS_Stop(VPSS_GRP VpssGrp,RK_U32 u32ChnNum)93 RK_S32 TEST_VPSS_Stop(VPSS_GRP VpssGrp, RK_U32 u32ChnNum) {
94     RK_S32 s32Ret = RK_SUCCESS;
95     VPSS_CHN VpssChn = 0;
96 
97     for (VpssChn = 0; VpssChn < u32ChnNum; VpssChn++) {
98         s32Ret = RK_MPI_VPSS_DisableChn(VpssGrp, VpssChn);
99         if (s32Ret != RK_SUCCESS) {
100             RK_LOGE("RK_MPI_VPSS_DisableChn failed with %#x!", s32Ret);
101             return s32Ret;
102         }
103     }
104 
105     s32Ret = RK_MPI_VPSS_StopGrp(VpssGrp);
106     if (s32Ret != RK_SUCCESS) {
107         RK_LOGE("RK_MPI_VPSS_StopGrp failed with %#x!", s32Ret);
108         return s32Ret;
109     }
110 
111     s32Ret = RK_MPI_VPSS_DestroyGrp(VpssGrp);
112     if (s32Ret != RK_SUCCESS) {
113         RK_LOGE("RK_MPI_VPSS_DestroyGrp failed with %#x!", s32Ret);
114         return s32Ret;
115     }
116 
117     return RK_SUCCESS;
118 }
119 
TEST_VPSS_GrpSetZoom(VPSS_GRP VpssGrp,RK_U32 u32Zoom,RK_BOOL bEnable)120 RK_S32 TEST_VPSS_GrpSetZoom(VPSS_GRP VpssGrp, RK_U32 u32Zoom, RK_BOOL bEnable) {
121     RK_S32 s32Ret = RK_SUCCESS;
122     VPSS_CROP_INFO_S stCropInfo;
123 
124     s32Ret = RK_MPI_VPSS_GetGrpCrop(VpssGrp, &stCropInfo);
125     if (s32Ret != RK_SUCCESS) {
126         RK_LOGE("RK_MPI_VPSS_GetGrpCrop failed with %#x!", s32Ret);
127         return s32Ret;
128     }
129 
130     stCropInfo.bEnable = bEnable;
131     stCropInfo.enCropCoordinate = VPSS_CROP_RATIO_COOR;
132     stCropInfo.stCropRect.s32X = 500 - u32Zoom / 2;
133     stCropInfo.stCropRect.s32Y = 500 - u32Zoom / 2;
134     stCropInfo.stCropRect.u32Width = u32Zoom;
135     stCropInfo.stCropRect.u32Height = u32Zoom;
136     s32Ret = RK_MPI_VPSS_SetGrpCrop(VpssGrp, &stCropInfo);
137     if (s32Ret != RK_SUCCESS) {
138         RK_LOGE("RK_MPI_VPSS_SetGrpCrop failed with %#x!", s32Ret);
139         return s32Ret;
140     }
141 
142     return s32Ret;
143 }
144 
TEST_VPSS_ChnSetZoom(VPSS_GRP VpssGrp,VPSS_CHN VpssChn,RK_U32 u32Zoom,RK_BOOL bEnable)145 RK_S32 TEST_VPSS_ChnSetZoom(VPSS_GRP VpssGrp, VPSS_CHN VpssChn, RK_U32 u32Zoom, RK_BOOL bEnable) {
146     RK_S32 s32Ret = RK_SUCCESS;
147     VPSS_CROP_INFO_S stCropInfo;
148 
149     s32Ret = RK_MPI_VPSS_GetChnCrop(VpssGrp, VpssChn, &stCropInfo);
150     if (s32Ret != RK_SUCCESS) {
151         RK_LOGE("RK_MPI_VPSS_GetChnCrop failed with %#x!", s32Ret);
152         return s32Ret;
153     }
154 
155     stCropInfo.bEnable = bEnable;
156     stCropInfo.enCropCoordinate = VPSS_CROP_RATIO_COOR;
157     stCropInfo.stCropRect.s32X = 500 - u32Zoom / 2;
158     stCropInfo.stCropRect.s32Y = 500 - u32Zoom / 2;
159     stCropInfo.stCropRect.u32Width = u32Zoom;
160     stCropInfo.stCropRect.u32Height = u32Zoom;
161     s32Ret = RK_MPI_VPSS_SetChnCrop(VpssGrp, VpssChn, &stCropInfo);
162     if (s32Ret != RK_SUCCESS) {
163         RK_LOGE("RK_MPI_VPSS_SetChnCrop failed with %#x!", s32Ret);
164         return s32Ret;
165     }
166 
167     return s32Ret;
168 }
169 
TEST_VPSS_SetChnRotation(VPSS_GRP VpssGrp,VPSS_CHN VpssChn,ROTATION_E enRotation)170 RK_S32 TEST_VPSS_SetChnRotation(VPSS_GRP VpssGrp, VPSS_CHN VpssChn, ROTATION_E enRotation) {
171     RK_S32 s32Ret = RK_SUCCESS;
172     ROTATION_E rotation = ROTATION_0;
173 
174     s32Ret = RK_MPI_VPSS_GetChnRotation(VpssGrp, VpssChn, &rotation);
175     if (s32Ret != RK_SUCCESS) {
176         RK_LOGE("RK_MPI_VPSS_GetChnRotation failed with %#x!", s32Ret);
177         return s32Ret;
178     }
179     s32Ret = RK_MPI_VPSS_SetChnRotation(VpssGrp, VpssChn, enRotation);
180     if (s32Ret != RK_SUCCESS) {
181         RK_LOGE("RK_MPI_VPSS_SetChnRotation failed with %#x!", s32Ret);
182         return s32Ret;
183     }
184 
185     return s32Ret;
186 }
187 
TEST_VPSS_SetChnRotationEx(VPSS_GRP VpssGrp,VPSS_CHN VpssChn,RK_U32 u32Angle)188 RK_S32 TEST_VPSS_SetChnRotationEx(VPSS_GRP VpssGrp, VPSS_CHN VpssChn, RK_U32 u32Angle) {
189     RK_S32 s32Ret = RK_SUCCESS;
190     VPSS_ROTATION_EX_ATTR_S stRotationEx;
191 
192     if (u32Angle == 0) {
193         return s32Ret;
194     }
195 
196     stRotationEx.bEnable = RK_TRUE;
197     stRotationEx.stRotationEx.u32Angle = u32Angle;
198     s32Ret = RK_MPI_VPSS_SetChnRotationEx(VpssGrp, VpssChn, &stRotationEx);
199     if (s32Ret != RK_SUCCESS) {
200         return s32Ret;
201     }
202     s32Ret = RK_MPI_VPSS_GetChnRotationEx(VpssGrp, VpssChn, &stRotationEx);
203     if (s32Ret != RK_SUCCESS) {
204         return s32Ret;
205     }
206     if (stRotationEx.bEnable != RK_TRUE
207             || stRotationEx.stRotationEx.u32Angle != u32Angle) {
208         s32Ret = RK_FAILURE;
209         return s32Ret;
210     }
211 
212     return s32Ret;
213 }
214 
215 
TEST_VPSS_Proc(void * pArgs)216 void* TEST_VPSS_Proc(void *pArgs) {
217     TEST_VPSS_PROC_S *pstCtx = reinterpret_cast<TEST_VPSS_PROC_S *>(pArgs);
218     RK_S32           s32Ret = RK_SUCCESS;
219     MB_BLK           srcBlk = MB_INVALID_HANDLE;
220     RK_S32           s32ReadLen = 0;
221     RK_S32           s32FrameCount = 0;
222     VIDEO_FRAME_INFO_S frameIn = {0};
223     VIDEO_FRAME_INFO_S frameOut = {0};
224     PIC_BUF_ATTR_S  stPicBufAttr;
225     MB_PIC_CAL_S    stMbPicCalResult;
226 
227     stPicBufAttr.u32Width = pstCtx->stProcCtx.u32RawWidth;
228     stPicBufAttr.u32Height = pstCtx->stProcCtx.u32RawHeight;
229     stPicBufAttr.enCompMode = COMPRESS_MODE_NONE;
230     stPicBufAttr.enPixelFormat = (PIXEL_FORMAT_E)pstCtx->stProcCtx.u32RawPixelFmt;
231     s32Ret = RK_MPI_CAL_VGS_GetPicBufferSize(&stPicBufAttr, &stMbPicCalResult);
232     if (s32Ret != RK_SUCCESS) {
233         RK_LOGE("get picture buffer size failed. err 0x%x", s32Ret);
234         return RK_NULL;
235     }
236 
237     RK_MPI_MMZ_Alloc(&srcBlk, stMbPicCalResult.u32MBSize, RK_MMZ_ALLOC_CACHEABLE);
238     while (pstCtx->bThreadStart == RK_TRUE) {
239         if (pstCtx->srcFp != RK_NULL) {
240             s32Ret = fseek(pstCtx->srcFp, 0, SEEK_SET);
241             if (s32Ret != 0) {
242                 break;
243             }
244             s32ReadLen = fread(RK_MPI_MB_Handle2VirAddr(srcBlk), 1, stMbPicCalResult.u32MBSize, pstCtx->srcFp);
245             if (s32ReadLen <= 0) {
246                 RK_LOGE("read size is not enough. read %d, request %d", s32ReadLen, stMbPicCalResult.u32MBSize);
247                 break;
248             }
249         } else {
250             s32Ret = TEST_COMM_FillImage((RK_U8 *)RK_MPI_MB_Handle2VirAddr(srcBlk),
251                             pstCtx->stProcCtx.u32RawWidth,
252                             pstCtx->stProcCtx.u32RawHeight,
253                             RK_MPI_CAL_COMM_GetHorStride(pstCtx->stProcCtx.u32RawWidth,
254                                     (PIXEL_FORMAT_E)pstCtx->stProcCtx.u32RawPixelFmt),
255                             stMbPicCalResult.u32VirHeight,
256                             (PIXEL_FORMAT_E)pstCtx->stProcCtx.u32RawPixelFmt,
257                             s32FrameCount);
258             if (s32Ret != RK_SUCCESS) {
259                 RK_MPI_MB_ReleaseMB(srcBlk);
260                 break;
261             }
262         }
263         RK_MPI_SYS_MmzFlushCache(srcBlk, RK_FALSE);
264         frameIn.stVFrame.pMbBlk = srcBlk;
265         frameIn.stVFrame.u32Width = pstCtx->stProcCtx.u32RawWidth;
266         frameIn.stVFrame.u32Height = pstCtx->stProcCtx.u32RawHeight;
267         frameIn.stVFrame.u32VirWidth = stMbPicCalResult.u32VirWidth;
268         frameIn.stVFrame.u32VirHeight = stMbPicCalResult.u32VirHeight;
269         frameIn.stVFrame.enPixelFormat = (PIXEL_FORMAT_E)pstCtx->stProcCtx.u32RawPixelFmt;
270         frameIn.stVFrame.enCompressMode = COMPRESS_MODE_NONE;
271         s32Ret = RK_MPI_VPSS_SendFrame(pstCtx->VpssGrp, 0, &frameIn, -1);
272         if (s32Ret != RK_SUCCESS) {
273             RK_MPI_MB_ReleaseMB(srcBlk);
274             break;
275         }
276         s32FrameCount++;
277 
278         for (RK_S32 i = 0; i < pstCtx->u32VpssChnNum; i++) {
279             s32Ret = RK_MPI_VPSS_GetChnFrame(pstCtx->VpssGrp, i, &frameOut, -1);
280             if (s32Ret != RK_SUCCESS) {
281                 continue;
282             }
283             RK_LOGI("get chn[%d] frame %p length %d", i, frameOut.stVFrame.pMbBlk, stMbPicCalResult.u32MBSize);
284             if (pstCtx->dstSaveFp != RK_NULL) {
285                 RK_MPI_SYS_MmzFlushCache(frameOut.stVFrame.pMbBlk, RK_TRUE);
286                 fwrite(RK_MPI_MB_Handle2VirAddr(frameOut.stVFrame.pMbBlk),
287                         1, stMbPicCalResult.u32MBSize, pstCtx->dstSaveFp);
288                 fflush(pstCtx->dstSaveFp);
289             }
290             RK_MPI_VPSS_ReleaseChnFrame(pstCtx->VpssGrp, i, &frameOut);
291             usleep(1000000 / pstCtx->u32SendFrameRate);
292         }
293     }
294 
295     return RK_NULL;
296 }
297 
TEST_VPSS_StartProc(VPSS_GRP VpssGrp,RK_U32 u32ChnNum,const TEST_VPSS_PROC_CTX_S * pstProcCtx)298 RK_S32 TEST_VPSS_StartProc(
299         VPSS_GRP VpssGrp, RK_U32 u32ChnNum,
300         const TEST_VPSS_PROC_CTX_S *pstProcCtx) {
301     RK_S32 s32Ret = 0;
302 
303     memcpy(&(gProcThread[VpssGrp].stProcCtx), pstProcCtx, sizeof(TEST_VPSS_PROC_CTX_S));
304 
305     if (gProcThread[VpssGrp].stProcCtx.srcFileName != RK_NULL) {
306         gProcThread[VpssGrp].srcFp = fopen(gProcThread[VpssGrp].stProcCtx.srcFileName, "r");
307         if (gProcThread[VpssGrp].srcFp == RK_NULL) {
308             RK_LOGE("can't open file %s!", gProcThread[VpssGrp].stProcCtx.srcFileName);
309             return RK_FAILURE;
310         }
311     }
312     if (gProcThread[VpssGrp].stProcCtx.dstSaveFileName != RK_NULL) {
313         gProcThread[VpssGrp].dstSaveFp = fopen(gProcThread[VpssGrp].stProcCtx.dstSaveFileName, "wb");
314         if (gProcThread[VpssGrp].dstSaveFp == RK_NULL) {
315             RK_LOGE("can't open file %s!", gProcThread[VpssGrp].stProcCtx.dstSaveFileName);
316             return RK_FAILURE;
317         }
318     }
319 
320     gProcThread[VpssGrp].bThreadStart = RK_TRUE;
321     gProcThread[VpssGrp].VpssGrp = VpssGrp;
322     gProcThread[VpssGrp].u32VpssChnNum = u32ChnNum;
323     gProcThread[VpssGrp].u32SendFrameRate = pstProcCtx->u32SendFrameRate;
324 
325     s32Ret = pthread_create(&(gProcThread[VpssGrp].VpssProcPid), 0,
326                             TEST_VPSS_Proc, (RK_VOID *)&(gProcThread[VpssGrp]));
327     if (s32Ret != 0) {
328         return s32Ret;
329     }
330 
331     return s32Ret;
332 }
333 
TEST_VPSS_StopProc(VPSS_GRP VpssGrp)334 RK_S32 TEST_VPSS_StopProc(VPSS_GRP VpssGrp) {
335     if (RK_TRUE == gProcThread[VpssGrp].bThreadStart) {
336         gProcThread[VpssGrp].bThreadStart = RK_FALSE;
337         pthread_join(gProcThread[VpssGrp].VpssProcPid, 0);
338         if (gProcThread[VpssGrp].srcFp != RK_NULL) {
339             fclose(gProcThread[VpssGrp].srcFp);
340             gProcThread[VpssGrp].srcFp = RK_NULL;
341         }
342         if (gProcThread[VpssGrp].dstSaveFp != RK_NULL) {
343             fclose(gProcThread[VpssGrp].dstSaveFp);
344             gProcThread[VpssGrp].dstSaveFp = RK_NULL;
345         }
346     }
347 
348     return RK_SUCCESS;
349 }
350 
351 #ifdef __cplusplus
352 #if __cplusplus
353 }
354 #endif
355 #endif /* End of #ifdef __cplusplus */
356