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