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