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 73 enum class Owner { 74 OWNED_BY_US, 75 OWNED_BY_CODEC, 76 OWNED_BY_USER, 77 OWNED_BY_SURFACE, 78 }; 79 80 std::shared_ptr<AVBuffer> avBuffer = nullptr; 81 std::shared_ptr<FSurfaceMemory> sMemory = nullptr; 82 std::atomic<Owner> owner_ = Owner::OWNED_BY_US; 83 int32_t width = 0; 84 int32_t height = 0; 85 int32_t bitDepth = BIT_DEPTH8BIT; 86 }; 87 88 private: 89 int32_t Initialize(); 90 91 using CreateHevcDecoderFuncType = INT32 (*)(HEVC_DEC_HANDLE *phDecoder, HEVC_DEC_INIT_PARAM *pstInitParam); 92 using DecodeFuncType = INT32 (*)(HEVC_DEC_HANDLE hDecoder, HEVC_DEC_INARGS *pstInArgs, 93 HEVC_DEC_OUTARGS *pstOutArgs); 94 using FlushFuncType = INT32 (*)(HEVC_DEC_HANDLE hDecoder, HEVC_DEC_OUTARGS *pstOutArgs); 95 using DeleteFuncType = INT32 (*)(HEVC_DEC_HANDLE hDecoder); 96 97 enum struct State : int32_t { 98 UNINITIALIZED, 99 INITIALIZED, 100 CONFIGURED, 101 STOPPING, 102 RUNNING, 103 FLUSHED, 104 FLUSHING, 105 EOS, 106 ERROR, 107 }; 108 109 enum PixelBitDepth : int32_t { 110 BIT_DEPTH8BIT = 8, 111 BIT_DEPTH10BIT = 10, 112 }; 113 114 #ifdef BUILD_ENG_VERSION 115 void OpenDumpFile(); 116 void DumpOutputBuffer(int32_t bitDepth); 117 void DumpConvertOut(struct SurfaceInfo &surfaceInfo); 118 #endif 119 bool IsActive() const; 120 void CalculateBufferSize(); 121 int32_t AllocateBuffers(); 122 void InitBuffers(); 123 void ResetBuffers(); 124 void ResetData(); 125 void ReleaseBuffers(); 126 void StopThread(); 127 void ReleaseResource(); 128 int32_t UpdateOutputBuffer(uint32_t index); 129 int32_t UpdateSurfaceMemory(uint32_t index); 130 void SendFrame(); 131 void ConfigureSurface(const Format &format, const std::string_view &formatKey, FormatDataType formatType); 132 void ConfigureDefaultVal(const Format &format, const std::string_view &formatKey, int32_t minVal = 0, 133 int32_t maxVal = INT_MAX); 134 void FindAvailIndex(uint32_t index); 135 void FramePostProcess(std::shared_ptr<HBuffer> &frameBuffer, uint32_t index, int32_t status, int ret); 136 int32_t AllocateInputBuffer(int32_t bufferCnt, int32_t inBufferSize); 137 int32_t AllocateOutputBuffer(int32_t bufferCnt); 138 int32_t FillFrameBuffer(const std::shared_ptr<HBuffer> &frameBuffer); 139 int32_t CheckFormatChange(uint32_t index, int width, int height, int bitDepth); 140 void SetSurfaceParameter(const Format &format, const std::string_view &formatKey, FormatDataType formatType); 141 int32_t ReplaceOutputSurfaceWhenRunning(sptr<Surface> newSurface); 142 int32_t SetQueueSize(const sptr<Surface> &surface, uint32_t targetSize); 143 int32_t SwitchBetweenSurface(const sptr<Surface> &newSurface); 144 int32_t RenderNewSurfaceWithOldBuffer(const sptr<Surface> &newSurface, uint32_t index); 145 int32_t FlushSurfaceMemory(std::shared_ptr<FSurfaceMemory> &surfaceMemory, uint32_t index); 146 int32_t GetSurfaceBufferStride(const std::shared_ptr<HBuffer> &frameBuffer); 147 int32_t SetSurfaceCfg(int32_t bufferCnt); 148 int32_t DecodeFrameOnce(); 149 void HevcFuncMatch(); 150 void ReleaseHandle(); 151 void InitHevcParams(); 152 void ConvertDecOutToAVFrame(int32_t bitDepth); 153 static int32_t CheckHevcDecLibStatus(); 154 // surface listener callback 155 void RequestBufferFromConsumer(); 156 GSError BufferReleasedByConsumer(uint64_t surfaceId); 157 GSError RegisterListenerToSurface(const sptr<Surface> &surface); 158 int32_t UnRegisterListenerToSurface(const sptr<Surface> &surface); 159 160 std::string codecName_; 161 std::atomic<State> state_ = State::UNINITIALIZED; 162 163 void* handle_ = nullptr; 164 uint32_t decInstanceID_; 165 HEVC_DEC_INIT_PARAM initParams_; 166 HEVC_DEC_INARGS hevcDecoderInputArgs_; 167 HEVC_DEC_OUTARGS hevcDecoderOutpusArgs_; 168 HEVC_DEC_HANDLE hevcSDecoder_ = nullptr; 169 CreateHevcDecoderFuncType hevcDecoderCreateFunc_ = nullptr; 170 DecodeFuncType hevcDecoderDecodecFrameFunc_ = nullptr; 171 FlushFuncType hevcDecoderFlushFrameFunc_ = nullptr; 172 DeleteFuncType hevcDecoderDeleteFunc_ = nullptr; 173 174 static std::mutex decoderCountMutex_; 175 static std::vector<uint32_t> decInstanceIDSet_; 176 static std::vector<uint32_t> freeIDSet_; 177 178 Format format_; 179 int32_t width_ = 0; 180 int32_t height_ = 0; 181 int32_t inputBufferSize_ = 0; 182 SurfaceControl sInfo_; 183 int32_t bitDepth_ = 8; 184 // // Receive frame 185 std::shared_ptr<AVFrame> cachedFrame_ = nullptr; 186 uint8_t *scaleData_[AV_NUM_DATA_POINTERS] = {nullptr}; 187 int32_t scaleLineSize_[AV_NUM_DATA_POINTERS] = {0}; 188 std::shared_ptr<Scale> scale_ = nullptr; 189 bool isConverted_ = false; 190 bool isOutBufSetted_ = false; 191 VideoPixelFormat outputPixelFmt_ = VideoPixelFormat::UNKNOWN; 192 // // Running 193 std::vector<std::shared_ptr<HBuffer>> buffers_[2]; 194 std::vector<std::shared_ptr<AVBuffer>> outAVBuffer4Surface_; 195 std::shared_ptr<BlockQueue<uint32_t>> inputAvailQue_; 196 std::shared_ptr<BlockQueue<uint32_t>> codecAvailQue_; 197 std::shared_ptr<BlockQueue<uint32_t>> renderAvailQue_; 198 std::map<uint32_t, std::pair<sptr<SurfaceBuffer>, OHOS::BufferFlushConfig>> renderSurfaceBufferMap_; 199 sptr<Surface> surface_ = nullptr; 200 std::shared_ptr<TaskThread> sendTask_ = nullptr; 201 std::mutex outputMutex_; 202 std::mutex decRunMutex_; 203 std::mutex surfaceMutex_; 204 std::shared_ptr<MediaCodecCallback> callback_; 205 std::atomic<bool> isSendEos_ = false; 206 std::atomic<bool> isBufferAllocated_ = false; 207 #ifdef BUILD_ENG_VERSION 208 std::shared_ptr<std::ofstream> dumpInFile_ = nullptr; 209 std::shared_ptr<std::ofstream> dumpOutFile_ = nullptr; 210 std::shared_ptr<std::ofstream> dumpConvertFile_ = nullptr; 211 #endif 212 }; 213 214 void HevcDecLog(UINT32 channelId, IHW265VIDEO_ALG_LOG_LEVEL eLevel, INT8 *pMsg, ...); 215 } // namespace Codec 216 } // namespace MediaAVCodec 217 } // namespace OHOS 218 #endif // HEVC_DECODER_H 219