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     struct SurfaceBufferItem {
33         sptr<SurfaceBuffer> buffer;
34         sptr<SyncFence> fence;
35         int32_t generation = 0;
36     };
37 
38 private:
39     // configure
40     int32_t OnConfigure(const Format &format) override;
41     int32_t SetupPort(const Format &format);
42     int32_t UpdateInPortFormat() override;
43     int32_t UpdateOutPortFormat() override;
44     void UpdateColorAspects() override;
45     void GetCropFromOmx(uint32_t w, uint32_t h, OHOS::Rect& damage);
46     int32_t RegisterListenerToSurface(const sptr<Surface> &surface);
47     void OnSetOutputSurface(const MsgInfo &msg, BufferOperationMode mode) override;
48     int32_t OnSetOutputSurfaceWhenCfg(const sptr<Surface> &surface);
49     int32_t OnSetParameters(const Format &format) override;
50     bool UpdateConfiguredFmt(OMX_COLOR_FORMATTYPE portFmt);
51     uint64_t GetProducerUsage();
52     void CombineConsumerUsage();
53     int32_t SaveTransform(const Format &format, bool set = false);
54     int32_t SetTransform();
55     int32_t SaveScaleMode(const Format &format, bool set = false);
56     int32_t SetScaleMode();
57 
58     // start
59     bool UseHandleOnOutputPort(bool isDynamic);
60     int32_t AllocateBuffersOnPort(OMX_DIRTYPE portIndex) override;
61     void SetCallerToBuffer(int fd) override;
62     void UpdateFormatFromSurfaceBuffer() override;
63     int32_t AllocOutDynamicSurfaceBuf();
64     int32_t AllocateOutputBuffersFromSurface();
65     int32_t SetQueueSize(const sptr<Surface> &surface, uint32_t targetSize);
66     __attribute__((no_sanitize("cfi"))) int32_t SubmitAllBuffersOwnedByUs() override;
67     int32_t SubmitOutputBuffersToOmxNode() override;
68     bool ReadyToStart() override;
69 
70     // input buffer circulation
71     void OnOMXEmptyBufferDone(uint32_t bufferId, BufferOperationMode mode) override;
72 
73     // output buffer circulation
74     void OnReleaseOutputBuffer(const BufferInfo &info) override;
75     void OnRenderOutputBuffer(const MsgInfo &msg, BufferOperationMode mode) override;
76     int32_t NotifySurfaceToRenderOutputBuffer(BufferInfo &info);
77     GSError OnBufferReleasedByConsumer(uint64_t surfaceId) override;
78     void OnGetBufferFromSurface(const ParamSP& param) override;
79     SurfaceBufferItem RequestBuffer();
80     std::vector<BufferInfo>::iterator FindBelongTo(sptr<SurfaceBuffer>& buffer);
81     void SurfaceModeSubmitBuffer();
82     void SurfaceModeSubmitBufferFromFreeList();
83     bool SurfaceModeSubmitOneItem(SurfaceBufferItem& item);
84     void DynamicModeSubmitBuffer() override;
85     void SurfaceDynamicModeSubmitBuffer(std::vector<BufferInfo>::iterator nullSlot);
86     void BufferDynamicModeSubmitBuffer(std::vector<BufferInfo>::iterator nullSlot);
87 
88     // switch surface
89     void OnSetOutputSurfaceWhenRunning(const sptr<Surface> &newSurface,
90         const MsgInfo &msg, BufferOperationMode mode);
91     void SwitchBetweenSurface(const sptr<Surface> &newSurface,
92         const MsgInfo &msg, BufferOperationMode mode);
93     void ConsumeFreeList(BufferOperationMode mode);
94 
95     // stop/release
96     void EraseBufferFromPool(OMX_DIRTYPE portIndex, size_t i) override;
97     void OnClearBufferPool(OMX_DIRTYPE portIndex) override;
98     void CancelBufferToSurface(BufferInfo &info);
99     void OnEnterUninitializedState() override;
100 
101     // VRR
102 #ifdef USE_VIDEO_PROCESSING_ENGINE
103     int32_t SetVrrEnable(const Format &format);
104     int32_t VrrPrediction(BufferInfo &info) override;
105     static constexpr double VRR_DEFAULT_INPUT_FRAME_RATE = 60.0;
106     std::shared_ptr<OHOS::Media::VideoProcessingEngine::VideoRefreshRatePrediction> vrrPredictor_;
107 #endif
108 
109 private:
110     static constexpr uint64_t SURFACE_MODE_PRODUCER_USAGE = BUFFER_USAGE_MEM_DMA | BUFFER_USAGE_VIDEO_DECODER;
111     static constexpr uint64_t BUFFER_MODE_REQUEST_USAGE =
112         SURFACE_MODE_PRODUCER_USAGE | BUFFER_USAGE_CPU_READ | BUFFER_USAGE_MEM_MMZ_CACHE;
113 
114     struct SurfaceItem {
115         SurfaceItem() = default;
116         explicit SurfaceItem(const sptr<Surface> &surface);
117         void Release();
118         sptr<Surface> surface_;
119     private:
120         std::optional<GraphicTransformType> originalTransform_;
121     } currSurface_;
122 
123     std::list<SurfaceBufferItem> freeList_;
124     int32_t currGeneration_ = 0;
125     bool isDynamic_ = false;
126     uint32_t outBufferCnt_ = 0;
127     GraphicTransformType transform_ = GRAPHIC_ROTATE_NONE;
128     std::optional<ScalingMode> scaleMode_;
129     double lastFlushRate_ = 0.0;
130     double codecRate_ = 0.0;
131 };
132 } // namespace OHOS::MediaAVCodec
133 #endif // HCODEC_HDECODER_H
134