xref: /OK3568_Linux_fs/external/rockit/mpi/example/common/test_mod_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 "test_mod_vpss.h"
23 #include "test_comm_imgproc.h"
24 #include "test_comm_sys.h"
25 #include "test_comm_utils.h"
26 #include "test_comm_vpss.h"
27 
28 #include <stdio.h>
29 #include <string.h>
30 #include <pthread.h>
31 #include <errno.h>
32 
33 #include "rk_debug.h"
34 #include "rk_mpi_vpss.h"
35 #include "rk_mpi_mb.h"
36 #include "rk_mpi_sys.h"
37 #include "rk_mpi_cal.h"
38 
39 #define VPSS_GET_CHN_FRAME_TIMEOUT_MS      200
40 
TEST_VPSS_InitAttr(TEST_VPSS_CTX_S * pstCtx,VPSS_GRP_ATTR_S * pstVpssGrpAttr,VPSS_CHN_ATTR_S * pstVpssChnAttrs)41 static void TEST_VPSS_InitAttr(
42         TEST_VPSS_CTX_S *pstCtx, VPSS_GRP_ATTR_S *pstVpssGrpAttr, VPSS_CHN_ATTR_S *pstVpssChnAttrs) {
43     memset(pstVpssGrpAttr, 0, sizeof(VPSS_GRP_ATTR_S));
44     memset(pstVpssChnAttrs, 0, sizeof(VPSS_CHN_ATTR_S) * VPSS_MAX_CHN_NUM);
45 
46     pstVpssGrpAttr->u32MaxW = 4096;
47     pstVpssGrpAttr->u32MaxH = 4096;
48     pstVpssGrpAttr->enPixelFormat = (PIXEL_FORMAT_E)pstCtx->s32SrcPixFormat;
49     pstVpssGrpAttr->enCompressMode = (COMPRESS_MODE_E)pstCtx->s32SrcCompressMode;
50     pstVpssGrpAttr->stFrameRate.s32SrcFrameRate = pstCtx->s32SrcGrpRate;
51     pstVpssGrpAttr->stFrameRate.s32DstFrameRate = pstCtx->s32DstGrpRate;
52 
53     for (RK_S32 i = 0; i < VPSS_MAX_CHN_NUM; i++) {
54         pstVpssChnAttrs[i].enChnMode = (VPSS_CHN_MODE_E)pstCtx->s32ChnMode;
55         pstVpssChnAttrs[i].enCompressMode = (COMPRESS_MODE_E)pstCtx->s32DstCompressMode;
56         pstVpssChnAttrs[i].enDynamicRange = DYNAMIC_RANGE_SDR8;
57         pstVpssChnAttrs[i].enPixelFormat = (PIXEL_FORMAT_E)pstCtx->s32DstPixFormat;
58         pstVpssChnAttrs[i].stFrameRate.s32SrcFrameRate = pstCtx->s32SrcChnRate;
59         pstVpssChnAttrs[i].stFrameRate.s32DstFrameRate = pstCtx->s32DstChnRate;
60         pstVpssChnAttrs[i].u32Width = pstCtx->s32DstWidth;
61         pstVpssChnAttrs[i].u32Height = pstCtx->s32DstHeight;
62         pstVpssChnAttrs[i].u32Depth = pstCtx->u32ChnDepth;
63         pstVpssChnAttrs[i].bFlip = (RK_BOOL)pstCtx->s32Flip;
64         pstVpssChnAttrs[i].bMirror = (RK_BOOL)pstCtx->s32Mirror;
65     }
66 }
67 
TEST_VPSS_CreatePool(TEST_VPSS_CTX_S * pstCtx)68 static MB_POOL TEST_VPSS_CreatePool(TEST_VPSS_CTX_S *pstCtx) {
69     MB_POOL_CONFIG_S stMbPoolCfg;
70     PIC_BUF_ATTR_S stPicBufAttr;
71     MB_PIC_CAL_S stMbPicCalResult;
72     RK_S32 s32Ret = RK_SUCCESS;
73 
74     stPicBufAttr.u32Width = pstCtx->s32SrcWidth;
75     stPicBufAttr.u32Height = pstCtx->s32SrcHeight;
76     stPicBufAttr.enCompMode = (COMPRESS_MODE_E)pstCtx->s32SrcCompressMode;
77     stPicBufAttr.enPixelFormat = (PIXEL_FORMAT_E)pstCtx->s32SrcPixFormat;
78     s32Ret = RK_MPI_CAL_VGS_GetPicBufferSize(&stPicBufAttr, &stMbPicCalResult);
79     if (s32Ret != RK_SUCCESS) {
80         RK_LOGE("get picture buffer size failed. err 0x%x", s32Ret);
81         return MB_INVALID_POOLID;
82     }
83 
84     memset(&stMbPoolCfg, 0, sizeof(MB_POOL_CONFIG_S));
85     stMbPoolCfg.u64MBSize = stMbPicCalResult.u32MBSize;
86     stMbPoolCfg.u32MBCnt  = 10;
87     stMbPoolCfg.enRemapMode = MB_REMAP_MODE_CACHED;
88     stMbPoolCfg.bPreAlloc = RK_TRUE;
89 
90     return RK_MPI_MB_CreatePool(&stMbPoolCfg);
91 }
92 
TEST_VPSS_ModSingleTest(void * arg)93 static void *TEST_VPSS_ModSingleTest(void *arg) {
94     RK_S32           s32Ret = RK_SUCCESS;
95     TEST_VPSS_CTX_S *pstCtx = reinterpret_cast<TEST_VPSS_CTX_S *>(arg);
96     void *retArg = RK_NULL;
97     VIDEO_FRAME_INFO_S stChnFrameInfos[VPSS_MAX_CHN_NUM];
98     VIDEO_FRAME_INFO_S stGrpFrameInfo;
99     char cWritePath[128] = {0};
100 
101     memset(stChnFrameInfos, 0, sizeof(VIDEO_FRAME_INFO_S) * VPSS_MAX_CHN_NUM);
102     memset(&stGrpFrameInfo, 0, sizeof(VIDEO_FRAME_INFO_S));
103 
104     s32Ret = TEST_VPSS_ModInit(pstCtx);
105     if (s32Ret != RK_SUCCESS) {
106         goto __FAILED;
107     }
108 
109     for (RK_S32 loopCount = 0; loopCount < pstCtx->s32LoopCount; loopCount++) {
110         s32Ret = TEST_VPSS_ModSendFrame(pstCtx);
111         if (s32Ret != RK_SUCCESS) {
112             goto __FAILED;
113         }
114         s32Ret = TEST_VPSS_ModGetChnFrame(pstCtx, stChnFrameInfos);
115         if (s32Ret != RK_SUCCESS) {
116             goto __FAILED;
117         }
118         for (RK_S32 i = 0; i < pstCtx->s32ChnNum; i++) {
119             if (pstCtx->dstFilePath) {
120                 snprintf(cWritePath, sizeof(cWritePath), "%schn_out_%dx%d_%d_%d.bin",
121                          pstCtx->dstFilePath, stChnFrameInfos[i].stVFrame.u32VirWidth,
122                          stChnFrameInfos[i].stVFrame.u32VirHeight, pstCtx->s32GrpIndex, i);
123                 s32Ret = TEST_COMM_FileWriteOneFrame(cWritePath, &(stChnFrameInfos[i]));
124                 if (s32Ret != RK_SUCCESS) {
125                     goto __FAILED;
126                 }
127             }
128             s32Ret = RK_MPI_VPSS_ReleaseChnFrame(pstCtx->s32GrpIndex, i, &(stChnFrameInfos[i]));
129             if (s32Ret != RK_SUCCESS) {
130                 goto __FAILED;
131             }
132         }
133     }
134 
135     retArg = arg;
136 __FAILED:
137     TEST_VPSS_ModDeInit(pstCtx);
138     if (retArg == RK_NULL) {
139         RK_LOGE("single vpss test %d running failed.", pstCtx->s32GrpIndex);
140     }
141     return retArg;
142 }
143 
TEST_VPSS_ModTest(TEST_VPSS_CTX_S * ctx)144 RK_S32 TEST_VPSS_ModTest(TEST_VPSS_CTX_S *ctx) {
145     RK_S32 s32Ret = RK_SUCCESS;
146     pthread_t tids[VPSS_MAX_GRP_NUM];
147     TEST_VPSS_CTX_S tmpCtx[VPSS_MAX_GRP_NUM];
148 
149     for (RK_S32 grpIndex = 0; grpIndex < ctx->s32GrpNum; grpIndex++) {
150         memcpy(&(tmpCtx[grpIndex]), ctx, sizeof(TEST_VPSS_CTX_S));
151         pthread_create(&tids[grpIndex], 0, TEST_VPSS_ModSingleTest, reinterpret_cast<void *>(&tmpCtx[grpIndex]));
152         ctx->s32GrpIndex++;
153     }
154 
155     for (RK_S32 grpIndex = 0; grpIndex < ctx->s32GrpNum; grpIndex++) {
156         void *retval = RK_NULL;
157         pthread_join(tids[grpIndex], &retval);
158         if (retval == RK_NULL) {
159             RK_LOGE("Has error test.");
160             s32Ret = RK_FAILURE;
161         }
162     }
163 
164     return s32Ret;
165 }
166 
TEST_VPSS_UnitTest(TEST_VPSS_CTX_S * pstCtx,VIDEO_FRAME_INFO_S * pstChnFrames)167 RK_S32 TEST_VPSS_UnitTest(TEST_VPSS_CTX_S *pstCtx, VIDEO_FRAME_INFO_S *pstChnFrames) {
168     RK_S32           s32Ret = RK_SUCCESS;
169 
170     s32Ret = TEST_VPSS_ModInit(pstCtx);
171     if (s32Ret != RK_SUCCESS) {
172         goto __FAILED;
173     }
174     s32Ret = TEST_VPSS_ModSendFrame(pstCtx);
175     if (s32Ret != RK_SUCCESS) {
176         goto __FAILED;
177     }
178     s32Ret = TEST_VPSS_ModGetChnFrame(pstCtx, pstChnFrames);
179     if (s32Ret != RK_SUCCESS) {
180         goto __FAILED;
181     }
182 
183 __FAILED:
184     TEST_VPSS_ModDeInit(pstCtx);
185 
186     return s32Ret;
187 }
188 
TEST_VPSS_ModInit(TEST_VPSS_CTX_S * pstCtx)189 RK_S32 TEST_VPSS_ModInit(TEST_VPSS_CTX_S *pstCtx) {
190     RK_S32           s32Ret = RK_SUCCESS;
191     MB_BLK           srcBlk = MB_INVALID_HANDLE;
192     VPSS_GRP_ATTR_S stVpssGrpAttr;
193     VPSS_CHN_ATTR_S stVpssChnAttr[VPSS_MAX_CHN_NUM];
194 
195     TEST_VPSS_InitAttr(pstCtx, &stVpssGrpAttr, stVpssChnAttr);
196 
197     if (pstCtx->bAttachPool) {
198         VPSS_MOD_PARAM_S stModParam;
199         stModParam.enVpssMBSource = MB_SOURCE_USER;
200         s32Ret = RK_MPI_VPSS_SetModParam(&stModParam);
201         if (s32Ret != RK_SUCCESS) {
202             goto __FAILED;
203         }
204     }
205 
206     s32Ret = TEST_VPSS_Start(pstCtx->s32GrpIndex, pstCtx->s32ChnNum,
207                              &stVpssGrpAttr, stVpssChnAttr);
208     if (s32Ret != RK_SUCCESS) {
209         goto __FAILED;
210     }
211 
212     s32Ret = RK_MPI_VPSS_SetVProcDev(pstCtx->s32GrpIndex, (VIDEO_PROC_DEV_TYPE_E)pstCtx->s32VProcDevType);
213     if (s32Ret != RK_SUCCESS) {
214         RK_LOGE("RK_MPI_VPSS_SetVProcDev(grp:%d) failed with %#x!", pstCtx->s32GrpIndex, s32Ret);
215         return s32Ret;
216     }
217 
218     s32Ret = TEST_VPSS_GrpSetZoom(pstCtx->s32GrpIndex, pstCtx->s32GrpCropRatio, pstCtx->bGrpCropEn);
219     if (s32Ret != RK_SUCCESS) {
220         goto __FAILED;
221     }
222 
223     s32Ret = RK_MPI_VPSS_SetGrpRotation(pstCtx->s32GrpIndex, (ROTATION_E)pstCtx->s32GrpRotation);
224     if (s32Ret != RK_SUCCESS) {
225         goto __FAILED;
226     }
227 
228     for (RK_S32 chnIndex = 0; chnIndex < pstCtx->s32ChnNum; chnIndex++) {
229         s32Ret = TEST_VPSS_ChnSetZoom(pstCtx->s32GrpIndex, chnIndex, pstCtx->s32ChnCropRatio, pstCtx->bChnCropEn);
230         if (s32Ret != RK_SUCCESS) {
231             goto __FAILED;
232         }
233         s32Ret = TEST_VPSS_SetChnRotation(pstCtx->s32GrpIndex, chnIndex, (ROTATION_E)pstCtx->s32Rotation);
234         if (s32Ret != RK_SUCCESS) {
235             goto __FAILED;
236         }
237         s32Ret = TEST_VPSS_SetChnRotationEx(pstCtx->s32GrpIndex, chnIndex, pstCtx->s32RotationEx);
238         if (s32Ret != RK_SUCCESS) {
239             goto __FAILED;
240         }
241         if (pstCtx->bAttachPool) {
242             pstCtx->attachPool = TEST_VPSS_CreatePool(pstCtx);
243             if (pstCtx->attachPool == MB_INVALID_POOLID) {
244                 s32Ret = RK_FAILURE;
245                 goto __FAILED;
246             }
247             s32Ret = RK_MPI_VPSS_AttachMbPool(pstCtx->s32GrpIndex, chnIndex, pstCtx->attachPool);
248             if (s32Ret != RK_SUCCESS) {
249                 goto __FAILED;
250             }
251         }
252     }
253 
254     return RK_SUCCESS;
255 __FAILED:
256     for (RK_S32 i = 0; i < pstCtx->s32ChnNum; i++) {
257         if (pstCtx->bAttachPool) {
258             s32Ret = RK_MPI_VPSS_DetachMbPool(pstCtx->s32GrpIndex, i);
259             if (s32Ret != RK_SUCCESS) {
260                 continue;
261             }
262         }
263     }
264     if (pstCtx->bAttachPool) {
265         RK_MPI_MB_DestroyPool(pstCtx->attachPool);
266     }
267     return s32Ret;
268 }
269 
TEST_VPSS_ModDeInit(TEST_VPSS_CTX_S * pstCtx)270 RK_S32 TEST_VPSS_ModDeInit(TEST_VPSS_CTX_S *pstCtx) {
271     RK_S32 s32Ret = RK_SUCCESS;
272 
273     for (RK_S32 i = 0; i < pstCtx->s32ChnNum; i++) {
274         if (pstCtx->bAttachPool) {
275             s32Ret = RK_MPI_VPSS_DetachMbPool(pstCtx->s32GrpIndex, i);
276             if (s32Ret != RK_SUCCESS) {
277                 continue;
278             }
279         }
280     }
281     TEST_VPSS_Stop(pstCtx->s32GrpIndex, pstCtx->s32ChnNum);
282     if (pstCtx->bAttachPool) {
283         RK_MPI_MB_DestroyPool(pstCtx->attachPool);
284     }
285 
286     return s32Ret;
287 }
288 
289 
TEST_VPSS_ModSendFrame(TEST_VPSS_CTX_S * pstCtx)290 RK_S32 TEST_VPSS_ModSendFrame(TEST_VPSS_CTX_S *pstCtx) {
291     RK_S32 s32Ret = RK_SUCCESS;
292     PIC_BUF_ATTR_S stPicBufAttr;
293     VIDEO_FRAME_INFO_S stVideoFrame;
294 
295     memset(&stVideoFrame, 0x0, sizeof(VIDEO_FRAME_INFO_S));
296 
297     stPicBufAttr.u32Width = pstCtx->s32SrcWidth;
298     stPicBufAttr.u32Height = pstCtx->s32SrcHeight;
299     stPicBufAttr.enPixelFormat = (PIXEL_FORMAT_E)pstCtx->s32SrcPixFormat;
300     stPicBufAttr.enCompMode = (COMPRESS_MODE_E)pstCtx->s32SrcCompressMode;
301     s32Ret = TEST_SYS_CreateVideoFrame(&stPicBufAttr, &stVideoFrame);
302     if (s32Ret != RK_SUCCESS) {
303         goto __FAILED;
304     }
305     if (pstCtx->srcFileName != RK_NULL) {
306         s32Ret = TEST_COMM_FileReadOneFrame(pstCtx->srcFileName, &stVideoFrame);
307         if (s32Ret != RK_SUCCESS) {
308             goto __FAILED;
309         }
310     } else {
311         s32Ret = TEST_COMM_FillImage(
312                             (RK_U8 *)RK_MPI_MB_Handle2VirAddr(stVideoFrame.stVFrame.pMbBlk),
313                             pstCtx->s32SrcWidth, pstCtx->s32SrcHeight,
314                             RK_MPI_CAL_COMM_GetHorStride(pstCtx->s32SrcVirWidth,
315                                             (PIXEL_FORMAT_E)pstCtx->s32SrcPixFormat),
316                             pstCtx->s32SrcVirHeight,
317                             (PIXEL_FORMAT_E)pstCtx->s32SrcPixFormat, 1);
318         if (s32Ret != RK_SUCCESS) {
319             goto __FAILED;
320         }
321     }
322     RK_MPI_SYS_MmzFlushCache(stVideoFrame.stVFrame.pMbBlk, RK_FALSE);
323 
324     s32Ret = RK_MPI_VPSS_SendFrame(pstCtx->s32GrpIndex, 0, &stVideoFrame, -1);
325     if (s32Ret != RK_SUCCESS) {
326         goto __FAILED;
327     }
328 
329 __FAILED:
330     RK_MPI_MB_ReleaseMB(stVideoFrame.stVFrame.pMbBlk);
331 
332     return s32Ret;
333 }
334 
TEST_VPSS_ModGetGrpFrame(TEST_VPSS_CTX_S * pstCtx,VIDEO_FRAME_INFO_S * pstVideoFrame)335 RK_S32 TEST_VPSS_ModGetGrpFrame(TEST_VPSS_CTX_S *pstCtx, VIDEO_FRAME_INFO_S *pstVideoFrame) {
336     RK_S32 s32Ret = RK_SUCCESS;
337 
338     s32Ret = RK_MPI_VPSS_GetGrpFrame(pstCtx->s32GrpIndex, 0, pstVideoFrame);
339     if (s32Ret != RK_SUCCESS) {
340         return s32Ret;
341     }
342 
343     return s32Ret;
344 }
345 
TEST_VPSS_ModGetChnFrame(TEST_VPSS_CTX_S * pstCtx,VIDEO_FRAME_INFO_S * pstVideoFrame)346 RK_S32 TEST_VPSS_ModGetChnFrame(TEST_VPSS_CTX_S *pstCtx, VIDEO_FRAME_INFO_S *pstVideoFrame) {
347     RK_S32 s32Ret = RK_SUCCESS;
348 
349     for (RK_S32 i = 0; i < pstCtx->s32ChnNum; i++) {
350         s32Ret = RK_MPI_VPSS_GetChnFrame(pstCtx->s32GrpIndex, i, &(pstVideoFrame[i]), VPSS_GET_CHN_FRAME_TIMEOUT_MS);
351         if (s32Ret != RK_SUCCESS) {
352             return RK_SUCCESS;
353         }
354     }
355 
356     return s32Ret;
357 }
358 
359 #ifdef __cplusplus
360 #if __cplusplus
361 }
362 #endif
363 #endif /* End of #ifdef __cplusplus */
364 
365