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_HDI_ENCODE_H 17 #define CODEC_HDI_ENCODE_H 18 19 #include <condition_variable> 20 #include <list> 21 #include <map> 22 #include <memory> 23 #include <mutex> 24 #include <ashmem.h> 25 #include <buffer_handle.h> 26 #include <OMX_Component.h> 27 #include <OMX_Core.h> 28 #include <OMX_VideoExt.h> 29 #include <securec.h> 30 #include "codec_hdi_callback.h" 31 #include "codec_utils.h" 32 #include "codec_omx_ext.h" 33 #include "command_parse.h" 34 #include "hdf_log.h" 35 #include "sys/mman.h" 36 #include "v2_0/codec_types.h" 37 #include "v2_0/icodec_callback.h" 38 #include "v2_0/icodec_component.h" 39 #include "v2_0/icodec_component_manager.h" 40 #include "v1_0/include/idisplay_buffer.h" 41 42 using OHOS::HDI::Codec::V2_0::OmxCodecBuffer; 43 class CodecHdiEncode : public ICodecHdiCallBackBase, 44 public std::enable_shared_from_this<CodecHdiEncode> { 45 enum class PortIndex { PORT_INDEX_INPUT = 0, PORT_INDEX_OUTPUT = 1 }; 46 struct BufferInfo { 47 std::shared_ptr<OmxCodecBuffer> omxBuffer; 48 std::shared_ptr<OHOS::Ashmem> avSharedPtr; 49 int bufferHandleId; 50 PortIndex portIndex; BufferInfoBufferInfo51 BufferInfo() 52 { 53 omxBuffer = nullptr; 54 avSharedPtr = nullptr; 55 portIndex = PortIndex::PORT_INDEX_INPUT; 56 bufferHandleId = -1; 57 } ~BufferInfoBufferInfo58 ~BufferInfo() 59 { 60 omxBuffer = nullptr; 61 if (avSharedPtr) { 62 avSharedPtr->UnmapAshmem(); 63 avSharedPtr->CloseAshmem(); 64 avSharedPtr = nullptr; 65 } 66 portIndex = PortIndex::PORT_INDEX_INPUT; 67 bufferHandleId = -1; 68 } 69 }; 70 using BufferInfo = struct BufferInfo; 71 72 public: 73 CodecHdiEncode(); 74 virtual ~CodecHdiEncode(); 75 76 bool Init(const CommandOpt &opt); 77 bool Configure(); 78 bool UseBuffers(); 79 int32_t UseBufferOnPort(PortIndex portIndex); 80 void FreeBuffers(); 81 void Run(); 82 void Release(); 83 void WaitForStatusChanged(); 84 void OnStatusChanged(); 85 bool ReadOneFrame(FILE *fp, char *buf, uint32_t &filledCount); 86 int32_t OnEmptyBufferDone(const struct OmxCodecBuffer &buffer) override; 87 int32_t OnFillBufferDone(const struct OmxCodecBuffer &buffer) override; 88 int32_t EventHandler(OHOS::HDI::Codec::V2_0::CodecEventType event, 89 const OHOS::HDI::Codec::V2_0::EventInfo &info) override; 90 91 private: 92 int32_t ConfigBitMode(); 93 int32_t UseBufferOnPort(PortIndex portIndex, int bufferCount, int bufferSize); 94 bool FillAllTheBuffer(); 95 int GetFreeBufferId(); 96 int32_t ConfigPortDefine(); 97 int32_t CheckAndUseBufferHandle(); 98 int32_t GetParameterSupportBufferType(std::vector<int8_t> &inVec, std::vector<int8_t> &outVec); 99 int32_t CheckSupportBufferType(PortIndex portIndex, CodecBufferType codecBufferType); 100 int32_t CheckAndUseDMABuffer(); 101 int32_t UseDMABuffer(PortIndex portIndex, int bufferCount, int bufferSize); 102 int32_t UseDynaBuffer(int bufferCount, int bufferSize); 103 bool FillCodecBuffer(std::shared_ptr<BufferInfo> bufferInfo, bool &endFlag); 104 int32_t CreateBufferHandle(); 105 int32_t GetComponentName(std::string &compName); AlignUp(uint32_t width)106 uint32_t inline AlignUp(uint32_t width) 107 { 108 return (((width) + alignment_ - 1) & (~(alignment_ - 1))); 109 } 110 111 private: 112 FILE *fpIn_; // input file 113 FILE *fpOut_; 114 uint32_t width_; 115 uint32_t height_; 116 uint32_t stride_; 117 OHOS::sptr<OHOS::HDI::Codec::V2_0::ICodecComponent> client_; 118 OHOS::sptr<OHOS::HDI::Codec::V2_0::ICodecCallback> callback_; 119 OHOS::sptr<OHOS::HDI::Codec::V2_0::ICodecComponentManager> omxMgr_; 120 uint32_t componentId_; 121 std::map<int, std::shared_ptr<BufferInfo>> omxBuffers_; // key is bufferID 122 std::map<int, const void *> addrs_; 123 std::list<int> unUsedInBuffers_; 124 std::list<int> unUsedOutBuffers_; 125 std::mutex lockInputBuffers_; 126 std::condition_variable statusCondition_; 127 std::mutex statusLock_; 128 bool exit_; 129 std::map<int, BufferHandle *> bufferHandles_; 130 std::list<int> freeBufferHandles_; 131 bool useBufferHandle_; 132 bool useDMABuffer_; 133 static CodecUtil *util_; 134 static constexpr uint32_t alignment_ = 16; 135 static OHOS::HDI::Display::Buffer::V1_0::IDisplayBuffer *gralloc_; 136 }; 137 138 #endif // CODEC_HDI_ENCODE_H