1 /* 2 * Copyright (c) 2022 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 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 #ifndef CODEC_HDI_ADAPTER_ENCODE_H 16 #define CODEC_HDI_ADAPTER_ENCODE_H 17 18 #include <OMX_Component.h> 19 #include <OMX_Core.h> 20 #include <OMX_VideoExt.h> 21 #include <ashmem.h> 22 #include <buffer_handle.h> 23 #include <buffer_handle_utils.h> 24 #include <idisplay_gralloc.h> 25 #include <condition_variable> 26 #include <deque> 27 #include <list> 28 #include <map> 29 #include <memory> 30 #include <mutex> 31 #include <queue> 32 #include <thread> 33 #include <vector> 34 #include "codec_component_manager.h" 35 #include "codec_component_type.h" 36 #include "command_adapter_parse.h" 37 #include "hdi_mpp.h" 38 39 enum class PortIndex { PORT_INDEX_INPUT = 0, PORT_INDEX_OUTPUT = 1 }; 40 41 class CodecHdiAdapterEncode { 42 struct BufferInfo { 43 std::shared_ptr<struct OmxCodecBuffer> omxBuffer; 44 std::shared_ptr<OHOS::Ashmem> avSharedPtr; 45 int bufferHandleId; 46 PortIndex portIndex; BufferInfoBufferInfo47 BufferInfo() 48 { 49 omxBuffer = nullptr; 50 avSharedPtr = nullptr; 51 portIndex = PortIndex::PORT_INDEX_INPUT; 52 bufferHandleId = -1; 53 } ~BufferInfoBufferInfo54 ~BufferInfo() 55 { 56 omxBuffer = nullptr; 57 if (avSharedPtr) { 58 avSharedPtr->UnmapAshmem(); 59 avSharedPtr->CloseAshmem(); 60 avSharedPtr = nullptr; 61 } 62 portIndex = PortIndex::PORT_INDEX_INPUT; 63 bufferHandleId = -1; 64 } 65 }; 66 67 public: 68 CodecHdiAdapterEncode(); 69 ~CodecHdiAdapterEncode(); 70 71 bool Init(CommandOpt &opt); 72 bool Configure(); 73 bool UseBuffers(); 74 int32_t UseBufferOnPort(PortIndex portIndex); 75 void FreeBuffers(); 76 void Run(); 77 void Release(); 78 static int32_t OnEvent(struct CodecCallbackType *self, OMX_EVENTTYPE event, struct EventInfo *info); 79 80 static int32_t OnEmptyBufferDone( 81 struct CodecCallbackType *self, int64_t appData, const struct OmxCodecBuffer *buffer); 82 static int32_t OnFillBufferDone( 83 struct CodecCallbackType *self, int64_t appData, const struct OmxCodecBuffer *buffer); 84 template <typename T> InitParam(T & param)85 inline void InitParam(T ¶m) 86 { 87 memset_s(¶m, sizeof(param), 0x0, sizeof(param)); 88 param.nSize = sizeof(param); 89 param.nVersion.s.nVersionMajor = 1; 90 } 91 template <typename T> InitParamInOhos(T & param)92 inline void InitParamInOhos(T ¶m) 93 { 94 memset_s(¶m, sizeof(param), 0x0, sizeof(param)); 95 param.size = sizeof(param); 96 param.version.s.nVersionMajor = 1; // mVersion.s.nVersionMajor; 97 } 98 void WaitForStatusChanged(); 99 void OnStatusChanged(); 100 bool ReadOneFrame(FILE *fp, char *buf, uint32_t &filledCount); 101 102 private: 103 int32_t OnEmptyBufferDone(const struct OmxCodecBuffer &buffer); 104 int32_t OnFillBufferDone(const struct OmxCodecBuffer &buffer); 105 int32_t ConfigBitMode(); 106 int32_t UseBufferOnPort(PortIndex portIndex, int bufferCount, int bufferSize); 107 bool FillAllTheBuffer(); 108 int GetFreeBufferId(); 109 int32_t ConfigPortDefine(); 110 int32_t ConfigMppPassthrough(); 111 int32_t ConfigMppExtPassthrough(int32_t codecType); 112 int32_t CheckAndUseBufferHandle(); 113 int32_t UseDynaBuffer(int bufferCount, int bufferSize); 114 bool FillCodecBuffer(std::shared_ptr<BufferInfo> bufferInfo, bool &endFlag); 115 int32_t CreateBufferHandle(); 116 void FreeBufferHandle(); 117 void CalcBpsRange(RKHdiRcSetup *rateControl, int32_t codecType); 118 void SetQpValue(RKHdiRcSetup *rateControl); AlignUp(uint32_t width)119 uint32_t inline AlignUp(uint32_t width) 120 { 121 return (((width) + alignment_ - 1) & (~(alignment_ - 1))); 122 } 123 124 private: 125 FILE *fpIn_; // input file 126 FILE *fpOut_; 127 uint32_t width_; 128 uint32_t height_; 129 uint32_t stride_; 130 uint32_t srcFileSize_; 131 uint32_t totalSrcSize_; 132 struct CodecComponentType *client_; 133 struct CodecCallbackType *callback_; 134 struct CodecComponentManager *omxMgr_; 135 uint32_t componentId_; 136 std::map<int, std::shared_ptr<BufferInfo>> omxBuffers_; // key is bufferID 137 std::list<int> unUsedInBuffers_; 138 std::list<int> unUsedOutBuffers_; 139 std::mutex lockInputBuffers_; 140 std::condition_variable statusCondition_; 141 std::mutex statusLock_; 142 bool exit_; 143 std::map<int, BufferHandle *> bufferHandles_; 144 std::list<int> freeBufferHandles_; 145 bool useBufferHandle_; 146 static constexpr uint32_t alignment_ = 16; 147 static OHOS::HDI::Display::V1_0::IDisplayGralloc *gralloc_; 148 }; 149 150 #endif // CODEC_HDI_ADAPTER_ENCODE_H 151