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 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 HEVC_DECODER_H 17 #define HEVC_DECODER_H 18 19 #include <atomic> 20 #include <list> 21 #include <map> 22 #include <shared_mutex> 23 #include <functional> 24 #include <fstream> 25 #include <tuple> 26 #include <vector> 27 #include <optional> 28 #include <algorithm> 29 #include "av_common.h" 30 #include "avcodec_common.h" 31 #include "avcodec_info.h" 32 #include "block_queue.h" 33 #include "codec_utils.h" 34 #include "codecbase.h" 35 #include "dma_swap.h" 36 #include "media_description.h" 37 #include "fsurface_memory.h" 38 #include "task_thread.h" 39 #include "surface_tools.h" 40 #include "HevcDec_Typedef.h" 41 42 namespace OHOS { 43 namespace MediaAVCodec { 44 namespace Codec { 45 using AVBuffer = Media::AVBuffer; 46 using AVAllocator = Media::AVAllocator; 47 using AVAllocatorFactory = Media::AVAllocatorFactory; 48 using MemoryFlag = Media::MemoryFlag; 49 using FormatDataType = Media::FormatDataType; 50 class HevcDecoder : public CodecBase, public RefBase { 51 public: 52 explicit HevcDecoder(const std::string &name); 53 ~HevcDecoder() override; 54 int32_t Init(Media::Meta &callerInfo) override; 55 int32_t Configure(const Format &format) override; 56 int32_t Start() override; 57 int32_t Stop() override; 58 int32_t Flush() override; 59 int32_t Reset() override; 60 int32_t Release() override; 61 int32_t SetParameter(const Format &format) override; 62 int32_t GetOutputFormat(Format &format) override; 63 64 int32_t QueueInputBuffer(uint32_t index) override; 65 int32_t ReleaseOutputBuffer(uint32_t index) override; 66 int32_t SetCallback(const std::shared_ptr<MediaCodecCallback> &callback) override; 67 int32_t SetOutputSurface(sptr<Surface> surface) override; 68 int32_t RenderOutputBuffer(uint32_t index) override; 69 int32_t NotifyMemoryRecycle() override; 70 int32_t NotifyMemoryWriteBack() override; 71 static int32_t GetCodecCapability(std::vector<CapabilityData> &capaArray); IsValid()72 bool IsValid() const { return isValid_; } 73 74 struct HBuffer { 75 public: 76 HBuffer() = default; 77 ~HBuffer() = default; 78 std::shared_ptr<AVBuffer> avBuffer = nullptr; 79 std::shared_ptr<FSurfaceMemory> sMemory = nullptr; 80 std::atomic<Owner> owner_ = Owner::OWNED_BY_US; 81 int32_t width = 0; 82 int32_t height = 0; 83 int32_t bitDepth = BIT_DEPTH8BIT; 84 bool hasSwapedOut = false; 85 }; 86 87 private: 88 int32_t Initialize(); 89 90 using CreateHevcDecoderFuncType = INT32 (*)(HEVC_DEC_HANDLE *phDecoder, HEVC_DEC_INIT_PARAM *pstInitParam); 91 using DecodeFuncType = INT32 (*)(HEVC_DEC_HANDLE hDecoder, HEVC_DEC_INARGS *pstInArgs, 92 HEVC_DEC_OUTARGS *pstOutArgs); 93 using FlushFuncType = INT32 (*)(HEVC_DEC_HANDLE hDecoder, HEVC_DEC_OUTARGS *pstOutArgs); 94 using DeleteFuncType = INT32 (*)(HEVC_DEC_HANDLE hDecoder); 95 96 enum struct State : int32_t { 97 UNINITIALIZED, 98 INITIALIZED, 99 CONFIGURED, 100 STOPPING, 101 RUNNING, 102 FLUSHED, 103 FLUSHING, 104 EOS, 105 ERROR, 106 FREEZING, 107 FROZEN, 108 }; 109 110 enum PixelBitDepth : int32_t { 111 BIT_DEPTH8BIT = 8, 112 BIT_DEPTH10BIT = 10, 113 }; 114 115 #ifdef BUILD_ENG_VERSION 116 void OpenDumpFile(); 117 void DumpOutputBuffer(int32_t bitDepth); 118 void DumpConvertOut(struct SurfaceInfo &surfaceInfo); 119 #endif 120 bool IsActive() const; 121 void CalculateBufferSize(); 122 int32_t AllocateBuffers(); 123 void InitBuffers(); 124 void ResetBuffers(); 125 void ResetData(); 126 void ReleaseBuffers(); 127 void StopThread(); 128 void ReleaseResource(); 129 int32_t UpdateOutputBuffer(uint32_t index); 130 int32_t UpdateSurfaceMemory(uint32_t index); 131 void SendFrame(); 132 void ConfigureSurface(const Format &format, const std::string_view &formatKey, FormatDataType formatType); 133 void ConfigureDefaultVal(const Format &format, const std::string_view &formatKey, int32_t minVal = 0, 134 int32_t maxVal = INT_MAX); 135 void FindAvailIndex(uint32_t index); 136 void FramePostProcess(std::shared_ptr<HBuffer> &frameBuffer, uint32_t index, int32_t status, int ret); 137 int32_t AllocateInputBuffer(int32_t bufferCnt, int32_t inBufferSize); 138 int32_t AllocateOutputBuffer(int32_t bufferCnt); 139 int32_t ClearSurfaceAndSetQueueSize(const sptr<Surface> &surface, int32_t bufferCnt); 140 int32_t AllocateOutputBuffersFromSurface(int32_t bufferCnt); 141 int32_t FillFrameBuffer(const std::shared_ptr<HBuffer> &frameBuffer); 142 int32_t CheckFormatChange(uint32_t index, int width, int height, int bitDepth); 143 void SetSurfaceParameter(const Format &format, const std::string_view &formatKey, FormatDataType formatType); 144 int32_t ReplaceOutputSurfaceWhenRunning(sptr<Surface> newSurface); 145 int32_t SetQueueSize(const sptr<Surface> &surface, uint32_t targetSize); 146 int32_t SwitchBetweenSurface(const sptr<Surface> &newSurface); 147 int32_t RenderNewSurfaceWithOldBuffer(const sptr<Surface> &newSurface, uint32_t index); 148 int32_t FlushSurfaceMemory(std::shared_ptr<FSurfaceMemory> &surfaceMemory, uint32_t index); 149 int32_t GetSurfaceBufferStride(const std::shared_ptr<HBuffer> &frameBuffer); 150 int32_t SetSurfaceCfg(); 151 int32_t Attach(sptr<SurfaceBuffer> surfaceBuffer); 152 int32_t Detach(sptr<SurfaceBuffer> surfaceBuffer); 153 void CombineConsumerUsage(); 154 int32_t DecodeFrameOnce(); 155 void HevcFuncMatch(); 156 void ReleaseHandle(); 157 void InitHevcParams(); 158 void ConvertDecOutToAVFrame(int32_t bitDepth); 159 static int32_t CheckHevcDecLibStatus(); 160 // surface listener callback 161 void RequestBufferFromConsumer(); 162 GSError BufferReleasedByConsumer(uint64_t surfaceId); 163 int32_t RegisterListenerToSurface(const sptr<Surface> &surface); 164 void UnRegisterListenerToSurface(const sptr<Surface> &surface); 165 void RequestSurfaceBufferThread(); 166 void StartRequestSurfaceBufferThread(); 167 void StopRequestSurfaceBufferThread(); 168 bool RequestSurfaceBufferOnce(uint32_t index); 169 void SetCallerToBuffer(sptr<SurfaceBuffer> surfaceBuffer); 170 171 // for memory recycle 172 int32_t FreezeBuffers(State curState); 173 int32_t ActiveBuffers(); 174 bool CanSwapOut(bool isOutputBuffer, std::shared_ptr<HBuffer> &hBuffer); 175 int32_t SwapOutBuffers(bool isOutputBuffer, State curState); 176 int32_t SwapInBuffers(bool isOutputBuffer); 177 bool disableDmaSwap_ = false; 178 int pid_ = -1; 179 180 CallerInfo hevcDecInfo_; 181 int32_t instanceId_ = -1; 182 std::string decName_; 183 std::string codecName_; 184 std::atomic<State> state_ = State::UNINITIALIZED; 185 186 void* handle_ = nullptr; 187 uint32_t decInstanceID_; 188 HEVC_DEC_INIT_PARAM initParams_; 189 HEVC_DEC_INARGS hevcDecoderInputArgs_; 190 HEVC_DEC_OUTARGS hevcDecoderOutpusArgs_; 191 HEVC_DEC_HANDLE hevcSDecoder_ = nullptr; 192 CreateHevcDecoderFuncType hevcDecoderCreateFunc_ = nullptr; 193 DecodeFuncType hevcDecoderDecodecFrameFunc_ = nullptr; 194 FlushFuncType hevcDecoderFlushFrameFunc_ = nullptr; 195 DeleteFuncType hevcDecoderDeleteFunc_ = nullptr; 196 197 static std::mutex decoderCountMutex_; 198 static std::vector<uint32_t> decInstanceIDSet_; 199 static std::vector<uint32_t> freeIDSet_; 200 201 bool isValid_ = true; 202 Format format_; 203 int32_t width_ = 0; 204 int32_t height_ = 0; 205 int32_t inputBufferSize_ = 0; 206 int32_t inputBufferCnt_ = 0; 207 int32_t outputBufferCnt_ = 0; 208 SurfaceControl sInfo_; 209 int32_t bitDepth_ = 8; 210 // // Receive frame 211 std::shared_ptr<AVFrame> cachedFrame_ = nullptr; 212 uint8_t *scaleData_[AV_NUM_DATA_POINTERS] = {nullptr}; 213 int32_t scaleLineSize_[AV_NUM_DATA_POINTERS] = {0}; 214 std::shared_ptr<Scale> scale_ = nullptr; 215 bool isConverted_ = false; 216 bool isOutBufSetted_ = false; 217 VideoPixelFormat outputPixelFmt_ = VideoPixelFormat::UNKNOWN; 218 // // Running 219 std::vector<std::shared_ptr<HBuffer>> buffers_[2]; 220 std::vector<std::shared_ptr<AVBuffer>> outAVBuffer4Surface_; 221 std::shared_ptr<BlockQueue<uint32_t>> inputAvailQue_; 222 std::shared_ptr<BlockQueue<uint32_t>> codecAvailQue_; 223 std::shared_ptr<BlockQueue<uint32_t>> renderAvailQue_; 224 std::shared_ptr<BlockQueue<uint32_t>> requestSurfaceBufferQue_; 225 std::map<uint32_t, std::pair<sptr<SurfaceBuffer>, OHOS::BufferFlushConfig>> renderSurfaceBufferMap_; 226 sptr<Surface> surface_ = nullptr; 227 std::shared_ptr<TaskThread> sendTask_ = nullptr; 228 std::mutex outputMutex_; 229 std::mutex decRunMutex_; 230 std::mutex surfaceMutex_; 231 std::mutex requestBufferMutex_; 232 std::mutex renderBufferMapMutex_; 233 std::condition_variable requestBufferCV_; 234 std::condition_variable requestBufferOnceDoneCV_; 235 std::shared_ptr<MediaCodecCallback> callback_; 236 std::atomic<bool> isSendEos_ = false; 237 std::atomic<bool> isBufferAllocated_ = false; 238 std::atomic<bool> requestSucceed_ = false; 239 std::atomic<bool> requestBufferFinished_ = true; 240 std::atomic<bool> requestBufferThreadExit_ = false; 241 std::thread mRequestSurfaceBufferThread_; 242 #ifdef BUILD_ENG_VERSION 243 std::shared_ptr<std::ofstream> dumpInFile_ = nullptr; 244 std::shared_ptr<std::ofstream> dumpOutFile_ = nullptr; 245 std::shared_ptr<std::ofstream> dumpConvertFile_ = nullptr; 246 #endif 247 }; 248 249 void HevcDecLog(UINT32 channelId, IHW265VIDEO_ALG_LOG_LEVEL eLevel, INT8 *pMsg, ...); 250 } // namespace Codec 251 } // namespace MediaAVCodec 252 } // namespace OHOS 253 #endif // HEVC_DECODER_H