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 21 namespace OHOS::MediaAVCodec { 22 class HDecoder : public HCodec { 23 public: HDecoder(CodecHDI::CodecCompCapability caps,OMX_VIDEO_CODINGTYPE codingType)24 HDecoder(CodecHDI::CodecCompCapability caps, OMX_VIDEO_CODINGTYPE codingType) 25 : HCodec(caps, codingType, false) {} 26 ~HDecoder() override; 27 28 private: 29 // configure 30 int32_t OnConfigure(const Format &format) override; 31 int32_t SetupPort(const Format &format); 32 int32_t UpdateInPortFormat() override; 33 int32_t UpdateOutPortFormat() override; 34 void UpdateColorAspects() override; 35 void GetCropFromOmx(uint32_t w, uint32_t h, OHOS::Rect& damage); 36 int32_t RegisterListenerToSurface(const sptr<Surface> &surface); 37 int32_t OnSetOutputSurface(const sptr<Surface> &surface, bool cfg) override; 38 int32_t OnSetOutputSurfaceWhenCfg(const sptr<Surface> &surface); 39 int32_t OnSetParameters(const Format &format) override; 40 bool UpdateConfiguredFmt(OMX_COLOR_FORMATTYPE portFmt); 41 uint64_t GetProducerUsage(); 42 void CombineConsumerUsage(); 43 int32_t SaveTransform(const Format &format, bool set = false); 44 int32_t SetTransform(); 45 int32_t SaveScaleMode(const Format &format, bool set = false); 46 int32_t SetScaleMode(); 47 48 // start 49 bool UseHandleOnOutputPort(bool isDynamic); 50 int32_t AllocateBuffersOnPort(OMX_DIRTYPE portIndex) override; 51 void SetCallerToBuffer(int fd) override; 52 void UpdateFormatFromSurfaceBuffer() override; 53 int32_t AllocOutDynamicSurfaceBuf(); 54 int32_t AllocateOutputBuffersFromSurface(); 55 int32_t SetQueueSize(const sptr<Surface> &surface, uint32_t targetSize); 56 __attribute__((no_sanitize("cfi"))) int32_t SubmitAllBuffersOwnedByUs() override; 57 int32_t SubmitOutputBuffersToOmxNode() override; 58 bool ReadyToStart() override; 59 60 // input buffer circulation 61 void OnOMXEmptyBufferDone(uint32_t bufferId, BufferOperationMode mode) override; 62 63 // output buffer circulation 64 void OnReleaseOutputBuffer(const BufferInfo &info) override; 65 void OnRenderOutputBuffer(const MsgInfo &msg, BufferOperationMode mode) override; 66 int32_t NotifySurfaceToRenderOutputBuffer(BufferInfo &info); 67 GSError OnBufferReleasedByConsumer(uint64_t surfaceId) override; 68 void OnGetBufferFromSurface(const ParamSP& param) override; 69 bool RequestAndFindBelongTo( 70 sptr<SurfaceBuffer>& buffer, sptr<SyncFence>& fence, std::vector<BufferInfo>::iterator& iter); 71 __attribute__((no_sanitize("cfi"))) void SubmitDynamicBufferIfPossible() override; 72 73 // switch surface 74 int32_t OnSetOutputSurfaceWhenRunning(const sptr<Surface> &newSurface); 75 int32_t SwitchBetweenSurface(const sptr<Surface> &newSurface); 76 77 // stop/release 78 void EraseBufferFromPool(OMX_DIRTYPE portIndex, size_t i) override; 79 void OnClearBufferPool(OMX_DIRTYPE portIndex) override; 80 void CancelBufferToSurface(BufferInfo &info); 81 void OnEnterUninitializedState() override; 82 83 private: 84 static constexpr uint64_t SURFACE_MODE_PRODUCER_USAGE = BUFFER_USAGE_MEM_DMA | BUFFER_USAGE_VIDEO_DECODER; 85 static constexpr uint64_t BUFFER_MODE_REQUEST_USAGE = 86 SURFACE_MODE_PRODUCER_USAGE | BUFFER_USAGE_CPU_READ | BUFFER_USAGE_MEM_MMZ_CACHE; 87 88 struct SurfaceItem { 89 SurfaceItem() = default; 90 explicit SurfaceItem(const sptr<Surface> &surface); 91 void Release(); 92 sptr<Surface> surface_; 93 private: 94 std::optional<GraphicTransformType> originalTransform_; 95 } currSurface_; 96 97 bool isDynamic_ = false; 98 uint32_t outBufferCnt_ = 0; 99 GraphicTransformType transform_ = GRAPHIC_ROTATE_NONE; 100 std::optional<ScalingMode> scaleMode_; 101 double lastFlushRate_ = 0.0; 102 double codecRate_ = 0.0; 103 }; 104 } // namespace OHOS::MediaAVCodec 105 #endif // HCODEC_HDECODER_H 106