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 16 #ifndef CODEC_HDI_ADAPTER_DECODE_H 17 #define CODEC_HDI_ADAPTER_DECODE_H 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 <condition_variable> 25 #include <deque> 26 #include <list> 27 #include <map> 28 #include <memory> 29 #include <mutex> 30 #include <queue> 31 #include <thread> 32 #include <vector> 33 #include "codec_component_manager.h" 34 #include "codec_component_type.h" 35 #include "command_adapter_parse.h" 36 37 enum class PortIndex { PORT_INDEX_INPUT = 0, PORT_INDEX_OUTPUT = 1 }; 38 39 class CodecHdiAdapterDecode { 40 struct BufferInfo { 41 std::shared_ptr<OmxCodecBuffer> omxBuffer; 42 std::shared_ptr<OHOS::Ashmem> avSharedPtr; 43 PortIndex portIndex; 44 BufferHandle *bufferHandle; BufferInfoBufferInfo45 BufferInfo() 46 { 47 omxBuffer = nullptr; 48 avSharedPtr = nullptr; 49 portIndex = PortIndex::PORT_INDEX_INPUT; 50 bufferHandle = nullptr; 51 } ~BufferInfoBufferInfo52 ~BufferInfo() 53 { 54 omxBuffer = nullptr; 55 if (avSharedPtr != nullptr) { 56 avSharedPtr->UnmapAshmem(); 57 avSharedPtr->CloseAshmem(); 58 avSharedPtr = nullptr; 59 } 60 portIndex = PortIndex::PORT_INDEX_INPUT; 61 } 62 }; 63 64 public: 65 CodecHdiAdapterDecode(); 66 ~CodecHdiAdapterDecode(); 67 bool Init(CommandOpt &opt); 68 bool Configure(); 69 bool UseBuffers(); 70 void FreeBuffers(); 71 void start(); 72 void Run(); 73 void Release(); 74 static int32_t OnEvent(struct CodecCallbackType *self, OMX_EVENTTYPE event, struct EventInfo *info); 75 static int32_t OnEmptyBufferDone( 76 struct CodecCallbackType *self, int64_t appData, const struct OmxCodecBuffer *buffer); 77 static int32_t OnFillBufferDone( 78 struct CodecCallbackType *self, int64_t appData, const struct OmxCodecBuffer *buffer); 79 template <typename T> InitParam(T & param)80 inline void InitParam(T ¶m) 81 { 82 memset_s(¶m, sizeof(param), 0x0, sizeof(param)); 83 param.nSize = sizeof(param); 84 param.nVersion.s.nVersionMajor = 1; // mVersion.s.nVersionMajor; 85 } 86 template <typename T> InitParamInOhos(T & param)87 inline void InitParamInOhos(T ¶m) 88 { 89 memset_s(¶m, sizeof(param), 0x0, sizeof(param)); 90 param.size = sizeof(param); 91 param.version.s.nVersionMajor = 1; // mVersion.s.nVersionMajor; 92 } 93 void WaitForStatusChanged(); 94 void OnStatusChanged(); 95 bool ReadOnePacket(FILE *fp, uint8_t *buf, uint32_t &filledCount); 96 bool ReadOneFrameFromFile(FILE *fp, uint8_t *buf, uint32_t &filledCount); 97 void DumpOutputToFile(FILE *fp, uint8_t *addr); 98 99 private: 100 int32_t UseBufferOnPort(PortIndex portIndex); 101 int32_t UseBufferOnPort(PortIndex portIndex, int bufferCount, int bufferSize); 102 int32_t OnEmptyBufferDone(const struct OmxCodecBuffer &buffer); 103 int32_t OnFillBufferDone(const struct OmxCodecBuffer &buffer); 104 int32_t CheckAndUseBufferHandle(); 105 int GetYuvSize(); 106 int32_t ConfigPortDefine(); 107 int32_t ConfigMppPassthrough(); 108 int GetFreeBufferId(); AlignUp(uint32_t width)109 uint32_t inline AlignUp(uint32_t width) 110 { 111 return (((width) + alignment_ - 1) & (~(alignment_ - 1))); 112 } 113 114 private: 115 FILE *fpIn_; // input file 116 FILE *fpOut_; 117 uint32_t width_; 118 uint32_t height_; 119 uint32_t stride_; 120 uint32_t inputBufferSize_; 121 uint32_t needSplit_; 122 uint32_t srcFileSize_; 123 uint32_t totalSrcSize_; 124 struct CodecComponentType *client_; 125 struct CodecCallbackType *callback_; 126 struct CodecComponentManager *omxMgr_; 127 uint32_t componentId_; 128 std::map<int, std::shared_ptr<BufferInfo>> omxBuffers_; // key is buferid 129 std::list<int> unUsedInBuffers_; 130 std::list<int> unUsedOutBuffers_; 131 std::mutex lockInputBuffers_; 132 std::condition_variable statusCondition_; 133 std::mutex statusLock_; 134 bool exit_; 135 codecMime codecMime_; 136 bool useBufferHandle_; 137 int count_; 138 static constexpr uint32_t alignment_ = 16; 139 }; 140 #endif /* CODEC_HDI_ADAPTER_DECODE_H */ 141