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 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 #include "codec_heif_encode_service.h"
16 #include <algorithm>
17 #include "codec_log_wrapper.h"
18 #include "hdf_base.h"
19 #include "hdf_remote_service.h"
20 #include <dlfcn.h>
21 #include <unistd.h>
22
23 namespace OHOS {
24 namespace HDI {
25 namespace Codec {
26 namespace Image {
27 namespace V2_1 {
28 using GetCodecHeifHwi = OHOS::VDI::HEIF::ICodecHeifHwi*(*)();
29
CodecHeifEncodeService()30 CodecHeifEncodeService::CodecHeifEncodeService()
31 {
32 isIPCMode_ = (HdfRemoteGetCallingPid() == getpid() ? false : true);
33 }
34
~CodecHeifEncodeService()35 CodecHeifEncodeService::~CodecHeifEncodeService()
36 {
37 heifHwi_ = nullptr;
38 libHeif_ = nullptr;
39 }
40
LoadVendorLib()41 bool CodecHeifEncodeService::LoadVendorLib()
42 {
43 std::lock_guard<std::mutex> lk(mutex_);
44 if (heifHwi_) {
45 return true;
46 }
47 if (libHeif_ == nullptr) {
48 void *handle = dlopen(CODEC_HEIF_VDI_LIB_NAME, RTLD_LAZY);
49 if (handle == nullptr) {
50 CODEC_LOGE("failed to load vendor lib");
51 return false;
52 }
53 libHeif_ = std::shared_ptr<void>(handle, dlclose);
54 }
55 auto func = reinterpret_cast<GetCodecHeifHwi>(dlsym(libHeif_.get(), "GetCodecHeifHwi"));
56 if (func == nullptr) {
57 CODEC_LOGE("failed to load symbol from vendor lib");
58 return false;
59 }
60 heifHwi_ = func();
61 if (heifHwi_ == nullptr) {
62 CODEC_LOGE("failed to create heif hardware encoder");
63 return false;
64 }
65 return true;
66 }
67
DoHeifEncode(const std::vector<ImageItem> & inputImgs,const std::vector<MetaItem> & inputMetas,const std::vector<ItemRef> & refs,const SharedBuffer & output,uint32_t & filledLen)68 int32_t CodecHeifEncodeService::DoHeifEncode(const std::vector<ImageItem>& inputImgs,
69 const std::vector<MetaItem>& inputMetas,
70 const std::vector<ItemRef>& refs,
71 const SharedBuffer& output, uint32_t& filledLen)
72 {
73 if (!isIPCMode_) {
74 return HDF_FAILURE;
75 }
76
77 std::vector<OHOS::VDI::HEIF::ImageItem> inputImgsInternal(inputImgs.size());
78 std::transform(inputImgs.cbegin(), inputImgs.cend(), inputImgsInternal.begin(),
79 OHOS::VDI::HEIF::ConvertImageItem);
80
81 std::vector<OHOS::VDI::HEIF::MetaItem> inputMetasInternal(inputMetas.size());
82 std::transform(inputMetas.cbegin(), inputMetas.cend(), inputMetasInternal.begin(),
83 OHOS::VDI::HEIF::ConvertMetaItem);
84
85 OHOS::VDI::HEIF::SharedBuffer outputToReturn = OHOS::VDI::HEIF::ConvertSharedBuffer(output);
86
87 if (!LoadVendorLib()) {
88 return HDF_FAILURE;
89 }
90
91 int32_t ret = (heifHwi_->DoHeifEncode)(inputImgsInternal, inputMetasInternal, refs, outputToReturn);
92 filledLen = outputToReturn.filledLen;
93 return ret;
94 }
95 } // V2_1
96 } // Image
97 } // Codec
98 } // HDI
99 } // OHOS