• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 AIHDR_ENHANCER_VIDEO_IMPL_H
17 #define AIHDR_ENHANCER_VIDEO_IMPL_H
18 
19 #include <functional>
20 #include <limits>
21 #include <map>
22 #include <mutex>
23 #include <queue>
24 
25 #include "native_window.h"
26 #include "surface.h"
27 #include "sync_fence.h"
28 
29 #include "aihdr_enhancer.h"
30 #include "aihdr_enhancer_video.h"
31 #include "aihdr_enhancer_video_common.h"
32 #include "algorithm_video_common.h"
33 
34 namespace OHOS {
35 namespace Media {
36 namespace VideoProcessingEngine {
37 class AihdrEnhancerVideoImpl : public AihdrEnhancerVideo {
38 public:
39     AihdrEnhancerVideoImpl();
40     ~AihdrEnhancerVideoImpl();
41     int32_t Init();
42     // 北向调用接口
43     int32_t SetCallback(const std::shared_ptr<AihdrEnhancerVideoCallback> &callback) override;
44     int32_t SetOutputSurface(sptr<Surface> surface);
45     sptr<Surface> CreateInputSurface();
46     int32_t SetSurface(const OHNativeWindow* window) override;
47     int32_t GetSurface(OHNativeWindow** window) override;
48     int32_t Configure() override;
49     int32_t Prepare() override;
50     int32_t Start() override;
51     int32_t Stop() override;
52     int32_t Reset() override;
53     int32_t Release() override;
54     int32_t NotifyEos() override;
55     int32_t ReleaseOutputBuffer(uint32_t index, bool render) override;
56     int32_t Flush() override;
57 
58     GSError OnConsumerBufferAvailable();
59     GSError OnProducerBufferReleased();
60 private:
61     struct SurfaceBufferWrapper {
62     public:
63         SurfaceBufferWrapper() = default;
64         ~SurfaceBufferWrapper() = default;
65 
66         sptr<SurfaceBuffer> memory{nullptr};
67         AihdrEnhancerBufferFlag bufferFlag{AIHDR_ENHANCER_BUFFER_FLAG_NONE};
68         sptr<SyncFence> fence{nullptr};
69         int64_t timestamp;
70     };
71     void InitBuffers();
72     bool WaitProcessing();
73     bool AcquireInputOutputBuffers(
74         std::shared_ptr<SurfaceBufferWrapper> &inputBuffer, std::shared_ptr<SurfaceBufferWrapper> &outputBuffer);
75     void DoTask();
76     void OnTriggered();
77     void Process(std::shared_ptr<SurfaceBufferWrapper> inputBuffer, std::shared_ptr<SurfaceBufferWrapper> outputBuffer);
78     int32_t AttachToNewSurface(sptr<Surface> newSurface);
79     int32_t SetOutputSurfaceConfig(sptr<Surface> surface);
80     int32_t SetOutputSurfaceRunning(sptr<Surface> newSurface);
81     int32_t GetReleaseOutBuffer();
82     std::atomic<VPEAlgoState> state_{VPEAlgoState::UNINITIALIZED};
83     std::shared_ptr<AihdrEnhancerVideoCallback> cb_{nullptr};
84     std::shared_ptr<AihdrEnhancer> csc_{nullptr};
85     std::mutex mutex_;
86     bool getUsage_{false};
87     std::atomic<bool> initBuffer_{false};
88 
89     // task相关
90     std::mutex mtxTaskDone_;
91     std::condition_variable cvTaskDone_;
92     std::shared_ptr<std::thread> taskThread_{nullptr};
93     std::condition_variable cvTaskStart_;
94     std::mutex mtxTaskStart_;
95     std::atomic<bool> isRunning_{false};
96     std::atomic<bool> isProcessing_{false};
97     std::atomic<bool> isEos_{false};
98 
99     // surface相关
100     std::queue<std::shared_ptr<SurfaceBufferWrapper>> outputBufferAvilQue_;
101     std::queue<std::shared_ptr<SurfaceBufferWrapper>> inputBufferAvilQue_;
102     std::queue<std::shared_ptr<SurfaceBufferWrapper>> renderBufferAvilQue_;
103     using RenderBufferAvilMapType = std::map<uint64_t, std::shared_ptr<SurfaceBufferWrapper>>;
104     RenderBufferAvilMapType renderBufferAvilMap_;
105     RenderBufferAvilMapType renderBufferMapBak_;
106     RenderBufferAvilMapType outputBufferAvilQueBak_;
107     std::mutex onBqMutex_;       // inputsruface buffer
108     std::mutex renderQueMutex_;  // outputsruface buffer
109     std::mutex surfaceChangeMutex_;
110     std::mutex surfaceChangeMutex2_;
111     sptr<Surface> inputSurface_{nullptr};
112     sptr<Surface> outputSurface_{nullptr};
113     static constexpr size_t MAX_BUFFER_CNT{5};
114     uint32_t outBufferCnt_{MAX_BUFFER_CNT};
115     uint32_t inBufferCnt_{MAX_BUFFER_CNT};
116     static constexpr size_t MAX_SURFACE_SEQUENCE{std::numeric_limits<uint32_t>::max()};
117     uint32_t lastSurfaceSequence_{MAX_SURFACE_SEQUENCE};
118     BufferRequestConfig requestCfg_{};
119     BufferFlushConfig flushCfg_{};
120 };
121 
122 class AihdrEnhancerBufferConsumerListener : public OHOS::IBufferConsumerListener {
123 public:
AihdrEnhancerBufferConsumerListener(AihdrEnhancerVideoImpl * process)124     explicit AihdrEnhancerBufferConsumerListener(AihdrEnhancerVideoImpl *process) : process_(process) {}
125     void OnBufferAvailable() override;
126 
127 private:
128     AihdrEnhancerVideoImpl *process_;
129 };
130 
131 } // namespace VideoProcessingEngine
132 } // namespace Media
133 } // namespace OHOS
134 #endif // AIHDR_ENHANCER_VIDEO_IMPL_H
135