xref: /OK3568_Linux_fs/external/rockit/mpi/example/common/test_comm_vdec.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 
17 #include <pthread.h>
18 
19 #include "test_comm_tmd.h"
20 
21 #include "rk_debug.h"
22 #include "rk_mpi_vdec.h"
23 #include "rk_mpi_sys.h"
24 
25 #ifdef __cplusplus
26 #if __cplusplus
27 extern "C" {
28 #endif
29 #endif /* End of #ifdef __cplusplus */
30 
31 typedef struct test_vdec_thread_s {
32      RK_BOOL bThreadStart;
33      pthread_t VdecPid;
34      STREAM_INFO_S stStreamInfo;
35 } TEST_VDEC_THREAD_S;
36 
37 // get pic thread info
38 static TEST_VDEC_THREAD_S gGetPicThread[VDEC_MAX_CHN_NUM];
39 // send stream thread info
40 static TEST_VDEC_THREAD_S gSendStremThread[VDEC_MAX_CHN_NUM];
41 
TEST_VDEC_Start(VDEC_CHN VdecChn,VDEC_CHN_ATTR_S * pstVdecAttr,VDEC_CHN_PARAM_S * pstVdecParam,VIDEO_DISPLAY_MODE_E enDispMode)42 RK_S32 TEST_VDEC_Start(VDEC_CHN VdecChn,
43                         VDEC_CHN_ATTR_S *pstVdecAttr,
44                         VDEC_CHN_PARAM_S *pstVdecParam,
45                         VIDEO_DISPLAY_MODE_E enDispMode) {
46     RK_S32 s32Ret = RK_SUCCESS;
47 
48     s32Ret = RK_MPI_VDEC_CreateChn(VdecChn, pstVdecAttr);
49     if (s32Ret != RK_SUCCESS) {
50         RK_LOGE("create %d vdec failed with %#x!", VdecChn, s32Ret);
51         return s32Ret;
52     }
53     s32Ret = RK_MPI_VDEC_SetChnParam(VdecChn, pstVdecParam);
54     if (s32Ret != RK_SUCCESS) {
55         RK_LOGE("RK_MPI_VDEC_SetChnParam failed with %#x!", s32Ret);
56         return s32Ret;
57     }
58     s32Ret = RK_MPI_VDEC_StartRecvStream(VdecChn);
59     if (s32Ret != RK_SUCCESS) {
60         RK_LOGE("RK_MPI_VDEC_StartRecvStream failed with %#x!", s32Ret);
61         return s32Ret;
62     }
63     s32Ret = RK_MPI_VDEC_SetDisplayMode(VdecChn, enDispMode);
64     if (s32Ret != RK_SUCCESS) {
65         RK_LOGE("RK_MPI_VDEC_SetDisplayMode failed with %#x!", s32Ret);
66         return s32Ret;
67     }
68 
69     return RK_SUCCESS;
70 }
71 
TEST_VDEC_Stop(VDEC_CHN VdecChn)72 RK_S32 TEST_VDEC_Stop(VDEC_CHN VdecChn) {
73     RK_S32 s32Ret;
74 
75     s32Ret = RK_MPI_VDEC_StopRecvStream(VdecChn);
76     if (RK_SUCCESS != s32Ret) {
77         RK_LOGE("RK_MPI_VDEC_StopRecvStream failed with %#x!", s32Ret);
78     }
79 
80     s32Ret = RK_MPI_VDEC_DestroyChn(VdecChn);
81     if (RK_SUCCESS != s32Ret) {
82         RK_LOGE("RK_MPI_VDEC_DestroyChn failed with %#x!", s32Ret);
83     }
84 
85     return s32Ret;
86 }
87 
TEST_VDEC_SendStreamProc(void * pArgs)88 static void* TEST_VDEC_SendStreamProc(void *pArgs) {
89     RK_S32 s32Ret = RK_SUCCESS;
90     RK_BOOL bReachEos = RK_FALSE;
91     MB_BLK pStreamBlk = MB_INVALID_HANDLE;
92     STREAM_DATA_S stStreamData;
93     MB_EXT_CONFIG_S stMbExtConfig;
94     VDEC_STREAM_S stStream;
95     TEST_VDEC_THREAD_S *pstThreadInfo = (TEST_VDEC_THREAD_S *)pArgs;
96     STREAM_INFO_S *pstStreamInfo = &pstThreadInfo->stStreamInfo;
97 
98     memset(&stMbExtConfig, 0, sizeof(MB_EXT_CONFIG_S));
99     memset(&stStream, 0, sizeof(VDEC_STREAM_S));
100 
101     while (pstThreadInfo->bThreadStart) {
102         memset(&stStreamData, 0, sizeof(STREAM_DATA_S));
103         s32Ret = TEST_COMM_TmdParserRead(pstStreamInfo, &stStreamData);
104         if (s32Ret != RK_SUCCESS) {
105             RK_LOGE("TEST_COMM_FFmParserRead failed with 0x%x!", s32Ret);
106             break;
107         }
108 
109         stMbExtConfig.pFreeCB = stStreamData.pFreeCB;
110         stMbExtConfig.pOpaque = stStreamData.pOpaque;
111         stMbExtConfig.pu8VirAddr = stStreamData.pu8VirAddr;
112         stMbExtConfig.u64Size = stStreamData.u64Size;
113 
114         RK_MPI_SYS_CreateMB(&pStreamBlk, &stMbExtConfig);
115 
116         stStream.u64PTS = stStreamData.u64PTS;
117         stStream.pMbBlk = pStreamBlk;
118         stStream.u32Len = stStreamData.u64Size;
119         stStream.bEndOfStream = stStreamData.bEndOfStream;
120         stStream.bEndOfFrame = stStreamData.bEndOfFrame;
121         stStream.bBypassMbBlk = RK_TRUE;
122 __RETRY:
123         s32Ret = RK_MPI_VDEC_SendStream(pstStreamInfo->VdecChn, &stStream, 200);
124         if (s32Ret != RK_SUCCESS) {
125             if (!pstThreadInfo->bThreadStart) {
126                 break;
127             }
128             RK_LOGV("RK_MPI_VDEC_SendStream failed with 0x%x", s32Ret);
129             goto  __RETRY;
130         }
131         RK_MPI_SYS_Free(stStream.pMbBlk);
132 
133         if (stStreamData.bEndOfStream) {
134             RK_LOGE("reach eos");
135             break;
136         }
137     }
138 
139     RK_LOGD("%s out\n", __FUNCTION__);
140     return RK_NULL;
141 }
142 
TEST_VDEC_StartSendStream(VDEC_CHN VdecChn,STREAM_INFO_S * pstStreamInfo)143 RK_S32 TEST_VDEC_StartSendStream(VDEC_CHN VdecChn, STREAM_INFO_S *pstStreamInfo) {
144     RK_S32 s32Ret = 0;
145 
146     gSendStremThread[VdecChn].bThreadStart = RK_TRUE;
147     memcpy(&gSendStremThread[VdecChn].stStreamInfo, pstStreamInfo, sizeof(STREAM_INFO_S));
148 
149     s32Ret = pthread_create(&(gSendStremThread[VdecChn].VdecPid), 0,
150                             TEST_VDEC_SendStreamProc,
151                             (RK_VOID *)&(gSendStremThread[VdecChn]));
152     if (s32Ret < 0)
153         return RK_FAILURE;
154 
155     return RK_SUCCESS;
156 }
157 
TEST_VENC_StopSendFrame(VDEC_CHN VdecChn)158 RK_S32 TEST_VENC_StopSendFrame(VDEC_CHN VdecChn) {
159     if (RK_TRUE == gSendStremThread[VdecChn].bThreadStart) {
160         gSendStremThread[VdecChn].bThreadStart = RK_FALSE;
161         pthread_join(gSendStremThread[VdecChn].VdecPid, 0);
162     }
163 
164     return RK_SUCCESS;
165 }
166 
167 
168 #ifdef __cplusplus
169 #if __cplusplus
170 }
171 #endif
172 #endif /* End of #ifdef __cplusplus */
173