• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SetCallerToBuffer(int fd) override;
64     void UpdateFormatFromSurfaceBuffer() override;
65     int32_t AllocOutDynamicSurfaceBuf();
66     int32_t AllocateOutputBuffersFromSurface();
67     int32_t ClearSurfaceAndSetQueueSize(const sptr<Surface> &surface, uint32_t targetSize);
68     int32_t SubmitAllBuffersOwnedByUs() override;
69     int32_t SubmitOutputBuffersToOmxNode() override;
70     bool ReadyToStart() override;
71 
72     // input buffer circulation
73     void OnOMXEmptyBufferDone(uint32_t bufferId, BufferOperationMode mode) override;
74 
75     // output buffer circulation
76     void BeforeCbOutToUser(BufferInfo &info) override;
77     void OnReleaseOutputBuffer(const BufferInfo &info) override;
78     void OnRenderOutputBuffer(const MsgInfo &msg, BufferOperationMode mode) override;
79     int32_t NotifySurfaceToRenderOutputBuffer(BufferInfo &info);
80     int32_t Attach(BufferInfo &info);
81     GSError OnBufferReleasedByConsumer(uint64_t surfaceId) override;
82     void OnGetBufferFromSurface(const ParamSP& param) override;
83     SurfaceBufferItem RequestBuffer();
84     std::vector<BufferInfo>::iterator FindBelongTo(sptr<SurfaceBuffer>& buffer);
85     std::vector<BufferInfo>::iterator FindNullSlotIfDynamicMode();
86     void SurfaceModeSubmitBuffer();
87     void SurfaceModeSubmitBufferFromFreeList();
88     bool SurfaceModeSubmitOneItem(SurfaceBufferItem& item);
89     void DynamicModeSubmitBuffer() override;
90     void DynamicModeSubmitIfEos() override;
91     void DynamicModeSubmitBufferToSlot(sptr<SurfaceBuffer>& buffer, std::vector<BufferInfo>::iterator nullSlot);
92     void DynamicModeSubmitBufferToSlot(std::vector<BufferInfo>::iterator nullSlot);
93     void SubmitBuffersToNextOwner() override;
94 
95     // switch surface
96     void OnSetOutputSurfaceWhenRunning(const sptr<Surface> &newSurface,
97         const MsgInfo &msg, BufferOperationMode mode);
98     void SwitchBetweenSurface(const sptr<Surface> &newSurface,
99         const MsgInfo &msg, BufferOperationMode mode);
100     void ConsumeFreeList(BufferOperationMode mode);
101 
102     // stop/release
103     void EraseBufferFromPool(OMX_DIRTYPE portIndex, size_t i) override;
104     void OnClearBufferPool(OMX_DIRTYPE portIndex) override;
105     void OnEnterUninitializedState() override;
106 
107     // VRR
108     int32_t SetVrrEnable(const Format &format);
109 
110     // swap dma buffer
111     bool CanSwapOut(OMX_DIRTYPE portIndex, BufferInfo& info);
112     int32_t SwapInBufferByPortIndex(OMX_DIRTYPE portIndex);
113     int32_t SwapOutBufferByPortIndex(OMX_DIRTYPE portIndex);
114 #ifdef USE_VIDEO_PROCESSING_ENGINE
115     int32_t VrrPrediction(BufferInfo &info) override;
116     int32_t InitVrr();
117     static constexpr double VRR_DEFAULT_INPUT_FRAME_RATE = 60.0;
118     using VrrCreate = Media::VideoProcessingEngine::VideoRefreshRatePredictionHandle* (*)();
119     using VrrDestroy = void (*)(Media::VideoProcessingEngine::VideoRefreshRatePredictionHandle*);
120     using VrrCheckSupport = int32_t (*)(Media::VideoProcessingEngine::VideoRefreshRatePredictionHandle*,
121         const char *processName);
122     using VrrProcess = void (*)(Media::VideoProcessingEngine::VideoRefreshRatePredictionHandle*,
123         OH_NativeBuffer*, int32_t, int32_t);
124     VrrCreate VrrCreateFunc_ = nullptr;
125     VrrDestroy VrrDestroyFunc_ = nullptr;
126     VrrCheckSupport VrrCheckSupportFunc_ = nullptr;
127     VrrProcess VrrProcessFunc_ = nullptr;
128     void *vpeHandle_ = nullptr;
129     bool vrrDynamicSwitch_ = false;
130     Media::VideoProcessingEngine::VideoRefreshRatePredictionHandle* vrrHandle_ = nullptr;
131 #endif
132     // freeze
133     int32_t FreezeBuffers() override;
134     int32_t ActiveBuffers() override;
135 
136 private:
137     static constexpr uint64_t SURFACE_MODE_PRODUCER_USAGE = BUFFER_USAGE_MEM_DMA | BUFFER_USAGE_VIDEO_DECODER;
138     static constexpr uint64_t BUFFER_MODE_REQUEST_USAGE =
139         SURFACE_MODE_PRODUCER_USAGE | BUFFER_USAGE_CPU_READ | BUFFER_USAGE_MEM_MMZ_CACHE;
140 
141     struct SurfaceItem {
142         SurfaceItem() = default;
143         explicit SurfaceItem(const sptr<Surface> &surface);
144         void Release();
145         sptr<Surface> surface_;
146     private:
147         std::optional<GraphicTransformType> originalTransform_;
148     } currSurface_;
149 
150     std::list<SurfaceBufferItem> freeList_;
151     int32_t currGeneration_ = 0;
152     bool isDynamic_ = false;
153     uint32_t outBufferCnt_ = 0;
154     GraphicTransformType transform_ = GRAPHIC_ROTATE_NONE;
155     std::optional<ScalingMode> scaleMode_;
156     double codecRate_ = 0.0;
157     OHOS::HDI::Display::Graphic::Common::V1_0::BufferHandleMetaRegion crop_{0};
158 };
159 } // namespace OHOS::MediaAVCodec
160 #endif // HCODEC_HDECODER_H
161