• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 HiHope Open Source Organization .
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 "hdi_mpp_config.h"
17 #include <securec.h>
18 #include <hdf_base.h>
19 #include <hdf_log.h>
20 #include "mpp_common.h"
21 
22 #define HDF_LOG_TAG codec_hdi_mpp_config
23 #define MPP_ALIGN_STRIDE_WITH_EIGHT         8
24 #define MPP_ALIGN_STRIDE_WITH_SIXTEEN       16
25 #define MPP_ALIGN_STRIDE_WITH_SIXTY_FOUR    64
26 #define MPP_ALIGN_MULTIPLE_WITH_TWO         2
27 #define MPP_ALIGN_MULTIPLE_WITH_THREE       3
28 #define MPP_ALIGN_MULTIPLE_WITH_FOUR        4
29 #define MPP_ALIGN_DIVISOR_WITH_TWO          2
30 #define MPP_ALIGN_DIVISOR_WITH_THREE        3
31 #define MPP_ALIGN_DIVISOR_WITH_FOUR         4
32 #define MPP_ALIGN_DIVISOR_WITH_SIXTEEN      16
33 #define GOP_MODE_THRESHOLD_VALUE            4
34 
GetDefaultConfig(RKHdiBaseComponent * pBaseComponent)35 int32_t GetDefaultConfig(RKHdiBaseComponent *pBaseComponent)
36 {
37     MPP_RET ret = MPP_OK;
38     MppCtx ctx = pBaseComponent->ctx;
39     RKMppApi *mppApi = pBaseComponent->mppApi;
40 
41     mppApi->HdiMppDecCfgInit(&pBaseComponent->cfg);
42     ret = pBaseComponent->mpi->control(ctx, MPP_DEC_GET_CFG, pBaseComponent->cfg);
43     if (ret != MPP_OK) {
44         HDF_LOGE("%{public}s: config MPP_DEC_GET_CFG failed, ret %{public}d", __func__, ret);
45         return HDF_FAILURE;
46     }
47 
48     return HDF_SUCCESS;
49 }
50 
ConvertRKFormat2HdiFormat(MppFrameFormat fmtRK)51 CodecPixelFormat ConvertRKFormat2HdiFormat(MppFrameFormat fmtRK)
52 {
53     if (fmtRK == MPP_FMT_YUV420SP) {
54         return PIXEL_FORMAT_YCBCR_420_SP;
55     } else {
56         return PIXEL_FORMAT_NONE;
57     }
58 }
59 
ConvertHdiFormat2RKFormat(CodecPixelFormat fmtHDI)60 MppFrameFormat ConvertHdiFormat2RKFormat(CodecPixelFormat fmtHDI)
61 {
62     if (fmtHDI == PIXEL_FORMAT_YCBCR_420_SP) {
63         return MPP_FMT_YUV420SP;
64     } else {
65         HDF_LOGE("%{public}s: do not support type %{public}d", __func__, fmtHDI);
66         return MPP_FMT_BUTT;
67     }
68 }
69 
ConvertHdiRcMode2RKRcMode(VideoCodecRcMode hdiRcMode)70 MppEncRcMode ConvertHdiRcMode2RKRcMode(VideoCodecRcMode hdiRcMode)
71 {
72     MppEncRcMode rkRcMode = MPP_ENC_RC_MODE_VBR;
73     switch (hdiRcMode) {
74         case VID_CODEC_RC_CBR:
75             rkRcMode = MPP_ENC_RC_MODE_CBR;
76             break;
77         case VID_CODEC_RC_VBR:
78             rkRcMode = MPP_ENC_RC_MODE_VBR;
79             break;
80         case VID_CODEC_RC_AVBR:
81             rkRcMode = MPP_ENC_RC_MODE_AVBR;
82             break;
83         case VID_CODEC_RC_FIXQP:
84             rkRcMode = MPP_ENC_RC_MODE_FIXQP;
85             break;
86         default:
87             break;
88     }
89     return rkRcMode;
90 }
91 
ConvertHdiGopMode2RKGopMode(VideoCodecGopMode hdiGopMode)92 MppEncRcGopMode ConvertHdiGopMode2RKGopMode(VideoCodecGopMode hdiGopMode)
93 {
94     MppEncRcGopMode rkGopMode = MPP_ENC_RC_NORMAL_P;
95     switch (hdiGopMode) {
96         case VID_CODEC_GOPMODE_NORMALP:
97             rkGopMode = MPP_ENC_RC_NORMAL_P;
98             break;
99         case VID_CODEC_GOPMODE_SMARTP:
100             rkGopMode = MPP_ENC_RC_SMART_P;
101             break;
102         default:
103             break;
104     }
105     return rkGopMode;
106 }
107 
ConvertHdiMimeCodecType2RKCodecType(AvCodecMime mime)108 MppCodingType ConvertHdiMimeCodecType2RKCodecType(AvCodecMime mime)
109 {
110     MppCodingType type = MPP_VIDEO_CodingMax;
111     switch (mime) {
112         case MEDIA_MIMETYPE_IMAGE_JPEG:
113             type = MPP_VIDEO_CodingMJPEG;
114             break;
115         case MEDIA_MIMETYPE_VIDEO_AVC:
116             type = MPP_VIDEO_CodingAVC;
117             break;
118         case MEDIA_MIMETYPE_VIDEO_HEVC:
119             type = MPP_VIDEO_CodingHEVC;
120             break;
121         default:
122             break;
123     }
124     return type;
125 }
126 
GetStrideByWidth(int32_t width,MppFrameFormat fmt)127 int32_t GetStrideByWidth(int32_t width, MppFrameFormat fmt)
128 {
129     int32_t stride = 0;
130 
131     switch (fmt & MPP_FRAME_FMT_MASK) {
132         case MPP_FMT_YUV420SP:
133         case MPP_FMT_YUV420SP_VU:
134             stride = MPP_ALIGN(width, MPP_ALIGN_STRIDE_WITH_EIGHT);
135             break;
136         case MPP_FMT_YUV420P:
137             /* NOTE: 420P need to align to 16 so chroma can align to 8 */
138             stride = MPP_ALIGN(width, MPP_ALIGN_STRIDE_WITH_SIXTEEN);
139             break;
140         case MPP_FMT_YUV422P:
141         case MPP_FMT_YUV422SP:
142         case MPP_FMT_YUV422SP_VU:
143             /* NOTE: 422 need to align to 8 so chroma can align to 16 */
144             stride = MPP_ALIGN(width, MPP_ALIGN_STRIDE_WITH_EIGHT);
145             break;
146         case MPP_FMT_RGB565:
147         case MPP_FMT_BGR565:
148         case MPP_FMT_RGB555:
149         case MPP_FMT_BGR555:
150         case MPP_FMT_RGB444:
151         case MPP_FMT_BGR444:
152         case MPP_FMT_YUV422_YUYV:
153         case MPP_FMT_YUV422_YVYU:
154         case MPP_FMT_YUV422_UYVY:
155         case MPP_FMT_YUV422_VYUY:
156             /* NOTE: for vepu limitation */
157             stride = MPP_ALIGN(width, MPP_ALIGN_STRIDE_WITH_EIGHT) * MPP_ALIGN_MULTIPLE_WITH_TWO;
158             break;
159         case MPP_FMT_RGB888:
160         case MPP_FMT_BGR888:
161             /* NOTE: for vepu limitation */
162             stride = MPP_ALIGN(width, MPP_ALIGN_STRIDE_WITH_EIGHT) * MPP_ALIGN_MULTIPLE_WITH_THREE;
163             break;
164         case MPP_FMT_RGB101010:
165         case MPP_FMT_BGR101010:
166         case MPP_FMT_ARGB8888:
167         case MPP_FMT_ABGR8888:
168         case MPP_FMT_BGRA8888:
169         case MPP_FMT_RGBA8888:
170             /* NOTE: for vepu limitation */
171             stride = MPP_ALIGN(width, MPP_ALIGN_STRIDE_WITH_EIGHT) * MPP_ALIGN_MULTIPLE_WITH_FOUR;
172             break;
173         default :
174             break;
175     }
176 
177     return stride;
178 }
179 
GetDefaultHorStride(int32_t width,CodecPixelFormat fmtHDI)180 int32_t GetDefaultHorStride(int32_t width, CodecPixelFormat fmtHDI)
181 {
182     MppFrameFormat fmt = ConvertHdiFormat2RKFormat(fmtHDI);
183     return GetStrideByWidth(width, fmt);
184 }
185 
GetFrameBufferSize(RKHdiBaseComponent * pBaseComponent)186 uint32_t GetFrameBufferSize(RKHdiBaseComponent *pBaseComponent)
187 {
188     uint32_t frameSize = 0;
189     switch (pBaseComponent->fmt) {
190         case MPP_FMT_YUV420SP:
191         case MPP_FMT_YUV420P:
192             frameSize = MPP_ALIGN(pBaseComponent->setup.stride.horStride, MPP_ALIGN_STRIDE_WITH_SIXTY_FOUR)
193                 * MPP_ALIGN(pBaseComponent->setup.stride.verStride, MPP_ALIGN_STRIDE_WITH_SIXTY_FOUR)
194                 * MPP_ALIGN_MULTIPLE_WITH_THREE / MPP_ALIGN_DIVISOR_WITH_TWO;
195             break;
196 
197         case MPP_FMT_YUV422_YUYV:
198         case MPP_FMT_YUV422_YVYU:
199         case MPP_FMT_YUV422_UYVY:
200         case MPP_FMT_YUV422_VYUY:
201         case MPP_FMT_YUV422P:
202         case MPP_FMT_YUV422SP:
203         case MPP_FMT_RGB444:
204         case MPP_FMT_BGR444:
205         case MPP_FMT_RGB555:
206         case MPP_FMT_BGR555:
207         case MPP_FMT_RGB565:
208         case MPP_FMT_BGR565:
209             frameSize = MPP_ALIGN(pBaseComponent->setup.stride.horStride, MPP_ALIGN_STRIDE_WITH_SIXTY_FOUR)
210                 * MPP_ALIGN(pBaseComponent->setup.stride.verStride, MPP_ALIGN_STRIDE_WITH_SIXTY_FOUR)
211                 * MPP_ALIGN_MULTIPLE_WITH_TWO;
212             break;
213 
214         default:
215             frameSize = MPP_ALIGN(pBaseComponent->setup.stride.horStride, MPP_ALIGN_STRIDE_WITH_SIXTY_FOUR)
216                 * MPP_ALIGN(pBaseComponent->setup.stride.verStride, MPP_ALIGN_STRIDE_WITH_SIXTY_FOUR)
217                 * MPP_ALIGN_MULTIPLE_WITH_FOUR;
218             break;
219     }
220     return frameSize;
221 }
222 
GetFrameHeaderSize(RKHdiBaseComponent * pBaseComponent)223 uint32_t GetFrameHeaderSize(RKHdiBaseComponent *pBaseComponent)
224 {
225     uint32_t headerSize = 0;
226     if (MPP_FRAME_FMT_IS_FBC(pBaseComponent->fmt)) {
227         headerSize = MPP_ALIGN(MPP_ALIGN(pBaseComponent->setup.width, MPP_ALIGN_STRIDE_WITH_SIXTEEN)
228             * MPP_ALIGN(pBaseComponent->setup.height, MPP_ALIGN_STRIDE_WITH_SIXTEEN)
229             / MPP_ALIGN_DIVISOR_WITH_SIXTEEN, SZ_4K);
230     } else {
231         headerSize = 0;
232     }
233     return headerSize;
234 }
235 
GetMppBuffers(RKHdiBaseComponent * pBaseComponent)236 int32_t GetMppBuffers(RKHdiBaseComponent *pBaseComponent)
237 {
238     MPP_RET ret = MPP_OK;
239     RKMppApi *mppApi = pBaseComponent->mppApi;
240     ret = mppApi->HdiMppBufferGroupGet(&pBaseComponent->frmGrp,
241         MPP_BUFFER_TYPE_DRM, MPP_BUFFER_INTERNAL, NULL, __func__);
242     if (ret != MPP_OK) {
243         HDF_LOGE("%{public}s: failed to get mpp buffer group ret %{public}d", __func__, ret);
244         return HDF_FAILURE;
245     }
246 
247     ret = mppApi->HdiMppBufferGetWithTag(pBaseComponent->frmGrp, &pBaseComponent->frmBuf,
248         pBaseComponent->frameSize + pBaseComponent->headerSize, MODULE_TAG, __func__);
249     if (ret != MPP_OK) {
250         HDF_LOGE("%{public}s: failed to get buffer for input frame ret %{public}d", __func__, ret);
251         return HDF_FAILURE;
252     }
253 
254     ret = mppApi->HdiMppBufferGetWithTag(pBaseComponent->frmGrp, &pBaseComponent->pktBuf,
255         pBaseComponent->frameSize, MODULE_TAG, __func__);
256     if (ret != MPP_OK) {
257         HDF_LOGE("%{public}s: failed to get buffer for output packet ret %{public}d", __func__, ret);
258         return HDF_FAILURE;
259     }
260 
261     return HDF_SUCCESS;
262 }
263 
SetParamWidth(RKHdiBaseComponent * pBaseComponent,Param * param)264 int32_t SetParamWidth(RKHdiBaseComponent *pBaseComponent, Param *param)
265 {
266     MPP_RET ret = MPP_OK;
267     RKMppApi *mppApi = pBaseComponent->mppApi;
268     RK_S32 *width = (RK_S32 *)param->val;
269     if (*width <= 0) {
270         HDF_LOGE("%{public}s: invalid width!", __func__);
271         return HDF_ERR_INVALID_PARAM;
272     }
273     ret = mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "prep:width", *width);
274     if (ret != MPP_OK) {
275         HDF_LOGE("%{public}s: config mpp set width failed!", __func__);
276         return HDF_FAILURE;
277     }
278     pBaseComponent->setup.width = *width;
279     if (pBaseComponent->setup.stride.horStride == 0 && pBaseComponent->setup.fmt != PIXEL_FORMAT_NONE) {
280         pBaseComponent->setup.stride.horStride = GetDefaultHorStride(*width, pBaseComponent->setup.fmt);
281         mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "prep:hor_stride", pBaseComponent->setup.stride.horStride);
282     }
283     return HDF_SUCCESS;
284 }
285 
SetParamHeight(RKHdiBaseComponent * pBaseComponent,Param * param)286 int32_t SetParamHeight(RKHdiBaseComponent *pBaseComponent, Param *param)
287 {
288     MPP_RET ret = MPP_OK;
289     RKMppApi *mppApi = pBaseComponent->mppApi;
290     RK_S32 *height = (RK_S32 *)param->val;
291     if (*height <= 0) {
292         HDF_LOGE("%{public}s: invalid height!", __func__);
293         return HDF_ERR_INVALID_PARAM;
294     }
295     ret = mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "prep:height", *height);
296     if (ret != MPP_OK) {
297         HDF_LOGE("%{public}s: config mpp set height failed!", __func__);
298         return HDF_FAILURE;
299     }
300     pBaseComponent->setup.height = *height;
301     if (pBaseComponent->setup.stride.verStride == 0) {
302         pBaseComponent->setup.stride.verStride = *height;
303         mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "prep:ver_stride", pBaseComponent->setup.stride.verStride);
304     }
305     return HDF_SUCCESS;
306 }
307 
SetParamPixleFmt(RKHdiBaseComponent * pBaseComponent,Param * param)308 int32_t SetParamPixleFmt(RKHdiBaseComponent *pBaseComponent, Param *param)
309 {
310     MPP_RET ret = MPP_OK;
311     RKMppApi *mppApi = pBaseComponent->mppApi;
312     RK_S32 *pixFmt = (RK_S32 *)param->val;
313     MppFrameFormat rkPixFmt = ConvertHdiFormat2RKFormat(*pixFmt);
314     ret = mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "prep:format", rkPixFmt);
315     if (ret != MPP_OK) {
316         HDF_LOGE("%{public}s: config mpp set pixFmt failed!", __func__);
317         return HDF_FAILURE;
318     }
319     pBaseComponent->setup.fmt = *pixFmt;
320     pBaseComponent->fmt = rkPixFmt;
321     if (pBaseComponent->setup.stride.horStride == 0 && pBaseComponent->setup.width != 0) {
322         pBaseComponent->setup.stride.horStride = GetDefaultHorStride(pBaseComponent->setup.width,
323             pBaseComponent->setup.fmt);
324         mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "prep:hor_stride", pBaseComponent->setup.stride.horStride);
325     }
326     return HDF_SUCCESS;
327 }
328 
SetParamStride(RKHdiBaseComponent * pBaseComponent,Param * param)329 int32_t SetParamStride(RKHdiBaseComponent *pBaseComponent, Param *param)
330 {
331     RKMppApi *mppApi = pBaseComponent->mppApi;
332     RKHdiStrideSetup *strideSet = (RKHdiStrideSetup *)param->val;
333     if (strideSet->horStride <= 0 || strideSet->verStride <= 0) {
334         HDF_LOGE("%{public}s: invalid stride value!", __func__);
335         return HDF_ERR_INVALID_PARAM;
336     }
337     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "prep:hor_stride", strideSet->horStride);
338     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "prep:ver_stride", strideSet->verStride);
339     pBaseComponent->setup.stride.horStride = strideSet->horStride;
340     pBaseComponent->setup.stride.verStride = strideSet->verStride;
341     return HDF_SUCCESS;
342 }
343 
SetParamFps(RKHdiBaseComponent * pBaseComponent,Param * param)344 int32_t SetParamFps(RKHdiBaseComponent *pBaseComponent, Param *param)
345 {
346     RKMppApi *mppApi = pBaseComponent->mppApi;
347     RKHdiFpsSetup *rkFpsSet = (RKHdiFpsSetup *)param->val;
348     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:fps_in_flex", rkFpsSet->fpsInFlex);
349     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:fps_in_num", rkFpsSet->fpsInNum);
350     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:fps_in_denorm", rkFpsSet->fpsInDen);
351     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:fps_out_flex", rkFpsSet->fpsOutFlex);
352     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:fps_out_num", rkFpsSet->fpsOutNum);
353     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:fps_out_denorm", rkFpsSet->fpsOutDen);
354     pBaseComponent->setup.fps = *rkFpsSet;
355     return HDF_SUCCESS;
356 }
357 
SetParamDrop(RKHdiBaseComponent * pBaseComponent,Param * param)358 int32_t SetParamDrop(RKHdiBaseComponent *pBaseComponent, Param *param)
359 {
360     RKMppApi *mppApi = pBaseComponent->mppApi;
361     RKHdiDropSetup *rkDropSet = (RKHdiDropSetup *)param->val;
362     mppApi->HdiMppEncCfgSetU32(pBaseComponent->cfg, "rc:drop_mode", rkDropSet->dropMode);
363     mppApi->HdiMppEncCfgSetU32(pBaseComponent->cfg, "rc:drop_thd", rkDropSet->dropThd);
364     mppApi->HdiMppEncCfgSetU32(pBaseComponent->cfg, "rc:drop_gap", rkDropSet->dropGap);
365     pBaseComponent->setup.drop = *rkDropSet;
366     return HDF_SUCCESS;
367 }
368 
SetParamRateControl(RKHdiBaseComponent * pBaseComponent,Param * param)369 int32_t SetParamRateControl(RKHdiBaseComponent *pBaseComponent, Param *param)
370 {
371     RKMppApi *mppApi = pBaseComponent->mppApi;
372     RKHdiRcSetup *rateControlSet = (RKHdiRcSetup *)param->val;
373     MppEncRcMode rkRcMode = ConvertHdiRcMode2RKRcMode(rateControlSet->rcMode);
374 
375     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:mode", rkRcMode);
376     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:bps_target", rateControlSet->bpsTarget);
377 
378     if (rateControlSet->rcMode != MPP_ENC_RC_MODE_FIXQP) {
379         mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:bps_max", rateControlSet->bpsMax);
380         mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:bps_min", rateControlSet->bpsMin);
381     }
382 
383     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:qp_init", rateControlSet->qpInit);
384     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:qp_max", rateControlSet->qpMax);
385     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:qp_min", rateControlSet->qpMin);
386     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:qp_max_i", rateControlSet->qpMaxI);
387     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:qp_min_i", rateControlSet->qpMinI);
388     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:qp_ip", rateControlSet->qpIp);
389     pBaseComponent->setup.rc = *rateControlSet;
390     return HDF_SUCCESS;
391 }
392 
SetParamGop(RKHdiBaseComponent * pBaseComponent,Param * param)393 int32_t SetParamGop(RKHdiBaseComponent *pBaseComponent, Param *param)
394 {
395     MPP_RET ret = MPP_OK;
396     RKMppApi *mppApi = pBaseComponent->mppApi;
397     RKHdiGopSetup *gopSet = (RKHdiGopSetup *)param->val;
398 
399     ret = mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:gop", gopSet->gop);
400     if (ret != MPP_OK) {
401         HDF_LOGE("%{public}s: config mpp set gop failed!", __func__);
402         return HDF_FAILURE;
403     }
404 
405     RK_U32 gopMode = 0;
406     RK_U32 defaultGopMode = ConvertHdiGopMode2RKGopMode(gopSet->gopMode);
407     mppApi->HdiMppEnvGetU32("gop_mode", &gopMode, defaultGopMode);
408     if (gopMode) {
409         MppEncRefCfg ref = NULL;
410         mppApi->HdiMppEncRefCfgInit(&ref);
411         if (gopMode < GOP_MODE_THRESHOLD_VALUE) {
412             mppApi->HdiMppEncGenRefCfg(ref, gopMode);
413         } else {
414             mppApi->HdiMppEncGenSmartGopRefCfg(ref, gopSet->gopLen, gopSet->viLen);
415         }
416         ret = pBaseComponent->mpi->control(pBaseComponent->ctx, MPP_ENC_SET_REF_CFG, ref);
417         if (ret != MPP_OK) {
418             HDF_LOGE("%{public}s: mpi control enc set ref cfg failed, ret %{public}d", __func__, ret);
419             return HDF_FAILURE;
420         }
421         mppApi->HdiMppEncRefCfgDeinit(&ref);
422     }
423     pBaseComponent->setup.gop = *gopSet;
424     return HDF_SUCCESS;
425 }
426 
SetParamMimeCodecType(RKHdiBaseComponent * pBaseComponent,Param * param)427 int32_t SetParamMimeCodecType(RKHdiBaseComponent *pBaseComponent, Param *param)
428 {
429     RKMppApi *mppApi = pBaseComponent->mppApi;
430     RKHdiCodecMimeSetup *codecTypeSet = (RKHdiCodecMimeSetup *)param->val;
431     pBaseComponent->codingType = ConvertHdiMimeCodecType2RKCodecType(codecTypeSet->mimeCodecType);
432 
433     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "codec:type", pBaseComponent->codingType);
434     if (pBaseComponent->codingType == MPP_VIDEO_CodingAVC) {
435         mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "h264:profile", codecTypeSet->avcSetup.profile);
436         mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "h264:level", codecTypeSet->avcSetup.level);
437         mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "h264:cabac_en", codecTypeSet->avcSetup.cabacEn);
438         mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "h264:cabac_idc", codecTypeSet->avcSetup.cabacIdc);
439         mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "h264:trans8x8", codecTypeSet->avcSetup.trans8x8);
440     }
441     pBaseComponent->setup.codecMime = *codecTypeSet;
442     return HDF_SUCCESS;
443 }
444 
SetParamCodecType(RKHdiBaseComponent * pBaseComponent,Param * param)445 int32_t SetParamCodecType(RKHdiBaseComponent *pBaseComponent, Param *param)
446 {
447     RK_S32 *codecType = (RK_S32 *)param->val;
448     if (*codecType < 0 || *codecType >= INVALID_TYPE) {
449         HDF_LOGE("%{public}s: invalid codecType:%{public}d", __func__, *codecType);
450         return HDF_ERR_INVALID_PARAM;
451     }
452     pBaseComponent->setup.codecType = *codecType;
453     return HDF_SUCCESS;
454 }
455 
SetParamSplitParse(RKHdiBaseComponent * pBaseComponent,Param * param)456 int32_t SetParamSplitParse(RKHdiBaseComponent *pBaseComponent, Param *param)
457 {
458     MPP_RET ret = MPP_OK;
459     MppCtx ctx = pBaseComponent->ctx;
460     RK_U32 needSplit = 1;
461     int32_t result = memcpy_s(&needSplit, sizeof(RK_U32), param->val, param->size);
462     if (result != EOK) {
463         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
464         return HDF_FAILURE;
465     }
466 
467     ret = pBaseComponent->mppApi->HdiMppDecCfgSetU32(pBaseComponent->cfg, "base:split_parse", needSplit);
468     if (ret != MPP_OK) {
469         HDF_LOGE("%{public}s: failed to set split_parse ret %{public}d", __func__, ret);
470         return HDF_FAILURE;
471     }
472 
473     ret = pBaseComponent->mpi->control(ctx, MPP_DEC_SET_CFG, pBaseComponent->cfg);
474     if (ret != MPP_OK) {
475         HDF_LOGE("%{public}s: config MPP_DEC_SET_CFG failed, ret %{public}d", __func__, ret);
476         UINTPTR userData = NULL;
477         EventType event = EVENT_ERROR;
478         uint32_t length = 0;
479         int32_t *eventData = NULL;
480 
481         pBaseComponent->pCallbacks->OnEvent(userData, event, length, eventData);
482         return HDF_FAILURE;
483     }
484 
485     pBaseComponent->setup.split = needSplit;
486     return HDF_SUCCESS;
487 }
488 
SetParamCodecFrameNum(RKHdiBaseComponent * pBaseComponent,Param * param)489 int32_t SetParamCodecFrameNum(RKHdiBaseComponent *pBaseComponent, Param *param)
490 {
491     RK_S32 num = 0;
492     int32_t ret = memcpy_s(&num, sizeof(RK_S32), param->val, param->size);
493     if (ret != EOK) {
494         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
495         return HDF_FAILURE;
496     }
497     pBaseComponent->frameNum = num;
498     HDF_LOGI("%{public}s: set frame number: %{public}d", __func__, pBaseComponent->frameNum);
499     return HDF_SUCCESS;
500 }
501 
CheckSetup(RKHdiBaseComponent * pBaseComponent)502 int32_t CheckSetup(RKHdiBaseComponent *pBaseComponent)
503 {
504     RKMppApi *mppApi = pBaseComponent->mppApi;
505     if (pBaseComponent->setup.width <= 0 || pBaseComponent->setup.height <= 0) {
506         HDF_LOGE("%{public}s: width or height invalid %{public}d", __func__);
507         return HDF_ERR_INVALID_PARAM;
508     }
509     if (pBaseComponent->setup.stride.horStride == 0) {
510         pBaseComponent->setup.stride.horStride = MPP_ALIGN(pBaseComponent->setup.width, MPP_ALIGN_STRIDE_WITH_SIXTEEN);
511         mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "prep:hor_stride", pBaseComponent->setup.stride.horStride);
512     }
513     if (pBaseComponent->setup.stride.horStride == 0) {
514         pBaseComponent->setup.stride.verStride = MPP_ALIGN(pBaseComponent->setup.height,
515             MPP_ALIGN_STRIDE_WITH_SIXTEEN);
516         mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "prep:ver_stride", pBaseComponent->setup.stride.verStride);
517     }
518     return HDF_SUCCESS;
519 }
520 
ValidateEncSetup(RKHdiBaseComponent * pBaseComponent,Param * param)521 int32_t ValidateEncSetup(RKHdiBaseComponent *pBaseComponent, Param *param)
522 {
523     MPP_RET ret = MPP_OK;
524     RKMppApi *mppApi = pBaseComponent->mppApi;
525     RK_U32 splitMode = 0;
526     RK_U32 splitArg = 0;
527 
528     int32_t checkResult = CheckSetup(pBaseComponent);
529     if (checkResult != HDF_SUCCESS) {
530         HDF_LOGE("%{public}s: CheckSetup failed!", __func__);
531         return checkResult;
532     }
533 
534     mppApi->HdiMppEnvGetU32("split_mode", &splitMode, MPP_ENC_SPLIT_NONE);
535     mppApi->HdiMppEnvGetU32("split_arg", &splitArg, 0);
536     if (splitMode) {
537         HDF_LOGI("%{public}s: %{public}p splitMode %{public}d splitArg %{public}d",
538             __func__, pBaseComponent->ctx, splitMode, splitArg);
539         mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "split:mode", splitMode);
540         mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "split:arg", splitArg);
541     }
542 
543     ret = pBaseComponent->mpi->control(pBaseComponent->ctx, MPP_ENC_SET_CFG, pBaseComponent->cfg);
544     if (ret != MPP_OK) {
545         HDF_LOGE("%{public}s: mpi control enc set cfg failed ret %{public}d", __func__, ret);
546         return HDF_FAILURE;
547     }
548 
549     /* optional */
550     MppEncSeiMode seiMode = MPP_ENC_SEI_MODE_ONE_FRAME;
551     ret = pBaseComponent->mpi->control(pBaseComponent->ctx, MPP_ENC_SET_SEI_CFG, &seiMode);
552     if (ret != MPP_OK) {
553         HDF_LOGE("%{public}s: mpi control enc set sei cfg failed ret %{public}d", __func__, ret);
554         return HDF_FAILURE;
555     }
556 
557     if ((pBaseComponent->codingType == MPP_VIDEO_CodingAVC) || (pBaseComponent->codingType == MPP_VIDEO_CodingHEVC)) {
558         MppEncHeaderMode headerMode = MPP_ENC_HEADER_MODE_EACH_IDR;
559         ret = pBaseComponent->mpi->control(pBaseComponent->ctx, MPP_ENC_SET_HEADER_MODE, &headerMode);
560         if (ret != MPP_OK) {
561             HDF_LOGE("%{public}s: mpi control enc set header mode failed ret %{public}d", __func__, ret);
562             return HDF_FAILURE;
563         }
564     }
565 
566     pBaseComponent->frameSize = GetFrameBufferSize(pBaseComponent);
567     pBaseComponent->headerSize = GetFrameHeaderSize(pBaseComponent);
568     ret = GetMppBuffers(pBaseComponent);
569     if (ret != MPP_OK) {
570         HDF_LOGE("%{public}s: GetMppBuffers failed ret %{public}d", __func__, ret);
571         return HDF_FAILURE;
572     }
573 
574     return HDF_SUCCESS;
575 }
576 
SetParamEncSetupAVC(RKHdiBaseComponent * pBaseComponent,Param * param)577 int32_t SetParamEncSetupAVC(RKHdiBaseComponent *pBaseComponent, Param *param)
578 {
579     RKMppApi *mppApi = pBaseComponent->mppApi;
580 
581     RKHdiEncSetupAVC *encSetupAVC = (RKHdiEncSetupAVC *)param->val;
582     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "h264:profile", encSetupAVC->profile);
583     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "h264:level", encSetupAVC->level);
584     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "h264:cabac_en", encSetupAVC->cabacEn);
585     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "h264:cabac_idc", encSetupAVC->cabacIdc);
586     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "h264:trans8x8", encSetupAVC->trans8x8);
587 
588     HDF_LOGI("%{public}s: set AVC encode profile:%{public}d, level:%{public}d",
589         __func__, encSetupAVC->profile, encSetupAVC->level);
590     return HDF_SUCCESS;
591 }
592 
GetParamBufferSize(RKHdiBaseComponent * pBaseComponent,Param * param)593 int32_t GetParamBufferSize(RKHdiBaseComponent *pBaseComponent, Param *param)
594 {
595     if (param->val == NULL) {
596         param->size = sizeof(RK_U32);
597         param->val = malloc(param->size);
598     }
599 
600     RKMppApi *mppApi = pBaseComponent->mppApi;
601     RK_U32 bufSize = 0;
602     if (pBaseComponent->frame) {
603         bufSize = mppApi->HdiMppFrameGetBufferSize(pBaseComponent->frame);
604     }
605 
606     int32_t ret = memcpy_s(param->val, param->size, &bufSize, sizeof(RK_U32));
607     if (ret != EOK) {
608         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
609         return HDF_FAILURE;
610     }
611     return HDF_SUCCESS;
612 }
613 
GetParamWidth(RKHdiBaseComponent * pBaseComponent,Param * param)614 int32_t GetParamWidth(RKHdiBaseComponent *pBaseComponent, Param *param)
615 {
616     if (param->val == NULL) {
617         param->size = sizeof(RK_S32);
618         param->val = malloc(param->size);
619     }
620     int32_t ret = memcpy_s(param->val, param->size, &pBaseComponent->setup.width, sizeof(RK_S32));
621     if (ret != EOK) {
622         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
623         return HDF_FAILURE;
624     }
625     return HDF_SUCCESS;
626 }
627 
GetParamHeight(RKHdiBaseComponent * pBaseComponent,Param * param)628 int32_t GetParamHeight(RKHdiBaseComponent *pBaseComponent, Param *param)
629 {
630     if (param->val == NULL) {
631         param->size = sizeof(RK_S32);
632         param->val = malloc(param->size);
633     }
634     int32_t ret = memcpy_s(param->val, param->size, &pBaseComponent->setup.height, sizeof(RK_S32));
635     if (ret != EOK) {
636         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
637         return HDF_FAILURE;
638     }
639     return HDF_SUCCESS;
640 }
641 
GetParamDecOutputPixelFmt(RKHdiBaseComponent * pBaseComponent,Param * param)642 int32_t GetParamDecOutputPixelFmt(RKHdiBaseComponent *pBaseComponent, Param *param)
643 {
644     if (param->val == NULL) {
645         param->size = sizeof(CodecPixelFormat);
646         param->val = malloc(param->size);
647     }
648 
649     RKMppApi *mppApi = pBaseComponent->mppApi;
650     MppFrameFormat fmtRK  = MPP_FMT_YUV420SP;
651     CodecPixelFormat format = PIXEL_FORMAT_YCBCR_420_SP;
652     if (pBaseComponent->frame) {
653         fmtRK = mppApi->HdiMppFrameGetFormat(pBaseComponent->frame);
654         format = ConvertRKFormat2HdiFormat(fmtRK);
655     }
656     // Rk mpp Only NV12 is supported
657     int32_t ret = memcpy_s(param->val, param->size, &format, sizeof(CodecPixelFormat));
658     if (ret != EOK) {
659         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
660         return HDF_FAILURE;
661     }
662     return HDF_SUCCESS;
663 }
664 
GetParamEncInputPixleFmt(RKHdiBaseComponent * pBaseComponent,Param * param)665 int32_t GetParamEncInputPixleFmt(RKHdiBaseComponent *pBaseComponent, Param *param)
666 {
667     if (param->val == NULL) {
668         param->size = sizeof(RK_S32);
669         param->val = malloc(param->size);
670     }
671     int32_t ret = memcpy_s(param->val, param->size, &pBaseComponent->setup.fmt, sizeof(RK_S32));
672     if (ret != EOK) {
673         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
674         return HDF_FAILURE;
675     }
676     return HDF_SUCCESS;
677 }
678 
GetParamPixleFmt(RKHdiBaseComponent * pBaseComponent,Param * param)679 int32_t GetParamPixleFmt(RKHdiBaseComponent *pBaseComponent, Param *param)
680 {
681     if (pBaseComponent->setup.codecType == VIDEO_DECODER) {
682         HDF_LOGI("%{public}s: codec type VIDEO_DECODER!", __func__);
683         return GetParamDecOutputPixelFmt(pBaseComponent, param);
684     } else if (pBaseComponent->setup.codecType == VIDEO_ENCODER) {
685         HDF_LOGI("%{public}s: codec type VIDEO_ENCODER!", __func__);
686         return GetParamEncInputPixleFmt(pBaseComponent, param);
687     } else {
688         HDF_LOGE("%{public}s: codec type invalid!", __func__);
689         return HDF_FAILURE;
690     }
691 }
692 
GetParamStride(RKHdiBaseComponent * pBaseComponent,Param * param)693 int32_t GetParamStride(RKHdiBaseComponent *pBaseComponent, Param *param)
694 {
695     if (param->val == NULL) {
696         param->size = sizeof(RKHdiStrideSetup);
697         param->val = malloc(param->size);
698     }
699     int32_t ret = memcpy_s(param->val, param->size, &pBaseComponent->setup.stride, sizeof(RKHdiStrideSetup));
700     if (ret != EOK) {
701         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
702         return HDF_FAILURE;
703     }
704     return HDF_SUCCESS;
705 }
706 
GetParamFps(RKHdiBaseComponent * pBaseComponent,Param * param)707 int32_t GetParamFps(RKHdiBaseComponent *pBaseComponent, Param *param)
708 {
709     if (param->val == NULL) {
710         param->size = sizeof(RKHdiFpsSetup);
711         param->val = malloc(param->size);
712     }
713     int32_t ret = memcpy_s(param->val, param->size, &pBaseComponent->setup.fps, sizeof(RKHdiFpsSetup));
714     if (ret != EOK) {
715         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
716         return HDF_FAILURE;
717     }
718     return HDF_SUCCESS;
719 }
720 
GetParamRateControl(RKHdiBaseComponent * pBaseComponent,Param * param)721 int32_t GetParamRateControl(RKHdiBaseComponent *pBaseComponent, Param *param)
722 {
723     if (param->val == NULL) {
724         param->size = sizeof(RKHdiRcSetup);
725         param->val = malloc(param->size);
726     }
727     int32_t ret = memcpy_s(param->val, param->size, &pBaseComponent->setup.rc, sizeof(RKHdiRcSetup));
728     if (ret != EOK) {
729         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
730         return HDF_FAILURE;
731     }
732     return HDF_SUCCESS;
733 }
734 
GetParamGop(RKHdiBaseComponent * pBaseComponent,Param * param)735 int32_t GetParamGop(RKHdiBaseComponent *pBaseComponent, Param *param)
736 {
737     if (param->val == NULL) {
738         param->size = sizeof(RKHdiGopSetup);
739         param->val = malloc(param->size);
740     }
741     int32_t ret = memcpy_s(param->val, param->size, &pBaseComponent->setup.gop, sizeof(RKHdiGopSetup));
742     if (ret != EOK) {
743         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
744         return HDF_FAILURE;
745     }
746     return HDF_SUCCESS;
747 }
748 
GetParamMimeCodecType(RKHdiBaseComponent * pBaseComponent,Param * param)749 int32_t GetParamMimeCodecType(RKHdiBaseComponent *pBaseComponent, Param *param)
750 {
751     if (param->val == NULL) {
752         param->size = sizeof(RKHdiCodecMimeSetup);
753         param->val = malloc(param->size);
754     }
755     int32_t ret = memcpy_s(param->val, param->size, &pBaseComponent->setup.codecMime, sizeof(RKHdiCodecMimeSetup));
756     if (ret != EOK) {
757         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
758         return HDF_FAILURE;
759     }
760     return HDF_SUCCESS;
761 }
762 
GetParamCodecType(RKHdiBaseComponent * pBaseComponent,Param * param)763 int32_t GetParamCodecType(RKHdiBaseComponent *pBaseComponent, Param *param)
764 {
765     if (param->val == NULL) {
766         param->size = sizeof(RK_S32);
767         param->val = malloc(param->size);
768     }
769     int32_t ret = memcpy_s(param->val, param->size, &pBaseComponent->setup.codecType, sizeof(RK_S32));
770     if (ret != EOK) {
771         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
772         return HDF_FAILURE;
773     }
774     return HDF_SUCCESS;
775 }
776 
GetParamSplitParse(RKHdiBaseComponent * pBaseComponent,Param * param)777 int32_t GetParamSplitParse(RKHdiBaseComponent *pBaseComponent, Param *param)
778 {
779     if (param->val == NULL) {
780         param->size = sizeof(RK_U32);
781         param->val = malloc(param->size);
782     }
783     int32_t ret = memcpy_s(param->val, param->size, &pBaseComponent->setup.split, sizeof(RK_U32));
784     if (ret != EOK) {
785         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
786         return HDF_FAILURE;
787     }
788     return HDF_SUCCESS;
789 }
790 
GetParamCodecFrameNum(RKHdiBaseComponent * pBaseComponent,Param * param)791 int32_t GetParamCodecFrameNum(RKHdiBaseComponent *pBaseComponent, Param *param)
792 {
793     if (param->val == NULL) {
794         param->size = sizeof(RK_S32);
795         param->val = malloc(param->size);
796     }
797     int32_t ret = memcpy_s(param->val, param->size, &pBaseComponent->frameNum, sizeof(RK_S32));
798     if (ret != EOK) {
799         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
800         return HDF_FAILURE;
801     }
802     return HDF_SUCCESS;
803 }
804 
GetParamDrop(RKHdiBaseComponent * pBaseComponent,Param * param)805 int32_t GetParamDrop(RKHdiBaseComponent *pBaseComponent, Param *param)
806 {
807     if (param->val == NULL) {
808         param->size = sizeof(RKHdiDropSetup);
809         param->val = malloc(param->size);
810     }
811     int32_t ret = memcpy_s(param->val, param->size, &pBaseComponent->setup.drop, sizeof(RKHdiDropSetup));
812     if (ret != EOK) {
813         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
814         return HDF_FAILURE;
815     }
816     return HDF_SUCCESS;
817 }
818