1/*
2 * Copyright 2023 Shenzhen Kaihong DID Co., Ltd.
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#ifndef CODEC_UTIL_H
17#define CODEC_UTIL_H
18
19#include <list>
20#include <map>
21#include <securec.h>
22#include "hdf_log.h"
23#include <OMX_Component.h>
24class CodecUtil {
25public:
26    template <typename T>
27    int32_t InitParam(T &param)
28    {
29        auto ret = memset_s(&param, sizeof(param), 0x0, sizeof(param));
30        if (ret != EOK) {
31            HDF_LOGE("%{public}s error, memset_s ret [%{public}d", __func__, ret);
32            return HDF_FAILURE;
33        }
34        param.nSize = sizeof(param);
35        param.nVersion.s.nVersionMajor = 1;  // mVersion.s.nVersionMajor;
36        return HDF_SUCCESS;
37    }
38
39    template <typename T>
40    int32_t InitParamInOhos(T &param)
41    {
42        auto ret = memset_s(&param, sizeof(param), 0x0, sizeof(param));
43        if (ret != EOK) {
44            HDF_LOGE("%{public}s error, memset_s ret [%{public}d", __func__, ret);
45            return HDF_FAILURE;
46        }
47        param.size = sizeof(param);
48        param.version.s.nVersionMajor = 1;  // mVersion.s.nVersionMajor;
49        return HDF_SUCCESS;
50    }
51
52    template <typename T>
53    void ObjectToVector(T &param, std::vector<int8_t> &vec)
54    {
55        vec.clear();
56        int8_t *paramPointer = (int8_t *)&param;
57        vec.insert(vec.end(), paramPointer, paramPointer + sizeof(param));
58    }
59
60    template <typename T>
61    int32_t VectorToObject(std::vector<int8_t> &vec, T &param)
62    {
63        auto ret = memcpy_s(&param, sizeof(param), vec.data(), vec.size());
64        if (ret != EOK) {
65            HDF_LOGE("%{public}s error, memset_s ret [%{public}d", __func__, ret);
66            return HDF_FAILURE;
67        }
68        vec.clear();
69        return HDF_SUCCESS;
70    }
71
72    void setParmValue(OMX_PARAM_PORTDEFINITIONTYPE &param, uint32_t width, uint32_t height, uint32_t stride)
73    {
74        param.format.video.nFrameWidth = width;
75        param.format.video.nFrameHeight = height;
76        param.format.video.nStride = stride;
77        param.format.video.nSliceHeight = height;
78    }
79};
80#endif /* CODEC_UTIL_H */
81