• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 #if defined(VIDEO_SUPPORT)
16 
17 #define HST_LOG_TAG "HdiCodecManager"
18 
19 #include "hdi_codec_manager.h"
20 #include "foundation/utils/constants.h"
21 #include "foundation/log.h"
22 #include "hdf_base.h"
23 #include "hdi_codec_adapter.h"
24 #include "plugin/common/plugin_caps_builder.h"
25 
26 namespace OHOS {
27 namespace Media {
28 namespace Plugin {
29 namespace CodecAdapter {
30 using namespace CodecHDI;
31 
GetInstance()32 HdiCodecManager& HdiCodecManager::GetInstance()
33 {
34     static HdiCodecManager impl;
35     return impl;
36 }
37 
HdiCodecManager()38 HdiCodecManager::HdiCodecManager()
39 {
40     Init();
41 }
42 
~HdiCodecManager()43 HdiCodecManager::~HdiCodecManager()
44 {
45     Reset();
46 }
47 
CreateComponent(const Plugin::Any & component,uint32_t & id,const std::string & name,const Plugin::Any & appData,const Plugin::Any & callbacks)48 int32_t HdiCodecManager::CreateComponent(const Plugin::Any& component, uint32_t& id, const std::string& name,
49                                          const Plugin::Any& appData, const Plugin::Any& callbacks)
50 {
51     MEDIA_LOG_I("CreateComponent Start");
52     if (!mgr_) {
53         Init();
54         FALSE_RETURN_V_MSG(mgr_ != nullptr, HDF_FAILURE, "mgr is nullptr");
55     }
56     sptr<CodecHDI::ICodecComponent> codecComponent = Plugin::AnyCast<sptr<CodecHDI::ICodecComponent>>(component);
57     FALSE_RETURN_V_MSG(codecComponent != nullptr, HDF_FAILURE, "component is nullptr");
58     return mgr_->CreateComponent(codecComponent, id, const_cast<char *>(name.c_str()),
59         Plugin::AnyCast<int64_t>(appData), Plugin::AnyCast<sptr<CodecHDI::ICodecCallback>>(callbacks));
60 }
61 
DestroyComponent(const Plugin::Any & component,uint32_t id)62 int32_t HdiCodecManager::DestroyComponent(const Plugin::Any& component, uint32_t id)
63 {
64     MEDIA_LOG_I("DestroyComponent Start");
65     FALSE_RETURN_V_MSG(mgr_ != nullptr, HDF_FAILURE, "mgr_ is nullptr");
66     (void)component;
67     return mgr_->DestroyComponent(id);
68 }
69 
RegisterCodecPlugins(const std::shared_ptr<OHOS::Media::Plugin::Register> & reg)70 Status HdiCodecManager::RegisterCodecPlugins(const std::shared_ptr<OHOS::Media::Plugin::Register>& reg)
71 {
72     MEDIA_LOG_I("RegisterCodecPlugins Start");
73     std::string packageName = "HdiCodecAdapter";
74     if (!mgr_) {
75         MEDIA_LOG_E("Codec package " PUBLIC_LOG_S " has no valid component manager", packageName.c_str());
76         return Status::ERROR_INVALID_DATA;
77     }
78     InitCaps();
79     for (auto codecCapability : codecCapabilitys_) {
80         if (codecCapability.pluginType != PluginType::VIDEO_DECODER &&
81             codecCapability.pluginType != PluginType::VIDEO_ENCODER) {
82             MEDIA_LOG_W("Plugin does not belong to video codec, pluginType: " PUBLIC_LOG_D32,
83                         codecCapability.pluginType);
84             continue;
85         }
86         CodecPluginDef def;
87         def.rank = 100; // 100 default rank
88         def.codecMode = CodecMode::HARDWARE;
89         def.pluginType = codecCapability.pluginType;
90         def.name = packageName + "." + codecCapability.name;
91         def.inCaps = codecCapability.inCaps;
92         def.outCaps = codecCapability.outCaps;
93         auto pluginMime = codecCapability.pluginMime;
94         def.creator = [pluginMime] (const std::string& name) -> std::shared_ptr<CodecPlugin> {
95             return std::make_shared<HdiCodecAdapter>(name, pluginMime);
96         };
97         if (reg->AddPlugin(def) != Status::OK) {
98             MEDIA_LOG_E("Add plugin " PUBLIC_LOG_S " failed", def.name.c_str());
99         }
100         MEDIA_LOG_DD("pluginType: " PUBLIC_LOG_D32 ", pluginName: " PUBLIC_LOG_S, def.pluginType, def.name.c_str());
101     }
102     return Status::OK;
103 }
104 
UnRegisterCodecPlugins()105 Status HdiCodecManager::UnRegisterCodecPlugins()
106 {
107     MEDIA_LOG_I("UnRegisterCodecPlugins Start");
108     return Status::OK;
109 }
110 
Init()111 void HdiCodecManager::Init()
112 {
113     MEDIA_LOG_I("Init Start");
114     mgr_ = CodecHDI::ICodecComponentManager::Get(false);
115 }
116 
Reset()117 void HdiCodecManager::Reset()
118 {
119     MEDIA_LOG_I("Reset Start");
120     mgr_ = nullptr;
121 }
122 
AddHdiCap(const CodecHDI::CodecCompCapability & hdiCap)123 void HdiCodecManager::AddHdiCap(const CodecHDI::CodecCompCapability& hdiCap)
124 {
125     MEDIA_LOG_DD("AddHdiCap Start");
126     auto pluginType = GetCodecType(hdiCap.type);
127     if (pluginType == PluginType::VIDEO_DECODER || pluginType == PluginType::VIDEO_ENCODER) {
128         CodecCapability codecCapability;
129         CapabilityBuilder incapBuilder;
130         CapabilityBuilder outcapBuilder;
131         auto mime = GetCodecMime(hdiCap.role);
132         if (mime == "video/unknown") {
133             return;
134         }
135         if (pluginType == PluginType::VIDEO_DECODER) {
136             incapBuilder.SetMime(mime);
137             if (mime == MEDIA_MIME_VIDEO_H264 || mime == MEDIA_MIME_VIDEO_H265) {
138                 incapBuilder.SetVideoBitStreamFormatList({VideoBitStreamFormat::ANNEXB});
139             }
140             outcapBuilder.SetMime(MEDIA_MIME_VIDEO_RAW);
141             outcapBuilder.SetVideoPixelFormatList(GetCodecFormats(hdiCap.port.video));
142         } else {
143             incapBuilder.SetMime(MEDIA_MIME_VIDEO_RAW);
144             incapBuilder.SetVideoPixelFormatList(GetCodecFormats(hdiCap.port.video));
145             outcapBuilder.SetMime(mime);
146         }
147         codecCapability.inCaps.push_back(incapBuilder.Build());
148         codecCapability.outCaps.push_back(outcapBuilder.Build());
149         codecCapability.pluginType = pluginType;
150         codecCapability.pluginMime = mime;
151         codecCapability.name = hdiCap.compName;
152         codecCapabilitys_.push_back(codecCapability);
153     }
154 }
155 
InitCaps()156 void HdiCodecManager::InitCaps()
157 {
158     MEDIA_LOG_I("InitCaps Start");
159     int32_t compCnt = 0;
160     int32_t ret = mgr_->GetComponentNum(compCnt);
161     if (ret != HDF_SUCCESS || compCnt <= 0) {
162         MEDIA_LOG_E("failed to query component number, ret=%d", ret);
163         return;
164     }
165     std::vector<CodecCompCapability> capList(compCnt);
166     ret = mgr_->GetComponentCapabilityList(capList, compCnt);
167     FALSE_RETURN_MSG(ret == HDF_SUCCESS, "GetComponentCapabilityList fail");
168     for (auto& cap : capList) {
169         AddHdiCap(cap);
170     }
171 }
172 
GetCodecFormats(const CodecHDI::CodecVideoPortCap & port)173 std::vector<VideoPixelFormat> HdiCodecManager::GetCodecFormats(const CodecHDI::CodecVideoPortCap& port)
174 {
175     std::vector<VideoPixelFormat> formats;
176     for (int32_t fmt : port.supportPixFmts) {
177         switch (fmt) {
178             case GRAPHIC_PIXEL_FMT_YCBCR_420_SP:
179                 formats.push_back(VideoPixelFormat::NV12);
180                 break;
181             case GRAPHIC_PIXEL_FMT_YCRCB_420_SP:
182                 formats.push_back(VideoPixelFormat::NV21);
183                 break;
184             case GRAPHIC_PIXEL_FMT_YCBCR_420_P:
185                 formats.push_back(VideoPixelFormat::YUV420P);
186                 break;
187             case GRAPHIC_PIXEL_FMT_RGBA_8888:
188                 formats.push_back(VideoPixelFormat::RGBA);
189                 break;
190             case GRAPHIC_PIXEL_FMT_BGRA_8888:
191                 formats.push_back(VideoPixelFormat::BGRA);
192                 break;
193             default:
194                 MEDIA_LOG_W("Unknown Format" PUBLIC_LOG_D32, fmt);
195         }
196     }
197     return formats;
198 }
199 
GetCodecType(const CodecHDI::CodecType & hdiType)200 PluginType HdiCodecManager::GetCodecType(const CodecHDI::CodecType& hdiType)
201 {
202     switch (hdiType) {
203         case VIDEO_DECODER:
204             return PluginType::VIDEO_DECODER;
205         case VIDEO_ENCODER:
206             return PluginType::VIDEO_ENCODER;
207         case AUDIO_DECODER:
208             return PluginType::AUDIO_DECODER;
209         case AUDIO_ENCODER:
210             return PluginType::AUDIO_ENCODER;
211         default:
212             return PluginType::INVALID_TYPE;
213     }
214 }
215 
GetCodecMime(const CodecHDI::AvCodecRole & role)216 std::string HdiCodecManager::GetCodecMime(const CodecHDI::AvCodecRole& role)
217 {
218     switch (role) {
219         case MEDIA_ROLETYPE_VIDEO_AVC:
220             return MEDIA_MIME_VIDEO_H264;
221         case MEDIA_ROLETYPE_VIDEO_HEVC:
222             return MEDIA_MIME_VIDEO_H265;
223         default:
224             return "video/unknown";
225     }
226 }
227 } // namespace CodecAdapter
228 } // namespace Plugin
229 } // namespace Media
230 } // namespace OHOS
231 #endif