1 /* 2 * Copyright (c) 2024 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 ¶m) 88 { 89 memset_s(¶m, 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 ¶m) 96 { 97 memset_s(¶m, 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 ¶m, std::vector<int8_t> &vec) 104 { 105 int8_t *paramPointer = reinterpret_cast<int8_t *>(¶m); 106 vec.clear(); 107 vec.insert(vec.end(), paramPointer, paramPointer + sizeof(param)); 108 } 109 110 template <typename T> VectorToObject(std::vector<int8_t> & vec,T & param)111 int32_t VectorToObject(std::vector<int8_t> &vec, T ¶m) 112 { 113 auto ret = memcpy_s(¶m, sizeof(param), vec.data(), vec.size()); 114 if (ret != EOK) { 115 HDF_LOGE("%{public}s error, memset_s ret [%{public}d", __func__, ret); 116 return HDF_FAILURE; 117 } 118 vec.clear(); 119 return HDF_SUCCESS; 120 } 121 122 uint32_t AlignUp(uint32_t width); 123 124 void InitOmxCodecBuffer(OmxCodecBuffer &buffer, CodecBufferType type); 125 126 void InitCodecBufferWithAshMem(enum PortIndex port, int bufferSize, std::shared_ptr<OmxCodecBuffer> omxBuffer, 127 std::shared_ptr<OHOS::Ashmem> sharedMem); 128 129 bool InitBufferHandleParameter(sptr<ICodecComponent> component, OMX_PARAM_PORTDEFINITIONTYPE ¶m, 130 uint32_t port, CodecBufferType bufferType); 131 132 bool FillCodecBufferWithBufferHandle(std::shared_ptr<OmxCodecBuffer> omxBuffer); 133 134 bool UseDynaBuffer(sptr<ICodecComponent> component, enum PortIndex port, int bufferCount, int bufferSize); 135 136 bool UseHandleBuffer(sptr<ICodecComponent> component, enum PortIndex port, int bufferCount, int bufferSize); 137 138 bool UseBufferOnPort(sptr<ICodecComponent> component, enum PortIndex port, int32_t bufferCount, 139 int32_t bufferSize); 140 141 bool AllocateBufferOnPort(sptr<ICodecComponent> component, enum PortIndex port, int32_t bufferCount, 142 int32_t bufferSize); 143 144 bool FreeBufferOnPort(sptr<ICodecComponent> component, enum PortIndex port); 145 146 int32_t GetPortParameter(sptr<ICodecComponent> component, PortIndex index, OMX_PARAM_PORTDEFINITIONTYPE ¶m); 147 148 bool FillAndEmptyAllBuffer(sptr<ICodecComponent> component, CodecBufferType type); 149 150 bool WaitState(sptr<ICodecComponent> component, CodecStateType objState); 151 152 bool PushAlongParam(OmxCodecBuffer &omxBuffer); 153 154 private: 155 static OHOS::HDI::Display::Buffer::V1_0::IDisplayBuffer *buffer_; 156 CodecVersionType version_; 157 std::map<int32_t, std::shared_ptr<BufferInfo>> inputBuffers_; 158 std::map<int32_t, std::shared_ptr<BufferInfo>> outputBuffers_; 159 }; 160 } // V3_0 161 } // Codec 162 } // HDI 163 } // OHOS 164 165 #endif /* CODEC_FUNCTION_UTIL_H */ 166