1 /* 2 * Copyright (C) 2023 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 HCODEC_HDECODER_H 17 #define HCODEC_HDECODER_H 18 19 #include "hcodec.h" 20 #include "v1_0/buffer_handle_meta_key_type.h" 21 #ifdef USE_VIDEO_PROCESSING_ENGINE 22 #include "video_refreshrate_prediction.h" 23 #endif 24 25 namespace OHOS::MediaAVCodec { 26 class HDecoder : public HCodec { 27 public: HDecoder(CodecHDI::CodecCompCapability caps,OMX_VIDEO_CODINGTYPE codingType)28 HDecoder(CodecHDI::CodecCompCapability caps, OMX_VIDEO_CODINGTYPE codingType) 29 : HCodec(caps, codingType, false) {} 30 ~HDecoder() override; 31 32 private: 33 struct SurfaceBufferItem { 34 sptr<SurfaceBuffer> buffer; 35 sptr<SyncFence> fence; 36 int32_t generation = 0; 37 bool hasSwapedOut = false; 38 }; 39 40 private: 41 // configure 42 int32_t OnConfigure(const Format &format) override; 43 int32_t SetupPort(const Format &format); 44 int32_t UpdateInPortFormat() override; 45 int32_t UpdateOutPortFormat() override; 46 void UpdateColorAspects() override; 47 void GetCropFromOmx(uint32_t w, uint32_t h, OHOS::Rect& damage); 48 int32_t RegisterListenerToSurface(const sptr<Surface> &surface); 49 void OnSetOutputSurface(const MsgInfo &msg, BufferOperationMode mode) override; 50 int32_t OnSetOutputSurfaceWhenCfg(const sptr<Surface> &surface); 51 int32_t OnSetParameters(const Format &format) override; 52 bool UpdateConfiguredFmt(OMX_COLOR_FORMATTYPE portFmt); 53 uint64_t GetProducerUsage(); 54 void CombineConsumerUsage(); 55 int32_t SaveTransform(const Format &format, bool set = false); 56 int32_t SetTransform(); 57 int32_t SaveScaleMode(const Format &format, bool set = false); 58 int32_t SetScaleMode(); 59 60 // start 61 bool UseHandleOnOutputPort(bool isDynamic); 62 int32_t AllocateBuffersOnPort(OMX_DIRTYPE portIndex) override; 63 void UpdateFmtFromSurfaceBuffer() override; 64 int32_t AllocOutDynamicSurfaceBuf(); 65 int32_t AllocateOutputBuffersFromSurface(); 66 int32_t ClearSurfaceAndSetQueueSize(const sptr<Surface> &surface, uint32_t targetSize); 67 int32_t SubmitAllBuffersOwnedByUs() override; 68 int32_t SubmitOutBufToOmx() override; 69 bool ReadyToStart() override; 70 71 // input buffer circulation 72 void OnOMXEmptyBufferDone(uint32_t bufferId, BufferOperationMode mode) override; 73 74 // output buffer circulation 75 void BeforeCbOutToUser(BufferInfo &info) override; 76 void ProcSurfaceBufferToUser(const sptr<SurfaceBuffer>& buffer) override; 77 void ProcAVBufferToUser(std::shared_ptr<AVBuffer> avBuffer, 78 std::shared_ptr<CodecHDI::OmxCodecBuffer> omxBuffer) override; 79 80 void OnReleaseOutputBuffer(const BufferInfo &info) override; 81 void OnRenderOutputBuffer(const MsgInfo &msg, BufferOperationMode mode) override; 82 int32_t NotifySurfaceToRenderOutputBuffer(BufferInfo &info); 83 int32_t Attach(BufferInfo &info); 84 void OnGetBufferFromSurface(const ParamSP& param) override; 85 SurfaceBufferItem RequestBuffer(); 86 std::vector<BufferInfo>::iterator FindBelongTo(sptr<SurfaceBuffer>& buffer); 87 std::vector<BufferInfo>::iterator FindNullSlotIfDynamicMode(); 88 void SurfaceModeSubmitBuffer(); 89 void SurfaceModeSubmitBufferFromFreeList(); 90 bool SurfaceModeSubmitOneItem(SurfaceBufferItem& item); 91 void DynamicModeSubmitBuffer() override; 92 void DynamicModeSubmitIfEos() override; 93 void DynamicModeSubmitBufferToSlot(sptr<SurfaceBuffer>& buffer, std::vector<BufferInfo>::iterator nullSlot); 94 void DynamicModeSubmitBufferToSlot(std::vector<BufferInfo>::iterator nullSlot); 95 void SubmitBuffersToNextOwner() override; 96 97 // switch surface 98 void OnSetOutputSurfaceWhenRunning(const sptr<Surface> &newSurface, 99 const MsgInfo &msg, BufferOperationMode mode); 100 void SwitchBetweenSurface(const sptr<Surface> &newSurface, 101 const MsgInfo &msg, BufferOperationMode mode); 102 void ConsumeFreeList(BufferOperationMode mode); 103 void ClassifyOutputBufferOwners(std::vector<size_t>& ownedByUs, 104 std::map<int64_t, size_t>& ownedBySurfaceFlushTime2BufferIndex); 105 106 // stop/release 107 void EraseBufferFromPool(OMX_DIRTYPE portIndex, size_t i) override; 108 void OnClearBufferPool(OMX_DIRTYPE portIndex) override; 109 void OnEnterUninitializedState() override; 110 111 // VRR 112 int32_t SetVrrEnable(const Format &format); 113 114 // swap dma buffer 115 bool CanSwapOut(OMX_DIRTYPE portIndex, BufferInfo& info); 116 int32_t SwapInBufferByPortIndex(OMX_DIRTYPE portIndex); 117 int32_t SwapOutBufferByPortIndex(OMX_DIRTYPE portIndex); 118 #ifdef USE_VIDEO_PROCESSING_ENGINE 119 int32_t VrrPrediction(BufferInfo &info) override; 120 int32_t InitVrr(); 121 static constexpr double VRR_DEFAULT_INPUT_FRAME_RATE = 60.0; 122 using VrrCreate = Media::VideoProcessingEngine::VideoRefreshRatePredictionHandle* (*)(); 123 using VrrDestroy = void (*)(Media::VideoProcessingEngine::VideoRefreshRatePredictionHandle*); 124 using VrrCheckSupport = int32_t (*)(Media::VideoProcessingEngine::VideoRefreshRatePredictionHandle*, 125 const char *processName); 126 using VrrProcess = void (*)(Media::VideoProcessingEngine::VideoRefreshRatePredictionHandle*, 127 OH_NativeBuffer*, int32_t, int32_t); 128 VrrCreate VrrCreateFunc_ = nullptr; 129 VrrDestroy VrrDestroyFunc_ = nullptr; 130 VrrCheckSupport VrrCheckSupportFunc_ = nullptr; 131 VrrProcess VrrProcessFunc_ = nullptr; 132 void *vpeHandle_ = nullptr; 133 bool vrrDynamicSwitch_ = false; 134 Media::VideoProcessingEngine::VideoRefreshRatePredictionHandle* vrrHandle_ = nullptr; 135 #endif 136 137 // Lpp target pts 138 int32_t SetLppTargetPts(const Format &format); 139 // freeze 140 int32_t FreezeBuffers() override; 141 int32_t ActiveBuffers() override; 142 int32_t DecreaseFreq() override; 143 int32_t RecoverFreq() override; 144 145 private: 146 static constexpr uint64_t SURFACE_MODE_PRODUCER_USAGE = BUFFER_USAGE_MEM_DMA | BUFFER_USAGE_VIDEO_DECODER; 147 static constexpr uint64_t BUFFER_MODE_REQUEST_USAGE = 148 SURFACE_MODE_PRODUCER_USAGE | BUFFER_USAGE_CPU_READ | BUFFER_USAGE_MEM_MMZ_CACHE; 149 uint64_t cfgedConsumerUsage = 0; 150 151 struct SurfaceItem { 152 SurfaceItem() = default; 153 SurfaceItem(const sptr<Surface> &surface, std::string codecId, int32_t instanceId); 154 void Release(bool cleanAll = false); 155 sptr<Surface> surface_; 156 private: 157 std::optional<GraphicTransformType> originalTransform_; 158 std::string compUniqueStr_; 159 int32_t instanceId_; 160 } currSurface_; 161 162 std::list<SurfaceBufferItem> freeList_; 163 int32_t currGeneration_ = 0; 164 bool isDynamic_ = false; 165 uint32_t outBufferCnt_ = 0; 166 GraphicTransformType transform_ = GRAPHIC_ROTATE_NONE; 167 std::optional<ScalingMode> scaleMode_; 168 OHOS::HDI::Display::Graphic::Common::V1_0::BufferHandleMetaRegion crop_{0}; 169 }; 170 } // namespace OHOS::MediaAVCodec 171 #endif // HCODEC_HDECODER_H 172