hc
2024-10-12 a5969cabbb4660eab42b6ef0412cbbd1200cf14d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * Copyright 2020 Rockchip Electronics Co. LTD
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
#include <math.h>
#include <stdlib.h>
#include <unistd.h>
 
#include "RTNodeVFilterVideoOutput.h"          // NOLINT
#include "RTNodeCommon.h"
#include "RTVideoFrame.h"
#include <sys/time.h>
#ifdef RV1126_RV1109
#include "drmDsp.h"
#endif
 
#ifdef LOG_TAG
#undef LOG_TAG
#endif
 
#define LOG_TAG "RTNodeVFilterVideoOutput"
#define kStubRockitVideoOutputDemo                MKTAG('r', 'k', 'v', 'o')
 
static RK_S32 VO_ENABLE()
{
    /* Enable VO */
    VO_PUB_ATTR_S VoPubAttr;
    VO_VIDEO_LAYER_ATTR_S stLayerAttr;
    RK_S32 s32Ret;
    VO_CHN_ATTR_S stChnAttr;
    VO_CHN VoChn;
    VoChn = 0;
 
    s32Ret = RK_MPI_SYS_Init();
    RT_LOGD("RK_MPI_SYS_Init ret = %d", s32Ret);
 
    VO_LAYER VoLayer = RK356X_VOP_LAYER_CLUSTER_0;
    VO_DEV VoDev = RK356X_VO_DEV_HD0;
 
    RK_MPI_VO_DisableLayer(VoLayer);
    RK_MPI_VO_DisableLayer(RK356X_VOP_LAYER_ESMART_0);
    RK_MPI_VO_DisableLayer(RK356X_VOP_LAYER_ESMART_1);
    RK_MPI_VO_DisableLayer(RK356X_VOP_LAYER_SMART_0);
    RK_MPI_VO_DisableLayer(RK356X_VOP_LAYER_SMART_1);
    RK_MPI_VO_Disable(VoDev);
    RT_LOGD("[%s] unint VO config first\n", __func__);
 
    memset(&VoPubAttr, 0, sizeof(VO_PUB_ATTR_S));
    memset(&stLayerAttr, 0, sizeof(VO_VIDEO_LAYER_ATTR_S));
 
    stLayerAttr.enPixFormat = RK_FMT_YUV420SP;
    stLayerAttr.stDispRect.s32X = 0;
    stLayerAttr.stDispRect.s32Y = 0;
    stLayerAttr.u32DispFrmRt = 30;
    stLayerAttr.stDispRect.u32Width = 1920;
    stLayerAttr.stDispRect.u32Height = 1080;
    stLayerAttr.stImageSize.u32Width = 1920;
    stLayerAttr.stImageSize.u32Height = 1080;
 
    s32Ret = RK_MPI_VO_GetPubAttr(VoDev, &VoPubAttr);
    if (s32Ret != RK_SUCCESS)
    {
        return s32Ret;
    }
 
    VoPubAttr.enIntfType = VO_INTF_HDMI;
    VoPubAttr.enIntfSync = VO_OUTPUT_1080P60;
 
    s32Ret = RK_MPI_VO_SetPubAttr(VoDev, &VoPubAttr);
    if (s32Ret != RK_SUCCESS)
    {
        return s32Ret;
    }
    s32Ret = RK_MPI_VO_Enable(VoDev);
    if (s32Ret != RK_SUCCESS)
    {
        return s32Ret;
    }
 
    s32Ret = RK_MPI_VO_SetLayerAttr(VoLayer, &stLayerAttr);
    if (s32Ret != RK_SUCCESS)
    {
        RK_LOGE("[%s] RK_MPI_VO_SetLayerAttr failed,s32Ret:%d\n", __func__, s32Ret);
        return RK_FAILURE;
    }
 
    s32Ret = RK_MPI_VO_BindLayer(VoLayer, VoDev, VO_LAYER_MODE_VIDEO);
    if (s32Ret != RK_SUCCESS)
    {
        RK_LOGE("[%s] RK_MPI_VO_BindLayer failed,s32Ret:%d\n", __func__, s32Ret);
        return RK_FAILURE;
    }
 
 
    s32Ret = RK_MPI_VO_EnableLayer(VoLayer);
    if (s32Ret != RK_SUCCESS)
    {
        RK_LOGE("[%s] RK_MPI_VO_EnableLayer failed,s32Ret:%d\n", __func__, s32Ret);
        return RK_FAILURE;
    }
 
    stChnAttr.stRect.s32X = 0;
    stChnAttr.stRect.s32Y = 0;
    stChnAttr.stRect.u32Width = stLayerAttr.stImageSize.u32Width;
    stChnAttr.stRect.u32Height = stLayerAttr.stImageSize.u32Height;
    stChnAttr.u32Priority = 0;
    if (1)
    {
        stChnAttr.u32FgAlpha = 128;
        stChnAttr.u32BgAlpha = 0;
    }
    else
    {
        stChnAttr.u32FgAlpha = 0;
        stChnAttr.u32BgAlpha = 128;
    }
 
    s32Ret = RK_MPI_VO_SetChnAttr(VoLayer, 0, &stChnAttr);
    if (s32Ret != RK_SUCCESS)
    {
        RK_LOGE("[%s] set chn Attr failed,s32Ret:%d\n", __func__, s32Ret);
        return RK_FAILURE;
    }
 
    s32Ret = RK_MPI_VO_EnableChn(VoLayer, 0);
    if (s32Ret != RK_SUCCESS)
    {
        RK_LOGE("[%s] Enalbe chn failed, s32Ret = %d\n", __func__, s32Ret);
        return RK_FAILURE;
    }
 
    return RK_SUCCESS;
}
 
RTNodeVFilterVideoOutput::RTNodeVFilterVideoOutput() {
    mLock = new RtMutex();
    RT_ASSERT(RT_NULL != mLock);
}
 
RTNodeVFilterVideoOutput::~RTNodeVFilterVideoOutput() {
    rt_safe_delete(mLock);
}
 
RT_RET RTNodeVFilterVideoOutput::open(RTTaskNodeContext *context) {
    RT_LOGD("wttt RTNodeVFilterVideoOutput open");
    RtMetaData* inputMeta   = context->options();
    drmInitSuccess = 0;
 
#ifdef RK356X
    VO_ENABLE();
    pstVFrame = (VIDEO_FRAME_INFO_S *)(calloc(sizeof(VIDEO_FRAME_INFO_S), 1));
 
#endif
 
#ifdef RV1126_RV1109
    if (initDrmDsp() < 0)
    {
        RT_LOGE("DRM display init failed\n");
    } else {
        drmInitSuccess = 1;
    }
#endif
 
 
    gettimeofday(&beginTime, NULL);
    frameCount = 0;
 
    return RT_OK;
}
 
RT_RET RTNodeVFilterVideoOutput::process(RTTaskNodeContext *context) {
    RT_RET         err       = RT_OK;
    RTVideoFrame *srcBuffer = RT_NULL;
    RtMutex::RtAutolock autoLock(mLock);
    RK_S32                ret = RT_OK;
 
    INT32 count = context->inputQueueSize("image:nv12");
    while (count) {
        srcBuffer = reinterpret_vframe(context->dequeInputBuffer("image:nv12"));
        if (srcBuffer == RT_NULL)
            continue;
 
        count--;
 
#ifdef RV1126_RV1109
        if (drmInitSuccess)
        {
            ret = drmDspFrame(srcBuffer->getWidth(), srcBuffer->getHeight(), srcBuffer->getData(), DRM_FORMAT_NV12);
            if (ret == RT_OK) {
                if (!access(HDMI_RKVO_DEBUG_FPS, 0)) {
                    ++frameCount;
                    if (frameCount == 100) {
                        struct timeval now_time;
                        gettimeofday(&now_time, NULL);
                        float use_times = (now_time.tv_sec * 1000 + now_time.tv_usec / 1000) -
                            (beginTime.tv_sec * 1000 + beginTime.tv_usec / 1000);
                        beginTime.tv_sec = now_time.tv_sec;
                        beginTime.tv_usec = now_time.tv_usec;
                        float fps = (1000 * frameCount) / use_times;
                        frameCount = 0;
                        RT_LOGE("hdmi rkvo fps = %0.1f", fps);
                    }
                }
            } else {
                RT_LOGE("drmDspFrame failed ret = %d", ret);
            }
        }
#endif
#ifdef RK356X
        RK_VOID               *pMblk;
        VO_LAYER              VoVideoLayer;
        VO_CHN                VoChn;
 
        VoVideoLayer = RK356X_VOP_LAYER_CLUSTER_0;
        VoChn = 0;
        /*fill pMbBlk*/
        pstVFrame->stVFrame.pMbBlk = srcBuffer;
        pstVFrame->stVFrame.u32Width = srcBuffer->getWidth();
        pstVFrame->stVFrame.u32Height = srcBuffer->getHeight();
        pstVFrame->stVFrame.u32VirWidth = srcBuffer->getWidth();
        pstVFrame->stVFrame.u32VirHeight = srcBuffer->getHeight();
        pstVFrame->stVFrame.enPixelFormat = RK_FMT_YUV420SP;
        pstVFrame->stVFrame.enCompressMode = COMPRESS_MODE_NONE;
        do {
            ret = RK_MPI_VO_SendFrame(VoVideoLayer, VoChn, pstVFrame, -1);
            if (ret == RT_OK) {
                if (!access(HDMI_RKVO_DEBUG_FPS, 0)) {
                    ++frameCount;
                    if (frameCount == 100) {
                        struct timeval now_time;
                        gettimeofday(&now_time, NULL);
                        float use_times = (now_time.tv_sec * 1000 + now_time.tv_usec / 1000) -
                            (beginTime.tv_sec * 1000 + beginTime.tv_usec / 1000);
                        beginTime.tv_sec = now_time.tv_sec;
                        beginTime.tv_usec = now_time.tv_usec;
                        float fps = (1000 * frameCount) / use_times;
                        frameCount = 0;
                        RT_LOGE("hdmi rkvo fps = %0.1f", fps);
                    }
                }
                break;
            } else {
                RT_LOGE("RK_MPI_VO_SendFrame failed ret = %d", ret);
                RK_MPI_VO_DestroyGraphicsFrameBuffer(pMblk);
                break;
            }
        } while (0);
#endif
        srcBuffer->release();
    }
    return err;
}
 
RT_RET RTNodeVFilterVideoOutput::close(RTTaskNodeContext *context) {
    RT_LOGD("wttt RTNodeVFilterVideoOutput close");
    RT_RET err = RT_OK;
#ifdef RV1126_RV1109
    if (drmInitSuccess)
    {
        deInitDrmDsp();
    }
#endif
 
#ifdef RK356X
    if(pstVFrame)
        free(pstVFrame);
#endif
 
    return err;
}
 
RT_RET RTNodeVFilterVideoOutput::invokeInternal(RtMetaData *meta) {
 
    RT_LOGD("wttt RTNodeVFilterVideoOutput invokeInternal");
    const char *command;
    if (RT_NULL == meta) {
        return RT_ERR_NULL_PTR;
    }
 
    return RT_OK;
}
 
static RTTaskNode* createVideoOutputFilter() {
    RT_LOGD("wttt RTNodeVFilterVideoOutput createVideoOutputFilter");
    return new RTNodeVFilterVideoOutput();
}
 
/*****************************************
 * register node stub to RTTaskNodeFactory
 *****************************************/
RTNodeStub node_stub_filter_video_output {
    .mUid          = kStubRockitVideoOutputDemo,
    .mName         = "rkvo",
    .mVersion      = "v1.0",
    .mCreateObj    = createVideoOutputFilter,
    .mCapsSrc      = { "video/x-raw", RT_PAD_SRC, RT_MB_TYPE_VFRAME, {RT_NULL, RT_NULL} },
    .mCapsSink     = { "video/x-raw", RT_PAD_SINK, RT_MB_TYPE_VFRAME, {RT_NULL, RT_NULL} },
};
 
RT_NODE_FACTORY_REGISTER_STUB(node_stub_filter_video_output);