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 "detail_enhancer_video_native.h"
17 
18 #include "native_window.h"
19 
20 #include "vpe_log.h"
21 
22 using namespace OHOS;
23 using namespace OHOS::Media::VideoProcessingEngine;
24 
25 namespace {
26 const std::unordered_map<int, int> LEVEL_MAP = {
27     { VIDEO_DETAIL_ENHANCER_QUALITY_LEVEL_NONE,     DetailEnhancer::LEVEL_NONE },
28     { VIDEO_DETAIL_ENHANCER_QUALITY_LEVEL_LOW,      DetailEnhancer::LEVEL_LOW },
29     { VIDEO_DETAIL_ENHANCER_QUALITY_LEVEL_MEDIUM,   DetailEnhancer::LEVEL_MEDIUM },
30     { VIDEO_DETAIL_ENHANCER_QUALITY_LEVEL_HIGH,     DetailEnhancer::LEVEL_HIGH },
31 };
32 }
33 
InitializeInner()34 VideoProcessing_ErrorCode DetailEnhancerVideoNative::InitializeInner()
35 {
36     return AlgoErrorToNdk(detailEnhancer_.Initialize());
37 }
38 
DeinitializeInner()39 VideoProcessing_ErrorCode DetailEnhancerVideoNative::DeinitializeInner()
40 {
41     return AlgoErrorToNdk(detailEnhancer_.Deinitialize());
42 }
43 
SetParameter(const OHOS::Media::Format & parameter)44 VideoProcessing_ErrorCode DetailEnhancerVideoNative::SetParameter(const OHOS::Media::Format& parameter)
45 {
46     int level;
47     if (!parameter.GetIntValue(VIDEO_DETAIL_ENHANCER_PARAMETER_KEY_QUALITY_LEVEL, level)) {
48         VPE_LOGE("No quality level is configured!");
49         return VIDEO_PROCESSING_ERROR_INVALID_PARAMETER;
50     }
51 
52     int innerLevel = NDKLevelToInner(level);
53     if (innerLevel == -1) {
54         VPE_LOGE("Quality level(%{public}d) is invalid!", level);
55         return VIDEO_PROCESSING_ERROR_INVALID_PARAMETER;
56     }
57     return AlgoErrorToNdk(detailEnhancer_.SetParameter(static_cast<DetailEnhancer::Level>(innerLevel)));
58 }
59 
GetParameter(OHOS::Media::Format & parameter)60 VideoProcessing_ErrorCode DetailEnhancerVideoNative::GetParameter(OHOS::Media::Format& parameter)
61 {
62     DetailEnhancer::Level level;
63     auto errorCode = AlgoErrorToNdk(detailEnhancer_.GetParameter(level));
64     if (errorCode != VIDEO_PROCESSING_SUCCESS) {
65         return errorCode;
66     }
67     if (!parameter.PutIntValue(VIDEO_DETAIL_ENHANCER_PARAMETER_KEY_QUALITY_LEVEL, level)) {
68         VPE_LOGE("Failed to configure quality level!");
69         return VIDEO_PROCESSING_ERROR_PROCESS_FAILED;
70     }
71     return VIDEO_PROCESSING_SUCCESS;
72 }
73 
SetProducerSurface(const OHNativeWindow & window,BufferRequestConfig & requestCfg)74 VideoProcessing_ErrorCode DetailEnhancerVideoNative::SetProducerSurface(const OHNativeWindow& window,
75     BufferRequestConfig& requestCfg)
76 {
77     requestCfg.width = window.surface->GetRequestWidth();
78     requestCfg.height = window.surface->GetRequestHeight();
79     return VIDEO_PROCESSING_SUCCESS;
80 }
81 
Process(const sptr<SurfaceBuffer> & sourceImage,sptr<SurfaceBuffer> & destinationImage)82 VideoProcessing_ErrorCode DetailEnhancerVideoNative::Process(const sptr<SurfaceBuffer>& sourceImage,
83     sptr<SurfaceBuffer>& destinationImage)
84 {
85     return AlgoErrorToNdk(detailEnhancer_.Process(sourceImage, destinationImage));
86 }
87 
UpdateRequestCfg(const sptr<SurfaceBuffer> & consumerBuffer,BufferRequestConfig & requestCfg)88 void DetailEnhancerVideoNative::UpdateRequestCfg(const sptr<SurfaceBuffer>& consumerBuffer,
89     BufferRequestConfig& requestCfg)
90 {
91     if (requestCfg.width == 0 || requestCfg.height == 0) {
92         requestCfg.width = consumerBuffer->GetWidth();
93         requestCfg.height = consumerBuffer->GetHeight();
94     }
95     requestCfg.format = consumerBuffer->GetFormat();
96 }
97 
NDKLevelToInner(int level) const98 int DetailEnhancerVideoNative::NDKLevelToInner(int level) const
99 {
100     auto it = LEVEL_MAP.find(level);
101     if (it == LEVEL_MAP.end()) {
102         VPE_LOGE("Invalid input level:%{public}d", level);
103         return -1;
104     }
105     return it->second;
106 }
107