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 #include "dynamic_controller.h"
17 #include "avcodec_errors.h"
18 
19 namespace OHOS {
20 namespace MediaAVCodec {
21 namespace PostProcessing {
22 
~DynamicController()23 DynamicController::~DynamicController()
24 {
25     if (instance_ != nullptr) {
26         AVCODEC_LOGW("Instance is not correctly destroyed.");
27     }
28 
29     UnloadInterfacesImpl();
30 }
31 
LoadInterfacesImpl()32 bool DynamicController::LoadInterfacesImpl()
33 {
34     if (ready_) {
35         AVCODEC_LOGD("Controller is ready.");
36         return true;
37     }
38     if (interface_.Load()) {
39         ready_ = true;
40         return true;
41     }
42     return false;
43 }
44 
UnloadInterfacesImpl()45 void DynamicController::UnloadInterfacesImpl()
46 {
47     interface_.Unload();
48     ready_ = false;
49 }
50 
IsColorSpaceConversionSupportedImpl(const CapabilityInfo & input,const CapabilityInfo & output)51 bool DynamicController::IsColorSpaceConversionSupportedImpl(const CapabilityInfo& input, const CapabilityInfo& output)
52 {
53     auto inputPtr = static_cast<const void*>(&input);
54     auto outputPtr = static_cast<const void*>(&output);
55     return interface_.Invoke<DynamicInterfaceName::IS_COLORSPACE_CONVERSION_SUPPORTED>(inputPtr, outputPtr);
56 }
57 
CreateImpl()58 int32_t DynamicController::CreateImpl()
59 {
60     instance_ = static_cast<DynamicColorSpaceConverterHandle*>(interface_.Invoke<DynamicInterfaceName::CREATE>());
61     CHECK_AND_RETURN_RET_LOG(instance_ != nullptr, AVCS_ERR_UNKNOWN, "Create VPE instance failed.");
62     return AVCS_ERR_OK;
63 }
64 
DestroyImpl()65 void DynamicController::DestroyImpl()
66 {
67     interface_.Invoke<DynamicInterfaceName::DESTROY>(instance_);
68     instance_ = nullptr;
69 }
70 
SetCallbackImpl(void * callback,void * userData)71 int32_t DynamicController::SetCallbackImpl(void* callback, void* userData)
72 {
73     auto ret = interface_.Invoke<DynamicInterfaceName::SET_CALLBACK>(instance_, callback, userData);
74     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN,
75         "Set callback for video processing failed.");
76     return AVCS_ERR_OK;
77 }
78 
SetOutputSurfaceImpl(sptr<Surface> surface)79 int32_t DynamicController::SetOutputSurfaceImpl(sptr<Surface> surface)
80 {
81     void* sf = static_cast<void*>(&surface);
82     auto ret = interface_.Invoke<DynamicInterfaceName::SET_OUTPUT_SURFACE>(instance_, sf);
83     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN,
84         "Set output surface for video processing failed.");
85     surface->UnRegisterReleaseListener();
86     surface->RegisterReleaseListener([this](sptr<SurfaceBuffer> &buffer) -> GSError {
87         return OnProducerBufferReleased(buffer);
88     });
89     return AVCS_ERR_OK;
90 }
91 
CreateInputSurfaceImpl(sptr<Surface> & surface)92 int32_t DynamicController::CreateInputSurfaceImpl(sptr<Surface>& surface)
93 {
94     void* sf = static_cast<void*>(&surface);
95     auto ret = interface_.Invoke<DynamicInterfaceName::CREATE_INPUT_SURFACE>(instance_, sf);
96     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK && surface != nullptr, AVCS_ERR_UNKNOWN,
97         "Create input surface for video processing failed.");
98     return AVCS_ERR_OK;
99 }
100 
SetParameterImpl(Media::Format & parameter)101 int32_t DynamicController::SetParameterImpl(Media::Format& parameter)
102 {
103     void* parameterPtr = static_cast<void*>(&parameter);
104     auto ret = interface_.Invoke<DynamicInterfaceName::CREATE_INPUT_SURFACE>(instance_, parameterPtr);
105     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Set parameter for VPE failed.");
106     return AVCS_ERR_OK;
107 }
108 
GetParameterImpl(Media::Format & parameter)109 int32_t DynamicController::GetParameterImpl(Media::Format& parameter)
110 {
111     void* parameterPtr = static_cast<void*>(&parameter);
112     auto ret = interface_.Invoke<DynamicInterfaceName::CREATE_INPUT_SURFACE>(instance_, parameterPtr);
113     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Get parameter for VPE failed.");
114     return AVCS_ERR_OK;
115 }
116 
ConfigureImpl(Media::Format & config)117 int32_t DynamicController::ConfigureImpl(Media::Format& config)
118 {
119     void* configPtr = static_cast<void*>(&config);
120     auto ret = interface_.Invoke<DynamicInterfaceName::CONFIGURE>(instance_, configPtr);
121     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Configure video processing failed.");
122     return AVCS_ERR_OK;
123 }
124 
PrepareImpl()125 int32_t DynamicController::PrepareImpl()
126 {
127     auto ret = interface_.Invoke<DynamicInterfaceName::PREPARE>(instance_);
128     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Prepare video processing failed.");
129     return AVCS_ERR_OK;
130 }
131 
StartImpl()132 int32_t DynamicController::StartImpl()
133 {
134     auto ret = interface_.Invoke<DynamicInterfaceName::START>(instance_);
135     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Start video processing failed.");
136     return AVCS_ERR_OK;
137 }
138 
StopImpl()139 int32_t DynamicController::StopImpl()
140 {
141     auto ret = interface_.Invoke<DynamicInterfaceName::STOP>(instance_);
142     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Stop video processing failed.");
143     return AVCS_ERR_OK;
144 }
145 
FlushImpl()146 int32_t DynamicController::FlushImpl()
147 {
148     auto ret = interface_.Invoke<DynamicInterfaceName::FLUSH>(instance_);
149     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Flush video processing failed.");
150     return AVCS_ERR_OK;
151 }
152 
GetOutputFormatImpl(Media::Format & format)153 int32_t DynamicController::GetOutputFormatImpl(Media::Format &format)
154 {
155     void *formatPtr = static_cast<void *>(&format);
156     auto ret = interface_.Invoke<DynamicInterfaceName::GET_OUTPUT_FORMAT>(instance_, formatPtr);
157     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN,
158                              "GetOutputFormat video processing failed.");
159     return AVCS_ERR_OK;
160 }
161 
ResetImpl()162 int32_t DynamicController::ResetImpl()
163 {
164     auto ret = interface_.Invoke<DynamicInterfaceName::RESET>(instance_);
165     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Reset video processing failed.");
166     return AVCS_ERR_OK;
167 }
168 
ReleaseImpl()169 int32_t DynamicController::ReleaseImpl()
170 {
171     auto ret = interface_.Invoke<DynamicInterfaceName::RELEASE>(instance_);
172     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Release video processing failed.");
173     return AVCS_ERR_OK;
174 }
175 
ReleaseOutputBufferImpl(uint32_t index,bool render)176 int32_t DynamicController::ReleaseOutputBufferImpl(uint32_t index, bool render)
177 {
178     auto ret = interface_.Invoke<DynamicInterfaceName::RELEASE_OUPUT_BUFFER>(instance_, index, render);
179     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN,
180         "Release output buffer of video processing failed.");
181     return AVCS_ERR_OK;
182 }
183 
184 GSError DynamicController::OnProducerBufferReleased([[maybe_unused]] sptr<SurfaceBuffer> &buffer)
185 {
186     auto ret = interface_.Invoke<DynamicInterfaceName::ON_PRODUCER_BUFFER_RELEASED>(instance_);
187     return static_cast<GSError>(ret);
188 }
189 
190 } // namespace PostProcessing
191 } // namespace MediaAVCodec
192 } // namespace OHOS