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 "codec_log_wrapper.h"
17 #include "hdf_base.h"
18 #include <dlfcn.h>
19 #include <unistd.h>
20
21 namespace OHOS {
22 namespace HDI {
23 namespace Codec {
24 namespace Image {
25 namespace V2_0 {
26 using GetCodecHeifHwi = ICodecHeifHwi*(*)();
27
LoadVendorLib()28 bool CodecHeifEncodeService::LoadVendorLib()
29 {
30 if (heifHwi_) {
31 return true;
32 }
33 if (libHeif_ == nullptr) {
34 void *handle = dlopen(CODEC_HEIF_VDI_LIB_NAME, RTLD_LAZY);
35 if (handle == nullptr) {
36 CODEC_LOGE("failed to load vendor lib");
37 return false;
38 }
39 libHeif_ = std::shared_ptr<void>(handle, dlclose);
40 }
41 auto func = reinterpret_cast<GetCodecHeifHwi>(dlsym(libHeif_.get(), "GetCodecHeifHwi"));
42 if (func == nullptr) {
43 CODEC_LOGE("failed to load symbol from vendor lib");
44 return false;
45 }
46 heifHwi_ = func();
47 if (heifHwi_ == nullptr) {
48 CODEC_LOGE("failed to create heif hardware encoder");
49 return false;
50 }
51 return true;
52 }
53
DoHeifEncode(const std::vector<ImageItem> & inputImgs,const std::vector<MetaItem> & inputMetas,const std::vector<ItemRef> & refs,const SharedBuffer & output,uint32_t & filledLen)54 int32_t CodecHeifEncodeService::DoHeifEncode(const std::vector<ImageItem>& inputImgs,
55 const std::vector<MetaItem>& inputMetas,
56 const std::vector<ItemRef>& refs,
57 const SharedBuffer& output, uint32_t& filledLen)
58 {
59 if (!LoadVendorLib()) {
60 return HDF_FAILURE;
61 }
62 SharedBuffer outputToReturn = output;
63 int32_t ret = (heifHwi_->DoHeifEncode)(inputImgs, inputMetas, refs, outputToReturn);
64 filledLen = outputToReturn.filledLen;
65 auto releaseRes = [](int fd) {
66 if (fd > 0) {
67 close(fd);
68 }
69 };
70 for (auto one : inputImgs) {
71 releaseRes(one.sharedProperties.fd);
72 }
73 for (auto one : inputMetas) {
74 releaseRes(one.data.fd);
75 }
76 releaseRes(output.fd);
77 return ret;
78 }
79 } // V2_0
80 } // Image
81 } // Codec
82 } // HDI
83 } // OHOS