1 /*
2 * Copyright (C) 2023 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 #define HST_LOG_TAG "HevcParserManager"
17
18 #include "hevc_parser_manager.h"
19 #include <dlfcn.h>
20 #include "common/log.h"
21
22 namespace {
23 const std::string HEVC_LIB_PATH = "libav_codec_hevc_parser.z.so";
24 }
25
26 namespace OHOS {
27 namespace Media {
28 namespace Plugins {
29 void *HevcParserManager::handler_ = nullptr;
30 HevcParserManager::CreateFunc HevcParserManager::createFunc_ = nullptr;
31 HevcParserManager::DestroyFunc HevcParserManager::destroyFunc_ = nullptr;
32 std::mutex HevcParserManager::mtx_;
33
~HevcParserManager()34 HevcParserManager::~HevcParserManager()
35 {
36 if (hevcParser_) {
37 destroyFunc_(hevcParser_);
38 hevcParser_ = nullptr;
39 }
40 }
41
Init()42 bool HevcParserManager::Init()
43 {
44 std::lock_guard<std::mutex> lock(mtx_);
45 if (!handler_) {
46 if (!CheckSymbol(LoadPluginFile(HEVC_LIB_PATH))) {
47 MEDIA_LOG_E("Load .so fail");
48 return false;
49 }
50 }
51 return true;
52 }
53
Create()54 std::shared_ptr<HevcParserManager> HevcParserManager::Create()
55 {
56 std::shared_ptr<HevcParserManager> loader = std::make_shared<HevcParserManager>();
57 if (!loader->Init()) {
58 return nullptr;
59 }
60 loader->hevcParser_ = loader->createFunc_();
61 if (!loader->hevcParser_) {
62 MEDIA_LOG_E("createFunc_ fail");
63 return nullptr;
64 }
65 return loader;
66 }
67
ParseExtraData(const uint8_t * sample,int32_t size,uint8_t ** extraDataBuf,int32_t * extraDataSize)68 void HevcParserManager::ParseExtraData(const uint8_t *sample, int32_t size,
69 uint8_t **extraDataBuf, int32_t *extraDataSize)
70 {
71 FALSE_RETURN_MSG(hevcParser_ != nullptr, "hevc parser is null!");
72 hevcParser_->ParseExtraData(sample, size, extraDataBuf, extraDataSize);
73 }
74
IsHdrVivid()75 bool HevcParserManager::IsHdrVivid()
76 {
77 FALSE_RETURN_V_MSG_E(hevcParser_ != nullptr, false, "hevc parser is null!");
78 return hevcParser_->IsHdrVivid();
79 }
80
GetColorRange()81 bool HevcParserManager::GetColorRange()
82 {
83 FALSE_RETURN_V_MSG_E(hevcParser_ != nullptr, false, "hevc parser is null!");
84 return hevcParser_->GetColorRange();
85 }
86
GetColorPrimaries()87 uint8_t HevcParserManager::GetColorPrimaries()
88 {
89 FALSE_RETURN_V_MSG_E(hevcParser_ != nullptr, 0, "hevc parser is null!");
90 return hevcParser_->GetColorPrimaries();
91 }
92
GetColorTransfer()93 uint8_t HevcParserManager::GetColorTransfer()
94 {
95 FALSE_RETURN_V_MSG_E(hevcParser_ != nullptr, 0, "hevc parser is null!");
96 return hevcParser_->GetColorTransfer();
97 }
98
GetColorMatrixCoeff()99 uint8_t HevcParserManager::GetColorMatrixCoeff()
100 {
101 FALSE_RETURN_V_MSG_E(hevcParser_ != nullptr, 0, "hevc parser is null!");
102 return hevcParser_->GetColorMatrixCoeff();
103 }
104
GetProfileIdc()105 uint8_t HevcParserManager::GetProfileIdc()
106 {
107 FALSE_RETURN_V_MSG_E(hevcParser_ != nullptr, 0, "hevc parser is null!");
108 return hevcParser_->GetProfileIdc();
109 }
110
GetLevelIdc()111 uint8_t HevcParserManager::GetLevelIdc()
112 {
113 FALSE_RETURN_V_MSG_E(hevcParser_ != nullptr, 0, "hevc parser is null!");
114 return hevcParser_->GetLevelIdc();
115 }
116
GetChromaLocation()117 uint32_t HevcParserManager::GetChromaLocation()
118 {
119 FALSE_RETURN_V_MSG_E(hevcParser_ != nullptr, 0, "hevc parser is null!");
120 return hevcParser_->GetChromaLocation();
121 }
122
GetPicWidInLumaSamples()123 uint32_t HevcParserManager::GetPicWidInLumaSamples()
124 {
125 FALSE_RETURN_V_MSG_E(hevcParser_ != nullptr, 0, "hevc parser is null!");
126 return hevcParser_->GetPicWidInLumaSamples();
127 }
128
GetPicHetInLumaSamples()129 uint32_t HevcParserManager::GetPicHetInLumaSamples()
130 {
131 FALSE_RETURN_V_MSG_E(hevcParser_ != nullptr, 0, "hevc parser is null!");
132 return hevcParser_->GetPicHetInLumaSamples();
133 }
134
ConvertExtraDataToAnnexb(uint8_t * extraData,int32_t extraDataSize)135 void HevcParserManager::ConvertExtraDataToAnnexb(uint8_t *extraData, int32_t extraDataSize)
136 {
137 FALSE_RETURN_MSG(hevcParser_ != nullptr, "hevc parser is null!");
138 hevcParser_->ConvertExtraDataToAnnexb(extraData, extraDataSize);
139 }
140
ConvertPacketToAnnexb(uint8_t ** hvccPacket,int32_t & hvccPacketSize)141 void HevcParserManager::ConvertPacketToAnnexb(uint8_t **hvccPacket, int32_t &hvccPacketSize)
142 {
143 FALSE_RETURN_MSG(hevcParser_ != nullptr, "hevc parser is null!");
144 hevcParser_->ConvertPacketToAnnexb(hvccPacket, hvccPacketSize);
145 }
146
ParseAnnexbExtraData(const uint8_t * sample,int32_t size)147 void HevcParserManager::ParseAnnexbExtraData(const uint8_t *sample, int32_t size)
148 {
149 FALSE_RETURN_MSG(hevcParser_ != nullptr, "hevc parser is null!");
150 hevcParser_->ParseAnnexbExtraData(sample, size);
151 }
152
ResetXPSSendStatus()153 void HevcParserManager::ResetXPSSendStatus()
154 {
155 FALSE_RETURN_MSG(hevcParser_ != nullptr, "hevc parser is null!");
156 hevcParser_->ResetXPSSendStatus();
157 }
158
LoadPluginFile(const std::string & path)159 void *HevcParserManager::LoadPluginFile(const std::string &path)
160 {
161 auto ptr = ::dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
162 if (ptr == nullptr) {
163 MEDIA_LOG_E("dlopen failed due to %{public}s", ::dlerror());
164 }
165 handler_ = ptr;
166 return ptr;
167 }
168
CheckSymbol(void * handler)169 bool HevcParserManager::CheckSymbol(void *handler)
170 {
171 if (handler) {
172 std::string createFuncName = "CreateHevcParser";
173 std::string destroyFuncName = "DestroyHevcParser";
174 CreateFunc createFunc = nullptr;
175 DestroyFunc destroyFunc = nullptr;
176 createFunc = (CreateFunc)(::dlsym(handler, createFuncName.c_str()));
177 destroyFunc = (DestroyFunc)(::dlsym(handler, destroyFuncName.c_str()));
178 if (createFunc && destroyFunc) {
179 MEDIA_LOG_D("CheckSymbol: createFuncName %{public}s", createFuncName.c_str());
180 MEDIA_LOG_D("CheckSymbol: destroyFuncName %{public}s", destroyFuncName.c_str());
181 createFunc_ = createFunc;
182 destroyFunc_ = destroyFunc;
183 return true;
184 }
185 }
186 return false;
187 }
188 } // namespace Plugins
189 } // namespace Media
190 } // namespace OHOS