1 /*
2  * Copyright (c) 2023 Huawei Device 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 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_FUNCTION_UTIL_H
17 #define CODEC_FUNCTION_UTIL_H
18 
19 #include <OMX_Component.h>
20 #include <OMX_Core.h>
21 #include <OMX_Video.h>
22 #include <OMX_VideoExt.h>
23 #include <list>
24 #include <map>
25 #include <securec.h>
26 #include "hdf_log.h"
27 #include "codec_omx_ext.h"
28 #include "v3_0/codec_callback_service.h"
29 #include "v3_0/icodec_component.h"
30 #include "v3_0/icodec_component_manager.h"
31 #include "v1_0/display_composer_type.h"
32 #include "v1_0/display_buffer_type.h"
33 #include "v1_0/include/idisplay_buffer.h"
34 
35 constexpr int32_t WIDTH = 640;
36 constexpr uint32_t MAX_ROLE_INDEX = 256;
37 constexpr int FD_DEFAULT = -1;
38 constexpr int64_t APP_DATA = 3;
39 constexpr int32_t HEIGHT = 480;
40 constexpr int32_t BUFFER_SIZE = WIDTH * HEIGHT * 3;
41 constexpr int32_t FRAMERATE = 30 << 16;
42 constexpr uint32_t BUFFER_ID_ERROR = 65000;
43 constexpr int ERROE_FENCEFD = -1;
44 constexpr uint32_t WAIT_TIME = 1000;
45 constexpr uint32_t MAX_WAIT = 50;
46 constexpr uint32_t DENOMINATOR = 2;
47 constexpr uint32_t NUMERATOR = 3;
48 constexpr uint32_t ALIGNMENT = 16;
49 
50 namespace OHOS {
51 namespace HDI {
52 namespace Codec {
53 namespace V3_0 {
54 enum class PortIndex { INDEX_INPUT = 0, INDEX_OUTPUT = 1 };
55 class FunctionUtil : public RefBase {
56     struct BufferInfo {
57         std::shared_ptr<OmxCodecBuffer> omxBuffer;
58         std::shared_ptr<OHOS::Ashmem> sharedMem;
59         BufferHandle *bufferHandle;
BufferInfoBufferInfo60         BufferInfo()
61         {
62             omxBuffer = nullptr;
63             sharedMem = nullptr;
64             bufferHandle = nullptr;
65         }
~BufferInfoBufferInfo66         ~BufferInfo()
67         {
68             omxBuffer = nullptr;
69             if (sharedMem != nullptr) {
70                 sharedMem->UnmapAshmem();
71                 sharedMem->CloseAshmem();
72                 sharedMem = nullptr;
73             }
74             if (bufferHandle != nullptr && buffer_ != nullptr) {
75                 buffer_->FreeMem(*bufferHandle);
76                 bufferHandle = nullptr;
77             }
78         }
79     };
80 
81 public:
82     explicit FunctionUtil(CodecVersionType version);
83 
84     ~FunctionUtil();
85 
86     template <typename T>
InitParam(T & param)87     void InitParam(T &param)
88     {
89         memset_s(&param, sizeof(param), 0x0, sizeof(param));
90         param.nSize = sizeof(param);
91         param.nVersion.nVersion = 1;
92     }
93 
94     template <typename T>
InitExtParam(T & param)95     void InitExtParam(T &param)
96     {
97         memset_s(&param, sizeof(param), 0x0, sizeof(param));
98         param.size = sizeof(param);
99         param.version.nVersion = 1;
100     }
101 
102     template <typename T>
ObjectToVector(T & param,std::vector<int8_t> & vec)103     void ObjectToVector(T &param, std::vector<int8_t> &vec)
104     {
105         int8_t *paramPointer = reinterpret_cast<int8_t *>(&param);
106         vec.insert(vec.end(), paramPointer, paramPointer + sizeof(param));
107     }
108 
109     template <typename T>
VectorToObject(std::vector<int8_t> & vec,T & param)110     int32_t VectorToObject(std::vector<int8_t> &vec, T &param)
111     {
112         auto ret = memcpy_s(&param, sizeof(param), vec.data(), vec.size());
113         if (ret != EOK) {
114             HDF_LOGE("%{public}s error, memset_s ret [%{public}d", __func__, ret);
115             return HDF_FAILURE;
116         }
117         vec.clear();
118         return HDF_SUCCESS;
119     }
120 
121     uint32_t AlignUp(uint32_t width);
122 
123     void InitOmxCodecBuffer(OmxCodecBuffer &buffer, CodecBufferType type);
124 
125     void InitCodecBufferWithAshMem(enum PortIndex port, int bufferSize, std::shared_ptr<OmxCodecBuffer> omxBuffer,
126         std::shared_ptr<OHOS::Ashmem> sharedMem);
127 
128     bool InitBufferHandleParameter(sptr<ICodecComponent> component, OMX_PARAM_PORTDEFINITIONTYPE &param,
129         uint32_t port, CodecBufferType bufferType);
130 
131     bool FillCodecBufferWithBufferHandle(std::shared_ptr<OmxCodecBuffer> omxBuffer);
132 
133     bool UseDynaBuffer(sptr<ICodecComponent> component, enum PortIndex port, int bufferCount, int bufferSize);
134 
135     bool UseHandleBuffer(sptr<ICodecComponent> component, enum PortIndex port, int bufferCount, int bufferSize);
136 
137     bool UseBufferOnPort(sptr<ICodecComponent> component, enum PortIndex port, int32_t bufferCount,
138         int32_t bufferSize);
139 
140     bool AllocateBufferOnPort(sptr<ICodecComponent> component, enum PortIndex port, int32_t bufferCount,
141         int32_t bufferSize);
142 
143     bool FreeBufferOnPort(sptr<ICodecComponent> component, enum PortIndex port);
144 
145     int32_t GetPortParameter(sptr<ICodecComponent> component, PortIndex index, OMX_PARAM_PORTDEFINITIONTYPE &param);
146 
147     bool FillAndEmptyAllBuffer(sptr<ICodecComponent> component, CodecBufferType type);
148 
149     bool WaitState(sptr<ICodecComponent> component, CodecStateType objState);
150 
151     bool PushAlongParam(OmxCodecBuffer &omxBuffer);
152 
153 private:
154     static OHOS::HDI::Display::Buffer::V1_0::IDisplayBuffer *buffer_;
155     CodecVersionType version_;
156     std::map<int32_t, std::shared_ptr<BufferInfo>> inputBuffers_;
157     std::map<int32_t, std::shared_ptr<BufferInfo>> outputBuffers_;
158 };
159 } // V3_0
160 } // Codec
161 } // HDI
162 } // OHOS
163 
164 #endif /* CODEC_FUNCTION_UTIL_H */
165