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