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 "StreamParserManager"
17
18 #include "stream_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 const std::string VVC_LIB_PATH = "libav_codec_vvc_parser.z.so";
25 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_DEMUXER, "StreamParserManager" };
26 }
27
28 namespace OHOS {
29 namespace Media {
30 namespace Plugins {
31 std::mutex StreamParserManager::mtx_;
32 std::map<VideoStreamType, void *> StreamParserManager::handlerMap_ {};
33 std::map<VideoStreamType, CreateFunc> StreamParserManager::createFuncMap_ {};
34 std::map<VideoStreamType, DestroyFunc> StreamParserManager::destroyFuncMap_ {};
35
StreamParserManager()36 StreamParserManager::StreamParserManager()
37 {
38 videoStreamType_ = VideoStreamType::HEVC;
39 }
40
~StreamParserManager()41 StreamParserManager::~StreamParserManager()
42 {
43 if (streamParser_) {
44 destroyFuncMap_[videoStreamType_](streamParser_);
45 streamParser_ = nullptr;
46 }
47 }
48
Init(VideoStreamType videoStreamType)49 bool StreamParserManager::Init(VideoStreamType videoStreamType)
50 {
51 std::lock_guard<std::mutex> lock(mtx_);
52 if (handlerMap_.count(videoStreamType) && createFuncMap_.count(videoStreamType) &&
53 destroyFuncMap_.count(videoStreamType)) {
54 return true;
55 }
56
57 std::string streamParserPath;
58 if (videoStreamType == VideoStreamType::HEVC) {
59 streamParserPath = HEVC_LIB_PATH;
60 }
61 if (handlerMap_.count(videoStreamType) == 0) {
62 handlerMap_[videoStreamType] = LoadPluginFile(streamParserPath);
63 }
64 FALSE_RETURN_V_MSG_E(CheckSymbol(handlerMap_[videoStreamType], videoStreamType), false,
65 "Load stream parser failed");
66 return true;
67 }
68
Create(VideoStreamType videoStreamType)69 std::shared_ptr<StreamParserManager> StreamParserManager::Create(VideoStreamType videoStreamType)
70 {
71 MEDIA_LOG_D("Parser " PUBLIC_LOG_D32, videoStreamType);
72 std::shared_ptr<StreamParserManager> loader = std::make_shared<StreamParserManager>();
73 if (!loader->Init(videoStreamType)) {
74 return nullptr;
75 }
76 loader->streamParser_ = loader->createFuncMap_[videoStreamType]();
77 if (!loader->streamParser_) {
78 MEDIA_LOG_E("CreateFunc_ failed");
79 return nullptr;
80 }
81 loader->videoStreamType_ = videoStreamType;
82 return loader;
83 }
84
ParseExtraData(const uint8_t * sample,int32_t size,uint8_t ** extraDataBuf,int32_t * extraDataSize)85 void StreamParserManager::ParseExtraData(const uint8_t *sample, int32_t size,
86 uint8_t **extraDataBuf, int32_t *extraDataSize)
87 {
88 FALSE_RETURN_MSG(streamParser_ != nullptr, "Stream parser is nullptr");
89 streamParser_->ParseExtraData(sample, size, extraDataBuf, extraDataSize);
90 }
91
IsHdrVivid()92 bool StreamParserManager::IsHdrVivid()
93 {
94 FALSE_RETURN_V_MSG_E(streamParser_ != nullptr, false, "Stream parser is nullptr");
95 return streamParser_->IsHdrVivid();
96 }
97
IsSyncFrame(const uint8_t * sample,int32_t size)98 bool StreamParserManager::IsSyncFrame(const uint8_t *sample, int32_t size)
99 {
100 FALSE_RETURN_V_MSG_E(streamParser_ != nullptr, false, "Stream parser is nullptr");
101 return streamParser_->IsSyncFrame(sample, size);
102 }
103
GetColorRange()104 bool StreamParserManager::GetColorRange()
105 {
106 FALSE_RETURN_V_MSG_E(streamParser_ != nullptr, false, "Stream parser is nullptr");
107 return streamParser_->GetColorRange();
108 }
109
GetColorPrimaries()110 uint8_t StreamParserManager::GetColorPrimaries()
111 {
112 FALSE_RETURN_V_MSG_E(streamParser_ != nullptr, 0, "Stream parser is nullptr");
113 return streamParser_->GetColorPrimaries();
114 }
115
GetColorTransfer()116 uint8_t StreamParserManager::GetColorTransfer()
117 {
118 FALSE_RETURN_V_MSG_E(streamParser_ != nullptr, 0, "Stream parser is nullptr");
119 return streamParser_->GetColorTransfer();
120 }
121
GetColorMatrixCoeff()122 uint8_t StreamParserManager::GetColorMatrixCoeff()
123 {
124 FALSE_RETURN_V_MSG_E(streamParser_ != nullptr, 0, "Stream parser is nullptr");
125 return streamParser_->GetColorMatrixCoeff();
126 }
127
GetProfileIdc()128 uint8_t StreamParserManager::GetProfileIdc()
129 {
130 FALSE_RETURN_V_MSG_E(streamParser_ != nullptr, 0, "Stream parser is nullptr");
131 return streamParser_->GetProfileIdc();
132 }
133
GetLevelIdc()134 uint8_t StreamParserManager::GetLevelIdc()
135 {
136 FALSE_RETURN_V_MSG_E(streamParser_ != nullptr, 0, "Stream parser is nullptr");
137 return streamParser_->GetLevelIdc();
138 }
139
GetChromaLocation()140 uint32_t StreamParserManager::GetChromaLocation()
141 {
142 FALSE_RETURN_V_MSG_E(streamParser_ != nullptr, 0, "Stream parser is nullptr");
143 return streamParser_->GetChromaLocation();
144 }
145
GetPicWidInLumaSamples()146 uint32_t StreamParserManager::GetPicWidInLumaSamples()
147 {
148 FALSE_RETURN_V_MSG_E(streamParser_ != nullptr, 0, "Stream parser is nullptr");
149 return streamParser_->GetPicWidInLumaSamples();
150 }
151
GetPicHetInLumaSamples()152 uint32_t StreamParserManager::GetPicHetInLumaSamples()
153 {
154 FALSE_RETURN_V_MSG_E(streamParser_ != nullptr, 0, "Stream parser is nullptr");
155 return streamParser_->GetPicHetInLumaSamples();
156 }
157
ConvertExtraDataToAnnexb(uint8_t * extraData,int32_t extraDataSize)158 bool StreamParserManager::ConvertExtraDataToAnnexb(uint8_t *extraData, int32_t extraDataSize)
159 {
160 FALSE_RETURN_V_MSG_E(streamParser_ != nullptr, false, "Stream parser is nullptr");
161 return streamParser_->ConvertExtraDataToAnnexb(extraData, extraDataSize);
162 }
163
ConvertPacketToAnnexb(uint8_t ** hvccPacket,int32_t & hvccPacketSize,uint8_t * sideData,size_t sideDataSize,bool isExtradata)164 void StreamParserManager::ConvertPacketToAnnexb(uint8_t **hvccPacket, int32_t &hvccPacketSize, uint8_t *sideData,
165 size_t sideDataSize, bool isExtradata)
166 {
167 FALSE_RETURN_MSG(streamParser_ != nullptr, "Stream parser is nullptr");
168 streamParser_->ConvertPacketToAnnexb(hvccPacket, hvccPacketSize, sideData, sideDataSize, isExtradata);
169 }
170
ParseAnnexbExtraData(const uint8_t * sample,int32_t size)171 void StreamParserManager::ParseAnnexbExtraData(const uint8_t *sample, int32_t size)
172 {
173 FALSE_RETURN_MSG(streamParser_ != nullptr, "Stream parser is nullptr");
174 streamParser_->ParseAnnexbExtraData(sample, size);
175 }
176
ResetXPSSendStatus()177 void StreamParserManager::ResetXPSSendStatus()
178 {
179 FALSE_RETURN_MSG(streamParser_ != nullptr, "Stream parser is nullptr");
180 streamParser_->ResetXPSSendStatus();
181 }
182
LoadPluginFile(const std::string & path)183 void *StreamParserManager::LoadPluginFile(const std::string &path)
184 {
185 auto ptr = ::dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
186 FALSE_RETURN_V_MSG_E(ptr != nullptr, ptr, "Dlopen failed due to %{public}s", ::dlerror());
187 return ptr;
188 }
189
CheckSymbol(void * handler,VideoStreamType videoStreamType)190 bool StreamParserManager::CheckSymbol(void *handler, VideoStreamType videoStreamType)
191 {
192 if (handler) {
193 std::string createFuncName = "CreateStreamParser";
194 std::string destroyFuncName = "DestroyStreamParser";
195 CreateFunc createFunc = nullptr;
196 DestroyFunc destroyFunc = nullptr;
197 createFunc = (CreateFunc)(::dlsym(handler, createFuncName.c_str()));
198 destroyFunc = (DestroyFunc)(::dlsym(handler, destroyFuncName.c_str()));
199 if (createFunc && destroyFunc) {
200 MEDIA_LOG_D("CreateFuncName %{public}s", createFuncName.c_str());
201 MEDIA_LOG_D("DestroyFuncName %{public}s", destroyFuncName.c_str());
202 createFuncMap_[videoStreamType] = createFunc;
203 destroyFuncMap_[videoStreamType] = destroyFunc;
204 return true;
205 }
206 }
207 return false;
208 }
209
GetLogInfo()210 std::vector<uint8_t> StreamParserManager::GetLogInfo()
211 {
212 FALSE_RETURN_V_MSG_E(streamParser_ != nullptr, {}, "Stream parser is nullptr");
213 return streamParser_->GetLogInfo();
214 }
215
GetMaxReorderPic()216 uint32_t StreamParserManager::GetMaxReorderPic()
217 {
218 FALSE_RETURN_V_MSG_E(streamParser_ != nullptr, {}, "Stream parser is nullptr");
219 MEDIA_LOG_D("GetMaxReorderPic is %{public}u", streamParser_->GetMaxReorderPic());
220 return streamParser_->GetMaxReorderPic();
221 }
222 } // namespace Plugins
223 } // namespace Media
224 } // namespace OHOS