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 21namespace OHOS { 22namespace HDI { 23namespace Codec { 24namespace Image { 25namespace V2_0 { 26using GetCodecHeifHwi = ICodecHeifHwi*(*)(); 27 28bool 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 54int32_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