1 /*
2  * Copyright (c) 2022-2023 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 <hdf_base.h>
18 #include <hdf_log.h>
19 #include <securec.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 
35 #define BPS_BASE                            16
36 #define BPS_MAX                             17
37 #define BPS_MEDIUM                          15
38 #define BPS_MIN                             1
39 #define BPS_TARGET                          2
40 #define FIXQP_INIT_VALUE                    20
41 #define FIXQP_MAX_VALUE                     20
42 #define FIXQP_MIN_VALUE                     20
43 #define FIXQP_MAX_I_VALUE                   20
44 #define FIXQP_MIN_I_VALUE                   20
45 #define FIXQP_IP_VALUE                      2
46 #define OTHER_QP_INIT_VALUE                 26
47 #define OTHER_QP_MAX_VALUE                  51
48 #define OTHER_QP_MIN_VALUE                  10
49 #define OTHER_QP_MAX_I_VALUE                51
50 #define OTHER_QP_MIN_I_VALUE                10
51 #define OTHER_QP_IP_VALUE                   2
52 #define AVC_SETUP_LEVEL_DEFAULT             40
53 #define AVC_SETUP_CABAC_EN_DEFAULT          1
54 #define AVC_SETUP_CABAC_IDC_DEFAULT         0
55 #define AVC_SETUP_TRANS_DEFAULT             1
56 #define AVC_SETUP_PROFILE_DEFAULT           100
57 #define FPS_SETUP_DEFAULT                   24
58 #define DFAULT_ENC_DROP_THD                 20
59 #define DFAULT_ENC_GOP_OPERATOR             2
60 #define DFAULT_BUF_SIZE_OPERATOR            2
61 #define FOUR_BYTE_PIX_BUF_SIZE_OPERATOR     4
62 #define PACKET_SIZE_MULTI                   3
63 #define PACKET_SIZE_OPERATOR                2
64 #define DEFAULT_RESOLUTION_SIZE             (1920 * 1088)
65 #define DEFAULT_PACKET_BUFFER_NUM           4
66 #define DEFAULT_FRAME_BUFFER_NUM            2
67 
68 #define DEFAULT_REF_FRAME_NUM               8
69 #define LEVEL_5P1_MAX_DPB_MBS               184320
70 #define REF_FRAME_DIVISOR                   16
71 #define MAX_REF_FRAME_NUM                   16
72 #define MIN_REF_FRAME_NUM                   6
73 #define WIDTH_OF_4K_VIDEO                   4096
74 #define HEIGHT_OF_4K_HEVC                   2160
75 #define HEIGHT_OF_4K_VP9                    2176
76 #define MAX_REF_FRAME_NUM_VP9               8
77 #define MIN_REF_FRAME_NUM_VP9               4
78 #define BASIC_DEC_FRAME_BUFFER_NUM          5
79 
80 static const CodecPixelFormatConvertTbl pixelFormatSupportted[] = {
81     {PIXEL_FMT_RGBA_8888, MPP_FMT_RGBA8888, RK_FORMAT_RGBA_8888},
82     {PIXEL_FMT_BGRA_8888, MPP_FMT_BGRA8888, RK_FORMAT_BGRA_8888},
83     {PIXEL_FMT_YCBCR_420_SP, MPP_FMT_YUV420SP, RK_FORMAT_YCbCr_420_SP },
84     {PIXEL_FMT_YCBCR_420_P, MPP_FMT_YUV420P, RK_FORMAT_YCbCr_420_P},
85 };
86 
InitMppConfig(RKHdiBaseComponent *pBaseComponent)87 int32_t InitMppConfig(RKHdiBaseComponent *pBaseComponent)
88 {
89     int32_t ret = 0;
90     if (pBaseComponent->ctxType == MPP_CTX_ENC) {
91         ret = pBaseComponent->mppApi->HdiMppEncCfgInit(&pBaseComponent->cfg);
92     } else if (pBaseComponent->ctxType == MPP_CTX_DEC) {
93         ret = pBaseComponent->mppApi->HdiMppDecCfgInit(&pBaseComponent->cfg);
94     }
95     return ret;
96 }
97 
DeinitMppConfig(RKHdiBaseComponent *pBaseComponent)98 int32_t DeinitMppConfig(RKHdiBaseComponent *pBaseComponent)
99 {
100     int32_t ret = 0;
101     if (pBaseComponent->cfg == NULL) {
102         return ret;
103     }
104 
105     if (pBaseComponent->ctxType == MPP_CTX_ENC) {
106         ret = pBaseComponent->mppApi->HdiMppEncCfgDeinit(pBaseComponent->cfg);
107     } else if (pBaseComponent->ctxType == MPP_CTX_DEC) {
108         ret = pBaseComponent->mppApi->HdiMppDecCfgDeinit(pBaseComponent->cfg);
109     }
110     pBaseComponent->cfg = NULL;
111     return ret;
112 }
113 
SetDefaultFps(RKHdiEncodeSetup *setup)114 void SetDefaultFps(RKHdiEncodeSetup *setup)
115 {
116     setup->fps.fpsInFlex = 0;
117     setup->fps.fpsInNum = FPS_SETUP_DEFAULT;
118     setup->fps.fpsOutNum = FPS_SETUP_DEFAULT;
119     setup->fps.fpsInDen = 1;
120     setup->fps.fpsOutDen = 1;
121     setup->fps.fpsOutFlex = 0;
122 }
123 
SetDefaultDropMode(RKHdiEncodeSetup *setup)124 void SetDefaultDropMode(RKHdiEncodeSetup *setup)
125 {
126     setup->drop.dropMode = MPP_ENC_RC_DROP_FRM_DISABLED;
127     setup->drop.dropThd = DFAULT_ENC_DROP_THD;      /* 20% of max bps */
128     setup->drop.dropGap = 1;                        /* Do not continuous drop frame */
129 }
130 
SetDefaultGopMode(RKHdiEncodeSetup *setup)131 void SetDefaultGopMode(RKHdiEncodeSetup *setup)
132 {
133     setup->gop.gopMode = VID_CODEC_GOPMODE_INVALID;
134     setup->gop.gopLen = 0;
135     setup->gop.viLen = 0;
136     setup->gop.gop = setup->fps.fpsOutNum * DFAULT_ENC_GOP_OPERATOR;
137 }
138 
GetDefaultConfig(RKHdiBaseComponent *pBaseComponent)139 static int32_t GetDefaultConfig(RKHdiBaseComponent *pBaseComponent)
140 {
141     MPP_RET ret = MPP_OK;
142     MppCtx ctx = pBaseComponent->ctx;
143 
144     if (pBaseComponent->ctxType == MPP_CTX_ENC) {
145         ret = pBaseComponent->mpi->control(ctx, MPP_ENC_GET_CFG, pBaseComponent->cfg);
146         if (ret != MPP_OK) {
147             HDF_LOGE("%{public}s: config MPP_ENC_GET_CFG failed, ret %{public}d", __func__, ret);
148             return HDF_FAILURE;
149         }
150     } else if (pBaseComponent->ctxType == MPP_CTX_ENC) {
151         ret = pBaseComponent->mpi->control(ctx, MPP_DEC_GET_CFG, pBaseComponent->cfg);
152         if (ret != MPP_OK) {
153             HDF_LOGE("%{public}s: config MPP_DEC_GET_CFG failed, ret %{public}d", __func__, ret);
154             return HDF_FAILURE;
155         }
156     }
157 
158     return HDF_SUCCESS;
159 }
160 
ConvertRKFormat2HdiFormat(MppFrameFormat fmtRK)161 static PixelFormat ConvertRKFormat2HdiFormat(MppFrameFormat fmtRK)
162 {
163     if (fmtRK == MPP_FMT_YUV420SP) {
164         return PIXEL_FMT_YCBCR_420_SP;
165     } else {
166         return PIXEL_FMT_BUTT;
167     }
168 }
169 
ConvertHdiFormat2RKFormat(PixelFormat fmtHDI)170 MppFrameFormat ConvertHdiFormat2RKFormat(PixelFormat fmtHDI)
171 {
172     MppFrameFormat mppFmt = MPP_FMT_BUTT;
173     int32_t count = sizeof(pixelFormatSupportted) / sizeof(CodecPixelFormatConvertTbl);
174     for (int i = 0; i < count; i++) {
175         if (pixelFormatSupportted[i].pixFormat == fmtHDI) {
176             mppFmt = pixelFormatSupportted[i].mppFormat;
177             break;
178         }
179     }
180     if (mppFmt == MPP_FMT_BUTT) {
181         HDF_LOGE("%{public}s: do not support type %{public}d", __func__, fmtHDI);
182     }
183     return mppFmt;
184 }
185 
ConvertHdiFormat2RgaFormat(PixelFormat fmtHDI)186 RgaSURF_FORMAT ConvertHdiFormat2RgaFormat(PixelFormat fmtHDI)
187 {
188     RgaSURF_FORMAT rgaFmt = RK_FORMAT_UNKNOWN;
189     int32_t count = sizeof(pixelFormatSupportted) / sizeof(CodecPixelFormatConvertTbl);
190     for (int i = 0; i < count; i++) {
191         if (pixelFormatSupportted[i].pixFormat == fmtHDI) {
192             rgaFmt = pixelFormatSupportted[i].rgaFormat;
193             break;
194         }
195     }
196     if (rgaFmt == RK_FORMAT_UNKNOWN) {
197         HDF_LOGE("%{public}s: do not support type %{public}d", __func__, fmtHDI);
198     }
199     return rgaFmt;
200 }
201 
ConvertMppFormat2RgaFormat(MppFrameFormat mppFmt)202 RgaSURF_FORMAT ConvertMppFormat2RgaFormat(MppFrameFormat mppFmt)
203 {
204     RgaSURF_FORMAT rgaFmt = RK_FORMAT_UNKNOWN;
205     int32_t count = sizeof(pixelFormatSupportted) / sizeof(CodecPixelFormatConvertTbl);
206     for (int i = 0; i < count; i++) {
207         if (pixelFormatSupportted[i].mppFormat == mppFmt) {
208             rgaFmt = pixelFormatSupportted[i].rgaFormat;
209             break;
210         }
211     }
212     if (rgaFmt == RK_FORMAT_UNKNOWN) {
213         HDF_LOGE("%{public}s: do not support type %{public}d", __func__, mppFmt);
214     }
215     return rgaFmt;
216 }
217 
ConvertHdiRcMode2RKRcMode(VideoCodecRcMode hdiRcMode)218 static MppEncRcMode ConvertHdiRcMode2RKRcMode(VideoCodecRcMode hdiRcMode)
219 {
220     MppEncRcMode rkRcMode = MPP_ENC_RC_MODE_VBR;
221     switch (hdiRcMode) {
222         case VID_CODEC_RC_CBR:
223             rkRcMode = MPP_ENC_RC_MODE_CBR;
224             break;
225         case VID_CODEC_RC_VBR:
226             rkRcMode = MPP_ENC_RC_MODE_VBR;
227             break;
228         case VID_CODEC_RC_AVBR:
229             rkRcMode = MPP_ENC_RC_MODE_AVBR;
230             break;
231         case VID_CODEC_RC_FIXQP:
232             rkRcMode = MPP_ENC_RC_MODE_FIXQP;
233             break;
234         default:
235             break;
236     }
237     return rkRcMode;
238 }
239 
ConvertHdiGopMode2RKGopMode(VideoCodecGopMode hdiGopMode)240 static MppEncRcGopMode ConvertHdiGopMode2RKGopMode(VideoCodecGopMode hdiGopMode)
241 {
242     MppEncRcGopMode rkGopMode = MPP_ENC_RC_NORMAL_P;
243     switch (hdiGopMode) {
244         case VID_CODEC_GOPMODE_NORMALP:
245             rkGopMode = MPP_ENC_RC_NORMAL_P;
246             break;
247         case VID_CODEC_GOPMODE_SMARTP:
248             rkGopMode = MPP_ENC_RC_SMART_P;
249             break;
250         default:
251             break;
252     }
253     return rkGopMode;
254 }
255 
ConvertHdiMimeCodecType2RKCodecType(AvCodecMime mime)256 static MppCodingType ConvertHdiMimeCodecType2RKCodecType(AvCodecMime mime)
257 {
258     MppCodingType type = MPP_VIDEO_CodingMax;
259     switch (mime) {
260         case MEDIA_MIMETYPE_IMAGE_JPEG:
261             type = MPP_VIDEO_CodingMJPEG;
262             break;
263         case MEDIA_MIMETYPE_VIDEO_AVC:
264             type = MPP_VIDEO_CodingAVC;
265             break;
266         case MEDIA_MIMETYPE_VIDEO_HEVC:
267             type = MPP_VIDEO_CodingHEVC;
268             break;
269         default:
270             break;
271     }
272     if (type == MPP_VIDEO_CodingMax) {
273         HDF_LOGE("%{public}s: do not support type %{public}d", __func__, type);
274     }
275     return type;
276 }
277 
GetStrideByWidth(int32_t width, MppFrameFormat fmt)278 static int32_t GetStrideByWidth(int32_t width, MppFrameFormat fmt)
279 {
280     int32_t stride = 0;
281 
282     switch (fmt & MPP_FRAME_FMT_MASK) {
283         case MPP_FMT_YUV420SP:
284         case MPP_FMT_YUV420SP_VU:
285             stride = MPP_ALIGN(width, MPP_ALIGN_STRIDE_WITH_EIGHT);
286             break;
287         case MPP_FMT_YUV420P:
288             /* NOTE: 420P need to align to 16 so chroma can align to 8 */
289             stride = MPP_ALIGN(width, MPP_ALIGN_STRIDE_WITH_SIXTEEN);
290             break;
291         case MPP_FMT_YUV422P:
292         case MPP_FMT_YUV422SP:
293         case MPP_FMT_YUV422SP_VU:
294             /* NOTE: 422 need to align to 8 so chroma can align to 16 */
295             stride = MPP_ALIGN(width, MPP_ALIGN_STRIDE_WITH_EIGHT);
296             break;
297         case MPP_FMT_RGB565:
298         case MPP_FMT_BGR565:
299         case MPP_FMT_RGB555:
300         case MPP_FMT_BGR555:
301         case MPP_FMT_RGB444:
302         case MPP_FMT_BGR444:
303         case MPP_FMT_YUV422_YUYV:
304         case MPP_FMT_YUV422_YVYU:
305         case MPP_FMT_YUV422_UYVY:
306         case MPP_FMT_YUV422_VYUY:
307             /* NOTE: for vepu limitation */
308             stride = MPP_ALIGN(width, MPP_ALIGN_STRIDE_WITH_EIGHT) * MPP_ALIGN_MULTIPLE_WITH_TWO;
309             break;
310         case MPP_FMT_RGB888:
311         case MPP_FMT_BGR888:
312             /* NOTE: for vepu limitation */
313             stride = MPP_ALIGN(width, MPP_ALIGN_STRIDE_WITH_EIGHT) * MPP_ALIGN_MULTIPLE_WITH_THREE;
314             break;
315         case MPP_FMT_RGB101010:
316         case MPP_FMT_BGR101010:
317         case MPP_FMT_ARGB8888:
318         case MPP_FMT_ABGR8888:
319         case MPP_FMT_BGRA8888:
320         case MPP_FMT_RGBA8888:
321             /* NOTE: for vepu limitation */
322             stride = MPP_ALIGN(width, MPP_ALIGN_STRIDE_WITH_EIGHT) * MPP_ALIGN_MULTIPLE_WITH_FOUR;
323             break;
324         default :
325             break;
326     }
327 
328     return stride;
329 }
330 
GetDefaultHorStride(int32_t width, PixelFormat fmtHDI)331 int32_t GetDefaultHorStride(int32_t width, PixelFormat fmtHDI)
332 {
333     MppFrameFormat fmt = ConvertHdiFormat2RKFormat(fmtHDI);
334     return GetStrideByWidth(width, fmt);
335 }
336 
GetMppFrameBufferSize(RKHdiBaseComponent *pBaseComponent)337 static uint32_t GetMppFrameBufferSize(RKHdiBaseComponent *pBaseComponent)
338 {
339     uint32_t frameSize = 0;
340     switch (pBaseComponent->fmt) {
341         case MPP_FMT_YUV420SP:
342         case MPP_FMT_YUV420P:
343             frameSize = MPP_ALIGN(pBaseComponent->horStride, MPP_ALIGN_STRIDE_WITH_SIXTY_FOUR)
344                 * MPP_ALIGN(pBaseComponent->verStride, MPP_ALIGN_STRIDE_WITH_SIXTY_FOUR)
345                 * MPP_ALIGN_MULTIPLE_WITH_THREE / MPP_ALIGN_DIVISOR_WITH_TWO;
346             break;
347 
348         case MPP_FMT_YUV422_YUYV:
349         case MPP_FMT_YUV422_YVYU:
350         case MPP_FMT_YUV422_UYVY:
351         case MPP_FMT_YUV422_VYUY:
352         case MPP_FMT_YUV422P:
353         case MPP_FMT_YUV422SP:
354         case MPP_FMT_RGB444:
355         case MPP_FMT_BGR444:
356         case MPP_FMT_RGB555:
357         case MPP_FMT_BGR555:
358         case MPP_FMT_RGB565:
359         case MPP_FMT_BGR565:
360             frameSize = MPP_ALIGN(pBaseComponent->horStride, MPP_ALIGN_STRIDE_WITH_SIXTY_FOUR)
361                 * MPP_ALIGN(pBaseComponent->verStride, MPP_ALIGN_STRIDE_WITH_SIXTY_FOUR)
362                 * MPP_ALIGN_MULTIPLE_WITH_TWO;
363             break;
364 
365         default:
366             frameSize = MPP_ALIGN(pBaseComponent->horStride, MPP_ALIGN_STRIDE_WITH_SIXTY_FOUR)
367                 * MPP_ALIGN(pBaseComponent->verStride, MPP_ALIGN_STRIDE_WITH_SIXTY_FOUR)
368                 * MPP_ALIGN_MULTIPLE_WITH_FOUR;
369             break;
370     }
371     return frameSize;
372 }
373 
GetMppFrameHeaderSize(RKHdiBaseComponent *pBaseComponent)374 static uint32_t GetMppFrameHeaderSize(RKHdiBaseComponent *pBaseComponent)
375 {
376     uint32_t headerSize = 0;
377     if (MPP_FRAME_FMT_IS_FBC(pBaseComponent->fmt)) {
378         headerSize = MPP_ALIGN(MPP_ALIGN(pBaseComponent->setup.width, MPP_ALIGN_STRIDE_WITH_SIXTEEN)
379             * MPP_ALIGN(pBaseComponent->setup.height, MPP_ALIGN_STRIDE_WITH_SIXTEEN)
380             / MPP_ALIGN_DIVISOR_WITH_SIXTEEN, SZ_4K);
381     } else {
382         headerSize = 0;
383     }
384     return headerSize;
385 }
386 
GetMppBuffers(RKHdiBaseComponent *pBaseComponent)387 static int32_t GetMppBuffers(RKHdiBaseComponent *pBaseComponent)
388 {
389     MPP_RET ret = MPP_OK;
390     RKMppApi *mppApi = pBaseComponent->mppApi;
391     ret = mppApi->HdiMppBufferGroupGet(&pBaseComponent->frmGrp,
392         MPP_BUFFER_TYPE_DRM, MPP_BUFFER_INTERNAL, NULL, __func__);
393     if (ret != MPP_OK) {
394         HDF_LOGE("%{public}s: failed to get mpp buffer group ret %{public}d", __func__, ret);
395         return HDF_FAILURE;
396     }
397 
398     ret = mppApi->HdiMppBufferGetWithTag(pBaseComponent->frmGrp, &pBaseComponent->frmBuf,
399         pBaseComponent->frameSize + pBaseComponent->headerSize, MODULE_TAG, __func__);
400     if (ret != MPP_OK) {
401         HDF_LOGE("%{public}s: failed to get buffer for input frame ret %{public}d", __func__, ret);
402         return HDF_FAILURE;
403     }
404 
405     ret = mppApi->HdiMppBufferGetWithTag(pBaseComponent->frmGrp, &pBaseComponent->pktBuf,
406         pBaseComponent->frameSize, MODULE_TAG, __func__);
407     if (ret != MPP_OK) {
408         HDF_LOGE("%{public}s: failed to get buffer for output packet ret %{public}d", __func__, ret);
409         return HDF_FAILURE;
410     }
411 
412     return HDF_SUCCESS;
413 }
414 
SetQpValue(RKHdiEncodeSetup *encSetup, MppEncRcMode mppRcMode)415 static void SetQpValue(RKHdiEncodeSetup *encSetup, MppEncRcMode mppRcMode)
416 {
417     switch (mppRcMode) {
418         case MPP_ENC_RC_MODE_FIXQP: {
419             encSetup->rc.qpInit = FIXQP_INIT_VALUE;
420             encSetup->rc.qpMax = FIXQP_MAX_VALUE;
421             encSetup->rc.qpMin = FIXQP_MIN_VALUE;
422             encSetup->rc.qpMaxI = FIXQP_MAX_I_VALUE;
423             encSetup->rc.qpMinI = FIXQP_MIN_I_VALUE;
424             encSetup->rc.qpIp = FIXQP_IP_VALUE;
425             break;
426         }
427         case MPP_ENC_RC_MODE_CBR:
428         case MPP_ENC_RC_MODE_VBR:
429         case MPP_ENC_RC_MODE_AVBR: {
430             encSetup->rc.qpInit = OTHER_QP_INIT_VALUE;
431             encSetup->rc.qpMax = OTHER_QP_MAX_VALUE;
432             encSetup->rc.qpMin = OTHER_QP_MIN_VALUE;
433             encSetup->rc.qpMaxI = OTHER_QP_MAX_I_VALUE;
434             encSetup->rc.qpMinI = OTHER_QP_MIN_I_VALUE;
435             encSetup->rc.qpIp = OTHER_QP_IP_VALUE;
436             break;
437         }
438         default: {
439             HDF_LOGE("%{public}s: not handled or unsupported encoder rc mode %{public}d", __func__, mppRcMode);
440             break;
441         }
442     }
443 }
444 
CalcBpsValue(RKHdiBaseComponent *pBaseComponent, MppEncRcMode mppRcMode)445 static void CalcBpsValue(RKHdiBaseComponent *pBaseComponent, MppEncRcMode mppRcMode)
446 {
447     RKHdiEncodeSetup *encSetup = &pBaseComponent->setup;
448     encSetup->rc.bpsTarget = encSetup->width * encSetup->height * BPS_TARGET / BPS_BASE *
449         (encSetup->fps.fpsOutNum / encSetup->fps.fpsOutDen);
450 
451     switch (mppRcMode) {
452         case MPP_ENC_RC_MODE_FIXQP: {
453             /* do not setup bitrate on FIXQP mode */
454             break;
455         }
456         case MPP_ENC_RC_MODE_CBR: {
457             /* CBR mode has narrow bound */
458             encSetup->rc.bpsMax = encSetup->rc.bpsTarget * BPS_MAX / BPS_BASE;
459             encSetup->rc.bpsMin = encSetup->rc.bpsTarget * BPS_MEDIUM / BPS_BASE;
460             break;
461         }
462         case MPP_ENC_RC_MODE_VBR:
463         case MPP_ENC_RC_MODE_AVBR: {
464             /* VBR mode has wide bound */
465             encSetup->rc.bpsMax = encSetup->rc.bpsTarget * BPS_MAX / BPS_BASE;
466             encSetup->rc.bpsMin = encSetup->rc.bpsTarget * BPS_MIN / BPS_BASE;
467             break;
468         }
469         default: {
470             /* default use CBR mode */
471             encSetup->rc.bpsMax = encSetup->rc.bpsTarget * BPS_MAX / BPS_BASE;
472             encSetup->rc.bpsMin = encSetup->rc.bpsTarget * BPS_MEDIUM / BPS_BASE;
473             break;
474         }
475     }
476     /* setup qp for different codec and rc_mode */
477     switch (pBaseComponent->codingType) {
478         case MPP_VIDEO_CodingAVC:
479         case MPP_VIDEO_CodingHEVC: {
480             SetQpValue(encSetup, mppRcMode);
481             break;
482         }
483         default: {
484             break;
485         }
486     }
487 }
488 
SetMimeSettings(RKHdiCodecMimeSetup *codecMimeSet, MppCodingType codingType)489 static void SetMimeSettings(RKHdiCodecMimeSetup *codecMimeSet, MppCodingType codingType)
490 {
491     switch (codingType) {
492         case MPP_VIDEO_CodingAVC: {
493             /*
494             * H.264 profile_idc parameter
495             * 66  - Baseline profile
496             * 77  - Main profile
497             * 100 - High profile
498             */
499             codecMimeSet->avcSetup.profile = AVC_SETUP_PROFILE_DEFAULT;
500             /*
501             * H.264 level_idc parameter
502             * 10 / 11 / 12 / 13    - qcif@15fps / cif@7.5fps / cif@15fps / cif@30fps
503             * 20 / 21 / 22         - cif@30fps / half-D1@@25fps / D1@12.5fps
504             * 30 / 31 / 32         - D1@25fps / 720p@30fps / 720p@60fps
505             * 40 / 41 / 42         - 1080p@30fps / 1080p@30fps / 1080p@60fps
506             * 50 / 51 / 52         - 4K@30fps
507             */
508             codecMimeSet->avcSetup.level = AVC_SETUP_LEVEL_DEFAULT;
509             codecMimeSet->avcSetup.cabacEn = AVC_SETUP_CABAC_EN_DEFAULT;
510             codecMimeSet->avcSetup.cabacIdc = AVC_SETUP_CABAC_IDC_DEFAULT;
511             codecMimeSet->avcSetup.trans8x8 = AVC_SETUP_TRANS_DEFAULT;
512             break;
513         }
514         case MPP_VIDEO_CodingHEVC: {
515             break;
516         }
517         default: {
518             break;
519         }
520     }
521 }
522 
SetGop(RKHdiBaseComponent *pBaseComponent)523 static int32_t SetGop(RKHdiBaseComponent *pBaseComponent)
524 {
525     MPP_RET ret = MPP_OK;
526     RKMppApi *mppApi = pBaseComponent->mppApi;
527     RKHdiGopSetup *gopSet = &pBaseComponent->setup.gop;
528 
529     gopSet->gop = gopSet->gopLen ? gopSet->gopLen : pBaseComponent->setup.fps.fpsOutNum * DFAULT_ENC_GOP_OPERATOR;
530     ret = mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:gop", gopSet->gop);
531     if (ret != MPP_OK) {
532         HDF_LOGE("%{public}s: config mpp set gop failed! ret: %{public}d", __func__, ret);
533         return HDF_FAILURE;
534     }
535 
536     RK_U32 gopMode = 0;
537     RK_U32 defaultGopMode = ConvertHdiGopMode2RKGopMode(gopSet->gopMode);
538     mppApi->HdiMppEnvGetU32("gop_mode", &gopMode, defaultGopMode);
539     if (gopMode) {
540         MppEncRefCfg ref = NULL;
541         mppApi->HdiMppEncRefCfgInit(&ref);
542         if (gopMode < GOP_MODE_THRESHOLD_VALUE) {
543             mppApi->HdiMppEncGenRefCfg(ref, gopMode);
544         } else {
545             mppApi->HdiMppEncGenSmartGopRefCfg(ref, gopSet->gopLen, gopSet->viLen);
546         }
547         ret = pBaseComponent->mpi->control(pBaseComponent->ctx, MPP_ENC_SET_REF_CFG, ref);
548         if (ret != MPP_OK) {
549             HDF_LOGE("%{public}s: mpi control enc set ref cfg failed, ret %{public}d", __func__, ret);
550             mppApi->HdiMppEncRefCfgDeinit(&ref);
551             return HDF_FAILURE;
552         }
553         mppApi->HdiMppEncRefCfgDeinit(&ref);
554     }
555     return HDF_SUCCESS;
556 }
557 
SetEncCfg(RKHdiBaseComponent *pBaseComponent)558 int32_t SetEncCfg(RKHdiBaseComponent *pBaseComponent)
559 {
560     RKMppApi *mppApi = pBaseComponent->mppApi;
561     RKHdiEncodeSetup *setup = &pBaseComponent->setup;
562     GetDefaultConfig(pBaseComponent);
563 
564     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "prep:width", setup->width);
565     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "prep:height", setup->height);
566 
567     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "prep:format", pBaseComponent->fmt);
568 
569     pBaseComponent->horStride = GetStrideByWidth(setup->width, pBaseComponent->fmt);
570     pBaseComponent->verStride = setup->height;
571     if (pBaseComponent->horStride == 0 || pBaseComponent->verStride == 0) {
572         HDF_LOGE("%{public}s: size or stride incorrect!", __func__);
573         return HDF_FAILURE;
574     }
575     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "prep:hor_stride", pBaseComponent->horStride);
576     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "prep:ver_stride", pBaseComponent->verStride);
577 
578     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:fps_in_flex", setup->fps.fpsInFlex);
579     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:fps_in_num", setup->fps.fpsInNum);
580     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:fps_in_denorm", setup->fps.fpsInDen);
581     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:fps_out_flex", setup->fps.fpsOutFlex);
582     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:fps_out_num", setup->fps.fpsOutNum);
583     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:fps_out_denorm", setup->fps.fpsOutDen);
584 
585     MppEncRcMode mppRcMode = ConvertHdiRcMode2RKRcMode(setup->rc.rcMode);
586     CalcBpsValue(pBaseComponent, mppRcMode);
587     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:mode", mppRcMode);
588     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:bps_target", setup->rc.bpsTarget);
589     if (mppRcMode != MPP_ENC_RC_MODE_FIXQP) {
590         mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:bps_max", setup->rc.bpsMax);
591         mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:bps_min", setup->rc.bpsMin);
592     }
593 
594     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:qp_init", setup->rc.qpInit);
595     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:qp_max", setup->rc.qpMax);
596     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:qp_min", setup->rc.qpMin);
597     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:qp_max_i", setup->rc.qpMaxI);
598     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:qp_min_i", setup->rc.qpMinI);
599     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "rc:qp_ip", setup->rc.qpIp);
600 
601     mppApi->HdiMppEncCfgSetU32(pBaseComponent->cfg, "rc:drop_mode", setup->drop.dropMode);
602     mppApi->HdiMppEncCfgSetU32(pBaseComponent->cfg, "rc:drop_thd", setup->drop.dropThd);
603     mppApi->HdiMppEncCfgSetU32(pBaseComponent->cfg, "rc:drop_gap", setup->drop.dropGap);
604 
605     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "codec:type", pBaseComponent->codingType);
606     SetMimeSettings(&setup->codecMime, pBaseComponent->codingType);
607     if (pBaseComponent->codingType == MPP_VIDEO_CodingAVC) {
608         mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "h264:profile", setup->codecMime.avcSetup.profile);
609         mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "h264:level", setup->codecMime.avcSetup.level);
610         mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "h264:cabac_en", setup->codecMime.avcSetup.cabacEn);
611         mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "h264:cabac_idc", setup->codecMime.avcSetup.cabacIdc);
612         mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "h264:trans8x8", setup->codecMime.avcSetup.trans8x8);
613     }
614     SetGop(pBaseComponent);
615     ValidateEncSetup(pBaseComponent, NULL);
616     return HDF_SUCCESS;
617 }
618 
SetDecCfg(RKHdiBaseComponent *pBaseComponent)619 int32_t SetDecCfg(RKHdiBaseComponent *pBaseComponent)
620 {
621     MPP_RET ret = MPP_OK;
622     RKMppApi *mppApi = pBaseComponent->mppApi;
623     MppCtx ctx = pBaseComponent->ctx;
624 
625     GetDefaultConfig(pBaseComponent);
626     mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "codec:type", pBaseComponent->codingType);
627     mppApi->HdiMppDecCfgSetU32(pBaseComponent->cfg, "base:split_parse", pBaseComponent->setup.split);
628     ret = pBaseComponent->mpi->control(ctx, MPP_DEC_SET_CFG, pBaseComponent->cfg);
629     if (ret != MPP_OK) {
630         HDF_LOGE("%{public}s: config MPP_DEC_SET_CFG failed, ret %{public}d", __func__, ret);
631         UINTPTR userData = NULL;
632         EventType event = EVENT_ERROR;
633         uint32_t length = 0;
634         int32_t *eventData = NULL;
635 
636         pBaseComponent->pCallbacks->OnEvent(userData, event, length, eventData);
637         return HDF_FAILURE;
638     }
639     return HDF_SUCCESS;
640 }
641 
SetParamWidth(RKHdiBaseComponent *pBaseComponent, Param *param)642 int32_t SetParamWidth(RKHdiBaseComponent *pBaseComponent, Param *param)
643 {
644     pBaseComponent->setup.width = *(RK_S32 *)param->val;
645     if (pBaseComponent->setup.width <= 0) {
646         HDF_LOGE("%{public}s: invalid width value!", __func__);
647         return HDF_ERR_INVALID_PARAM;
648     }
649     return HDF_SUCCESS;
650 }
651 
SetParamHeight(RKHdiBaseComponent *pBaseComponent, Param *param)652 int32_t SetParamHeight(RKHdiBaseComponent *pBaseComponent, Param *param)
653 {
654     pBaseComponent->setup.height = *(RK_S32 *)param->val;
655     if (pBaseComponent->setup.height <= 0) {
656         HDF_LOGE("%{public}s: invalid height value!", __func__);
657         return HDF_ERR_INVALID_PARAM;
658     }
659     return HDF_SUCCESS;
660 }
661 
SetParamPixelFmt(RKHdiBaseComponent *pBaseComponent, Param *param)662 int32_t SetParamPixelFmt(RKHdiBaseComponent *pBaseComponent, Param *param)
663 {
664     RK_S32 *hdiPixFmt = (RK_S32 *)param->val;
665     MppFrameFormat mppPixFmt = ConvertHdiFormat2RKFormat(*hdiPixFmt);
666     if (mppPixFmt == MPP_FMT_BUTT) {
667         HDF_LOGE("%{public}s: invalid pixel format value!", __func__);
668         return HDF_FAILURE;
669     }
670     pBaseComponent->setup.fmt = *hdiPixFmt;
671     pBaseComponent->fmt = MPP_FMT_YUV420SP;
672     return HDF_SUCCESS;
673 }
674 
SetParamStride(RKHdiBaseComponent *pBaseComponent, Param *param)675 int32_t SetParamStride(RKHdiBaseComponent *pBaseComponent, Param *param)
676 {
677     pBaseComponent->setup.stride.horStride = *(RK_S32 *)param->val;
678     if (pBaseComponent->setup.stride.horStride <= 0) {
679         HDF_LOGE("%{public}s: invalid stride value!", __func__);
680         return HDF_ERR_INVALID_PARAM;
681     }
682     return HDF_SUCCESS;
683 }
684 
SetParamFps(RKHdiBaseComponent *pBaseComponent, Param *param)685 int32_t SetParamFps(RKHdiBaseComponent *pBaseComponent, Param *param)
686 {
687     pBaseComponent->setup.fps.fpsInNum = *(RK_S32 *)param->val;
688     pBaseComponent->setup.fps.fpsOutNum = *(RK_S32 *)param->val;
689     if (pBaseComponent->setup.fps.fpsInNum <= 0) {
690         HDF_LOGE("%{public}s: invalid fps value!", __func__);
691         return HDF_ERR_INVALID_PARAM;
692     }
693     return HDF_SUCCESS;
694 }
695 
SetParamDrop(RKHdiBaseComponent *pBaseComponent, Param *param)696 int32_t SetParamDrop(RKHdiBaseComponent *pBaseComponent, Param *param)
697 {
698     pBaseComponent->setup.drop.dropMode = *(RK_U32 *)param->val;
699     if (pBaseComponent->setup.drop.dropMode < 0 || pBaseComponent->setup.drop.dropMode >= MPP_ENC_RC_DROP_FRM_BUTT) {
700         HDF_LOGE("%{public}s: drop stride value!", __func__);
701         return HDF_ERR_INVALID_PARAM;
702     }
703     return HDF_SUCCESS;
704 }
705 
SetParamRateControl(RKHdiBaseComponent *pBaseComponent, Param *param)706 int32_t SetParamRateControl(RKHdiBaseComponent *pBaseComponent, Param *param)
707 {
708     pBaseComponent->setup.rc.rcMode = *(RK_S32 *)param->val;
709     if (pBaseComponent->setup.rc.rcMode < 0) {
710         HDF_LOGE("%{public}s: invalid rcMode value!", __func__);
711         return HDF_ERR_INVALID_PARAM;
712     }
713     return HDF_SUCCESS;
714 }
715 
SetParamGop(RKHdiBaseComponent *pBaseComponent, Param *param)716 int32_t SetParamGop(RKHdiBaseComponent *pBaseComponent, Param *param)
717 {
718     pBaseComponent->setup.gop.gopMode = *(VideoCodecGopMode *)param->val;
719     if (pBaseComponent->setup.gop.gopMode < 0 || pBaseComponent->setup.gop.gopMode >= VID_CODEC_GOPMODE_INVALID) {
720         HDF_LOGE("%{public}s: invalid gop value!", __func__);
721         return HDF_ERR_INVALID_PARAM;
722     }
723     return HDF_SUCCESS;
724 }
725 
SetParamMimeCodecType(RKHdiBaseComponent *pBaseComponent, Param *param)726 int32_t SetParamMimeCodecType(RKHdiBaseComponent *pBaseComponent, Param *param)
727 {
728     RKHdiCodecMimeSetup *mimeCodecTypeSet = &pBaseComponent->setup.codecMime;
729     mimeCodecTypeSet->mimeCodecType = *(RK_S32 *)param->val;
730     if (mimeCodecTypeSet->mimeCodecType < 0 || mimeCodecTypeSet->mimeCodecType >= MEDIA_MIMETYPE_INVALID) {
731         HDF_LOGE("%{public}s: invalid mime type value!", __func__);
732         return HDF_ERR_INVALID_PARAM;
733     }
734 
735     MppCodingType codingType = ConvertHdiMimeCodecType2RKCodecType(mimeCodecTypeSet->mimeCodecType);
736     if (codingType == MPP_VIDEO_CodingMax) {
737         return HDF_ERR_INVALID_PARAM;
738     }
739     pBaseComponent->codingType = codingType;
740     return HDF_SUCCESS;
741 }
742 
SetParamCodecType(RKHdiBaseComponent *pBaseComponent, Param *param)743 int32_t SetParamCodecType(RKHdiBaseComponent *pBaseComponent, Param *param)
744 {
745     pBaseComponent->setup.codecType = *(RK_S32 *)param->val;
746     if (pBaseComponent->setup.codecType < 0 || pBaseComponent->setup.codecType >= INVALID_TYPE) {
747         HDF_LOGE("%{public}s: invalid codec type value!", __func__);
748         return HDF_ERR_INVALID_PARAM;
749     }
750     return HDF_SUCCESS;
751 }
752 
SetParamSplitParse(RKHdiBaseComponent *pBaseComponent, Param *param)753 int32_t SetParamSplitParse(RKHdiBaseComponent *pBaseComponent, Param *param)
754 {
755     pBaseComponent->setup.split = *(RK_U32 *)param->val;
756     return HDF_SUCCESS;
757 }
758 
SetParamCodecFrameNum(RKHdiBaseComponent *pBaseComponent, Param *param)759 int32_t SetParamCodecFrameNum(RKHdiBaseComponent *pBaseComponent, Param *param)
760 {
761     pBaseComponent->frameNum = *(RK_S32 *)param->val;
762     if (pBaseComponent->frameNum < 0) {
763         HDF_LOGE("%{public}s: invalid frame num value!", __func__);
764         return HDF_ERR_INVALID_PARAM;
765     }
766     return HDF_SUCCESS;
767 }
768 
CheckSetupStride(RKHdiBaseComponent *pBaseComponent)769 int32_t CheckSetupStride(RKHdiBaseComponent *pBaseComponent)
770 {
771     if (pBaseComponent->setup.width <= 0 || pBaseComponent->setup.height <= 0) {
772         HDF_LOGE("%{public}s: width or height invalid!", __func__);
773         return HDF_ERR_INVALID_PARAM;
774     }
775     if (pBaseComponent->setup.stride.horStride == 0) {
776         pBaseComponent->setup.stride.horStride = MPP_ALIGN(pBaseComponent->setup.width, MPP_ALIGN_STRIDE_WITH_SIXTEEN);
777     }
778     if (pBaseComponent->setup.stride.verStride == 0) {
779         pBaseComponent->setup.stride.verStride = pBaseComponent->setup.height;
780     }
781     return HDF_SUCCESS;
782 }
783 
ValidateEncSetup(RKHdiBaseComponent *pBaseComponent, Param *param)784 int32_t ValidateEncSetup(RKHdiBaseComponent *pBaseComponent, Param *param)
785 {
786     MPP_RET ret = MPP_OK;
787     RKMppApi *mppApi = pBaseComponent->mppApi;
788     RK_U32 splitMode = 0;
789     RK_U32 splitArg = 0;
790 
791     int32_t checkResult = CheckSetupStride(pBaseComponent);
792     if (checkResult != HDF_SUCCESS) {
793         HDF_LOGE("%{public}s: CheckSetupStride failed!", __func__);
794         return checkResult;
795     }
796 
797     mppApi->HdiMppEnvGetU32("split_mode", &splitMode, MPP_ENC_SPLIT_NONE);
798     mppApi->HdiMppEnvGetU32("split_arg", &splitArg, 0);
799     if (splitMode) {
800         HDF_LOGI("%{public}s: %{public}p splitMode %{public}d splitArg %{public}d",
801             __func__, pBaseComponent->ctx, splitMode, splitArg);
802         mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "split:mode", splitMode);
803         mppApi->HdiMppEncCfgSetS32(pBaseComponent->cfg, "split:arg", splitArg);
804     }
805 
806     ret = pBaseComponent->mpi->control(pBaseComponent->ctx, MPP_ENC_SET_CFG, pBaseComponent->cfg);
807     if (ret != MPP_OK) {
808         HDF_LOGE("%{public}s: mpi control enc set cfg failed ret %{public}d", __func__, ret);
809         return HDF_FAILURE;
810     }
811 
812     /* optional */
813     MppEncSeiMode seiMode = MPP_ENC_SEI_MODE_ONE_FRAME;
814     ret = pBaseComponent->mpi->control(pBaseComponent->ctx, MPP_ENC_SET_SEI_CFG, &seiMode);
815     if (ret != MPP_OK) {
816         HDF_LOGE("%{public}s: mpi control enc set sei cfg failed ret %{public}d", __func__, ret);
817         return HDF_FAILURE;
818     }
819 
820     if ((pBaseComponent->codingType == MPP_VIDEO_CodingAVC) || (pBaseComponent->codingType == MPP_VIDEO_CodingHEVC)) {
821         MppEncHeaderMode headerMode = MPP_ENC_HEADER_MODE_EACH_IDR;
822         ret = pBaseComponent->mpi->control(pBaseComponent->ctx, MPP_ENC_SET_HEADER_MODE, &headerMode);
823         if (ret != MPP_OK) {
824             HDF_LOGE("%{public}s: mpi control enc set header mode failed ret %{public}d", __func__, ret);
825             return HDF_FAILURE;
826         }
827     }
828 
829     pBaseComponent->frameSize = GetMppFrameBufferSize(pBaseComponent);
830     pBaseComponent->headerSize = GetMppFrameHeaderSize(pBaseComponent);
831     ret = GetMppBuffers(pBaseComponent);
832     if (ret != MPP_OK) {
833         HDF_LOGE("%{public}s: GetMppBuffers failed ret %{public}d", __func__, ret);
834         return HDF_FAILURE;
835     }
836 
837     return HDF_SUCCESS;
838 }
839 
CalculateTotalDecRefFrames(RKHdiBaseComponent *pBaseComponent)840 uint32_t CalculateTotalDecRefFrames(RKHdiBaseComponent *pBaseComponent)
841 {
842     uint32_t refFramesNum = DEFAULT_REF_FRAME_NUM;
843     int32_t width = pBaseComponent->setup.width;
844     int32_t height = pBaseComponent->setup.height;
845 
846     if (width == 0 || height == 0 || pBaseComponent->codingType < 0 ||
847         pBaseComponent->codingType >= MPP_VIDEO_CodingMax) {
848         HDF_LOGE("%{public}s: invalid parameters", __func__);
849         return refFramesNum;
850     }
851     switch (pBaseComponent->codingType) {
852         case MPP_VIDEO_CodingAVC:
853             /* used level 5.1 MaxDpbMbs to compute ref num */
854             refFramesNum = LEVEL_5P1_MAX_DPB_MBS / ((width / REF_FRAME_DIVISOR) * (height / REF_FRAME_DIVISOR));
855             if (refFramesNum > MAX_REF_FRAME_NUM) {
856                 /* max ref frame number is 16 */
857                 refFramesNum = MAX_REF_FRAME_NUM;
858             } else if (refFramesNum < MIN_REF_FRAME_NUM) {
859                 /* min ref frame number is 6 */
860                 refFramesNum = MIN_REF_FRAME_NUM;
861             }
862             break;
863         case MPP_VIDEO_CodingHEVC:
864             /* use 4K resolution to compute ref num */
865             refFramesNum = WIDTH_OF_4K_VIDEO * HEIGHT_OF_4K_HEVC * MIN_REF_FRAME_NUM / (width * height);
866             if (refFramesNum > MAX_REF_FRAME_NUM) {
867                 /* max ref frame number is 16 */
868                 refFramesNum = MAX_REF_FRAME_NUM;
869             } else if (refFramesNum < MIN_REF_FRAME_NUM) {
870                 /* min ref frame number is 6 */
871                 refFramesNum = MIN_REF_FRAME_NUM;
872             }
873             break;
874         case MPP_VIDEO_CodingVP9:
875             refFramesNum = WIDTH_OF_4K_VIDEO * HEIGHT_OF_4K_VP9 * MIN_REF_FRAME_NUM_VP9 / (width * height);
876             if (refFramesNum > MAX_REF_FRAME_NUM_VP9) {
877                 /* max ref frame number is 8 */
878                 refFramesNum = MAX_REF_FRAME_NUM_VP9;
879             } else if (refFramesNum < MIN_REF_FRAME_NUM_VP9) {
880                 /* min ref frame number is 4 */
881                 refFramesNum = MIN_REF_FRAME_NUM_VP9;
882             }
883             break;
884         default:
885             refFramesNum = DEFAULT_REF_FRAME_NUM;
886             break;
887     }
888 
889     return refFramesNum;
890 }
891 
GetParamInputBufferCount(RKHdiBaseComponent *pBaseComponent, Param *param)892 int32_t GetParamInputBufferCount(RKHdiBaseComponent *pBaseComponent, Param *param)
893 {
894     if (param->val == NULL) {
895         param->size = sizeof(RK_U32);
896         param->val = malloc(param->size);
897     }
898 
899     RK_U32 inputBufferCount = 0;
900     if (pBaseComponent->ctxType == MPP_CTX_ENC) {
901         inputBufferCount = DEFAULT_FRAME_BUFFER_NUM;
902     } else {
903         inputBufferCount = DEFAULT_PACKET_BUFFER_NUM;
904     }
905     int32_t ret = memcpy_s(param->val, param->size, &inputBufferCount, sizeof(RK_U32));
906     if (ret != EOK) {
907         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
908         return HDF_FAILURE;
909     }
910     return HDF_SUCCESS;
911 }
912 
GetParamOutputBufferCount(RKHdiBaseComponent *pBaseComponent, Param *param)913 int32_t GetParamOutputBufferCount(RKHdiBaseComponent *pBaseComponent, Param *param)
914 {
915     if (param->val == NULL) {
916         param->size = sizeof(RK_U32);
917         param->val = malloc(param->size);
918     }
919 
920     RK_U32 outputBufferCount = 0;
921     if (pBaseComponent->ctxType == MPP_CTX_ENC) {
922         outputBufferCount = DEFAULT_PACKET_BUFFER_NUM;
923     } else {
924         RK_U32 refFramesNum = CalculateTotalDecRefFrames(pBaseComponent);
925         outputBufferCount = refFramesNum + BASIC_DEC_FRAME_BUFFER_NUM;
926     }
927     int32_t ret = memcpy_s(param->val, param->size, &outputBufferCount, sizeof(RK_U32));
928     if (ret != EOK) {
929         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
930         return HDF_FAILURE;
931     }
932     return HDF_SUCCESS;
933 }
934 
GetParamBufferSize(RKHdiBaseComponent *pBaseComponent, Param *param)935 int32_t GetParamBufferSize(RKHdiBaseComponent *pBaseComponent, Param *param)
936 {
937     if (param->val == NULL) {
938         param->size = sizeof(RK_U32);
939         param->val = malloc(param->size);
940     }
941 
942     PixelFormat format = pBaseComponent->setup.fmt;
943     if (format == PIXEL_FMT_BUTT) {
944         HDF_LOGE("%{public}s: pixel format not set!", __func__);
945         return HDF_FAILURE;
946     }
947 
948     RK_U32 bufferSize = pBaseComponent->setup.width * pBaseComponent->setup.height;
949     if (bufferSize == 0) {
950         bufferSize = DEFAULT_RESOLUTION_SIZE;
951     }
952     if (format == PIXEL_FMT_YCBCR_420_SP || format == PIXEL_FMT_YCBCR_420_P) {
953         bufferSize = (bufferSize * PACKET_SIZE_MULTI) / PACKET_SIZE_OPERATOR;
954     } else if (format == PIXEL_FMT_BGRA_8888 || format == PIXEL_FMT_RGBA_8888) {
955         bufferSize = bufferSize * FOUR_BYTE_PIX_BUF_SIZE_OPERATOR;
956     } else {
957         bufferSize = bufferSize * DFAULT_BUF_SIZE_OPERATOR;
958     }
959     int32_t ret = memcpy_s(param->val, param->size, &bufferSize, sizeof(RK_U32));
960     if (ret != EOK) {
961         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
962         return HDF_FAILURE;
963     }
964     return HDF_SUCCESS;
965 }
966 
GetParamWidth(RKHdiBaseComponent *pBaseComponent, Param *param)967 int32_t GetParamWidth(RKHdiBaseComponent *pBaseComponent, Param *param)
968 {
969     if (param->val == NULL) {
970         param->size = sizeof(RK_S32);
971         param->val = malloc(param->size);
972     }
973     int32_t ret = memcpy_s(param->val, param->size, &pBaseComponent->setup.width, sizeof(RK_S32));
974     if (ret != EOK) {
975         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
976         return HDF_FAILURE;
977     }
978     return HDF_SUCCESS;
979 }
980 
GetParamHeight(RKHdiBaseComponent *pBaseComponent, Param *param)981 int32_t GetParamHeight(RKHdiBaseComponent *pBaseComponent, Param *param)
982 {
983     if (param->val == NULL) {
984         param->size = sizeof(RK_S32);
985         param->val = malloc(param->size);
986     }
987     int32_t ret = memcpy_s(param->val, param->size, &pBaseComponent->setup.height, sizeof(RK_S32));
988     if (ret != EOK) {
989         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
990         return HDF_FAILURE;
991     }
992     return HDF_SUCCESS;
993 }
994 
GetParamDecOutputPixelFmt(RKHdiBaseComponent *pBaseComponent, Param *param)995 int32_t GetParamDecOutputPixelFmt(RKHdiBaseComponent *pBaseComponent, Param *param)
996 {
997     if (param->val == NULL) {
998         param->size = sizeof(PixelFormat);
999         param->val = malloc(param->size);
1000     }
1001 
1002     PixelFormat format = pBaseComponent->setup.fmt;
1003     if (format == PIXEL_FMT_BUTT) {
1004         // Rk mpp only support NV12 output (rga can be used for other formats)
1005         format = PIXEL_FMT_YCBCR_420_SP;
1006         pBaseComponent->setup.fmt = PIXEL_FMT_YCBCR_420_SP;
1007         pBaseComponent->fmt = MPP_FMT_YUV420SP;
1008     }
1009     int32_t ret = memcpy_s(param->val, param->size, &format, sizeof(PixelFormat));
1010     if (ret != EOK) {
1011         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
1012         return HDF_FAILURE;
1013     }
1014     return HDF_SUCCESS;
1015 }
1016 
GetParamEncInputPixleFmt(RKHdiBaseComponent *pBaseComponent, Param *param)1017 int32_t GetParamEncInputPixleFmt(RKHdiBaseComponent *pBaseComponent, Param *param)
1018 {
1019     if (param->val == NULL) {
1020         param->size = sizeof(PixelFormat);
1021         param->val = malloc(param->size);
1022     }
1023     int32_t ret = memcpy_s(param->val, param->size, &pBaseComponent->setup.fmt, sizeof(PixelFormat));
1024     if (ret != EOK) {
1025         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
1026         return HDF_FAILURE;
1027     }
1028     return HDF_SUCCESS;
1029 }
1030 
GetParamPixleFmt(RKHdiBaseComponent *pBaseComponent, Param *param)1031 int32_t GetParamPixleFmt(RKHdiBaseComponent *pBaseComponent, Param *param)
1032 {
1033     if (pBaseComponent->setup.codecType == VIDEO_DECODER) {
1034         return GetParamDecOutputPixelFmt(pBaseComponent, param);
1035     } else if (pBaseComponent->setup.codecType == VIDEO_ENCODER) {
1036         return GetParamEncInputPixleFmt(pBaseComponent, param);
1037     } else {
1038         HDF_LOGE("%{public}s: codec type invalid!", __func__);
1039         return HDF_FAILURE;
1040     }
1041 }
1042 
GetParamStride(RKHdiBaseComponent *pBaseComponent, Param *param)1043 int32_t GetParamStride(RKHdiBaseComponent *pBaseComponent, Param *param)
1044 {
1045     if (param->val == NULL) {
1046         param->size = sizeof(RK_S32);
1047         param->val = malloc(param->size);
1048     }
1049     int32_t ret = memcpy_s(param->val, param->size, &pBaseComponent->setup.stride.horStride, sizeof(RK_S32));
1050     if (ret != EOK) {
1051         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
1052         return HDF_FAILURE;
1053     }
1054     return HDF_SUCCESS;
1055 }
1056 
GetParamFps(RKHdiBaseComponent *pBaseComponent, Param *param)1057 int32_t GetParamFps(RKHdiBaseComponent *pBaseComponent, Param *param)
1058 {
1059     if (param->val == NULL) {
1060         param->size = sizeof(RK_S32);
1061         param->val = malloc(param->size);
1062     }
1063     int32_t ret = memcpy_s(param->val, param->size, &pBaseComponent->setup.fps.fpsInNum, sizeof(RK_S32));
1064     if (ret != EOK) {
1065         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
1066         return HDF_FAILURE;
1067     }
1068     return HDF_SUCCESS;
1069 }
1070 
GetParamRateControl(RKHdiBaseComponent *pBaseComponent, Param *param)1071 int32_t GetParamRateControl(RKHdiBaseComponent *pBaseComponent, Param *param)
1072 {
1073     if (param->val == NULL) {
1074         param->size = sizeof(RK_S32);
1075         param->val = malloc(param->size);
1076     }
1077     int32_t ret = memcpy_s(param->val, param->size, &pBaseComponent->setup.rc.rcMode, sizeof(RK_S32));
1078     if (ret != EOK) {
1079         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
1080         return HDF_FAILURE;
1081     }
1082     return HDF_SUCCESS;
1083 }
1084 
GetParamGop(RKHdiBaseComponent *pBaseComponent, Param *param)1085 int32_t GetParamGop(RKHdiBaseComponent *pBaseComponent, Param *param)
1086 {
1087     if (param->val == NULL) {
1088         param->size = sizeof(RK_S32);
1089         param->val = malloc(param->size);
1090     }
1091     int32_t ret = memcpy_s(param->val, param->size, &pBaseComponent->setup.gop.gopMode, sizeof(RK_S32));
1092     if (ret != EOK) {
1093         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
1094         return HDF_FAILURE;
1095     }
1096     return HDF_SUCCESS;
1097 }
1098 
GetParamMimeCodecType(RKHdiBaseComponent *pBaseComponent, Param *param)1099 int32_t GetParamMimeCodecType(RKHdiBaseComponent *pBaseComponent, Param *param)
1100 {
1101     if (param->val == NULL) {
1102         param->size = sizeof(RK_S32);
1103         param->val = malloc(param->size);
1104     }
1105     int32_t ret = memcpy_s(param->val, param->size, &pBaseComponent->setup.codecMime.mimeCodecType, sizeof(RK_S32));
1106     if (ret != EOK) {
1107         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
1108         return HDF_FAILURE;
1109     }
1110     return HDF_SUCCESS;
1111 }
1112 
GetParamCodecType(RKHdiBaseComponent *pBaseComponent, Param *param)1113 int32_t GetParamCodecType(RKHdiBaseComponent *pBaseComponent, Param *param)
1114 {
1115     if (param->val == NULL) {
1116         param->size = sizeof(RK_S32);
1117         param->val = malloc(param->size);
1118     }
1119     int32_t ret = memcpy_s(param->val, param->size, &pBaseComponent->setup.codecType, sizeof(RK_S32));
1120     if (ret != EOK) {
1121         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
1122         return HDF_FAILURE;
1123     }
1124     return HDF_SUCCESS;
1125 }
1126 
GetParamSplitParse(RKHdiBaseComponent *pBaseComponent, Param *param)1127 int32_t GetParamSplitParse(RKHdiBaseComponent *pBaseComponent, Param *param)
1128 {
1129     if (param->val == NULL) {
1130         param->size = sizeof(RK_U32);
1131         param->val = malloc(param->size);
1132     }
1133     int32_t ret = memcpy_s(param->val, param->size, &pBaseComponent->setup.split, sizeof(RK_U32));
1134     if (ret != EOK) {
1135         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
1136         return HDF_FAILURE;
1137     }
1138     return HDF_SUCCESS;
1139 }
1140 
GetParamCodecFrameNum(RKHdiBaseComponent *pBaseComponent, Param *param)1141 int32_t GetParamCodecFrameNum(RKHdiBaseComponent *pBaseComponent, Param *param)
1142 {
1143     if (param->val == NULL) {
1144         param->size = sizeof(RK_S32);
1145         param->val = malloc(param->size);
1146     }
1147     int32_t ret = memcpy_s(param->val, param->size, &pBaseComponent->frameNum, sizeof(RK_S32));
1148     if (ret != EOK) {
1149         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
1150         return HDF_FAILURE;
1151     }
1152     return HDF_SUCCESS;
1153 }
1154 
GetParamDrop(RKHdiBaseComponent *pBaseComponent, Param *param)1155 int32_t GetParamDrop(RKHdiBaseComponent *pBaseComponent, Param *param)
1156 {
1157     if (param->val == NULL) {
1158         param->size = sizeof(RK_S32);
1159         param->val = malloc(param->size);
1160     }
1161     int32_t ret = memcpy_s(param->val, param->size, &pBaseComponent->setup.drop.dropMode, sizeof(RK_S32));
1162     if (ret != EOK) {
1163         HDF_LOGE("%{public}s: copy data failed, error code: %{public}d", __func__, ret);
1164         return HDF_FAILURE;
1165     }
1166     return HDF_SUCCESS;
1167 }
1168