• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 Shenzhen Kaihong DID 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 #include "codec_component_config.h"
16 #include <cinttypes>
17 #include <osal_mem.h>
18 #include "codec_log_wrapper.h"
19 #include "codec_hcb_util.h"
20 
21 #define CODEC_CONFIG_NAME "media_codec_capabilities"
22 
23 namespace {
24     constexpr int32_t MASK_NUM_LIMIT = 32;
25     constexpr char NODE_VIDEO_HARDWARE_ENCODERS[] = "VideoHwEncoders";
26     constexpr char NODE_VIDEO_HARDWARE_DECODERS[] = "VideoHwDecoders";
27     constexpr char NODE_VIDEO_SOFTWARE_ENCODERS[] = "VideoSwEncoders";
28     constexpr char NODE_VIDEO_SOFTWARE_DECODERS[] = "VideoSwDecoders";
29     constexpr char NODE_AUDIO_HARDWARE_ENCODERS[] = "AudioHwEncoders";
30     constexpr char NODE_AUDIO_HARDWARE_DECODERS[] = "AudioHwDecoders";
31     constexpr char NODE_AUDIO_SOFTWARE_ENCODERS[] = "AudioSwEncoders";
32     constexpr char NODE_AUDIO_SOFTWARE_DECODERS[] = "AudioSwDecoders";
33 
34     constexpr char CODEC_CONFIG_KEY_ROLE[] = "role";
35     constexpr char CODEC_CONFIG_KEY_TYPE[] = "type";
36     constexpr char CODEC_CONFIG_KEY_NAME[] = "name";
37     constexpr char CODEC_CONFIG_KEY_SUPPORT_PROFILES[] = "supportProfiles";
38     constexpr char CODEC_CONFIG_KEY_MAX_INST[] = "maxInst";
39     constexpr char CODEC_CONFIG_KEY_IS_SOFTWARE_CODEC[] = "isSoftwareCodec";
40     constexpr char CODEC_CONFIG_KEY_PROCESS_MODE_MASK[] = "processModeMask";
41     constexpr char CODEC_CONFIG_KEY_CAPS_MASK[] = "capsMask";
42     constexpr char CODEC_CONFIG_KEY_MIN_BITRATE[] = "minBitRate";
43     constexpr char CODEC_CONFIG_KEY_MAX_BITRATE[] = "maxBitRate";
44 
45     constexpr char CODEC_CONFIG_KEY_MIN_WIDTH[] = "minWidth";
46     constexpr char CODEC_CONFIG_KEY_MIN_HEIGHT[] = "minHeight";
47     constexpr char CODEC_CONFIG_KEY_MAX_WIDTH[] = "maxWidth";
48     constexpr char CODEC_CONFIG_KEY_MAX_HEIGHT[] = "maxHeight";
49     constexpr char CODEC_CONFIG_KEY_WIDTH_ALIGNMENT[] = "widthAlignment";
50     constexpr char CODEC_CONFIG_KEY_HEIGHT_ALIGNMENT[] = "heightAlignment";
51     constexpr char CODEC_CONFIG_KEY_MIN_BLOCK_COUNT[] = "minBlockCount";
52     constexpr char CODEC_CONFIG_KEY_MAX_BLOCK_COUNT[] = "maxBlockCount";
53     constexpr char CODEC_CONFIG_KEY_MIN_BLOCKS_PER_SECOND[] = "minBlocksPerSecond";
54     constexpr char CODEC_CONFIG_KEY_MAX_BLOCKS_PER_SECOND[] = "maxBlocksPerSecond";
55     constexpr char CODEC_CONFIG_KEY_SUPPORT_PIXEL_FMTS[] = "supportPixelFmts";
56     constexpr char CODEC_CONFIG_KEY_BLOCK_SIZE_WIDTH[] = "blockSizeWidth";
57     constexpr char CODEC_CONFIG_KEY_BLOCK_SIZE_HEIGHT[] = "blockSizeHeight";
58     constexpr char CODEC_CONFIG_KEY_MIN_FRAME_RATE[] = "minFrameRate";
59     constexpr char CODEC_CONFIG_KEY_MAX_FRAME_RATE[] = "maxFrameRate";
60     constexpr char CODEC_CONFIG_KEY_BITE_RATE_MODE[] = "bitRateMode";
61     constexpr char CODEC_CONFIG_KEY_MESURED_FRAME_RATE[] = "measuredFrameRate";
62     constexpr char CODEC_CONFIG_KEY_CAN_SWAP_WIDTH_HEIGHT[] = "canSwapWidthHeight";
63 
64     constexpr char CODEC_CONFIG_KEY_SAMPLE_FORMATS[] = "sampleFormats";
65     constexpr char CODEC_CONFIG_KEY_SAMPLE_RATE[] = "sampleRate";
66     constexpr char CODEC_CONFIG_KEY_CHANNEL_LAYOUTS[] = "channelLayouts";
67     constexpr char CODEC_CONFIG_KEY_CHANNEL_COUNT[] = "channelCount";
68 }
69 
70 using namespace OHOS::HDI::Codec::V2_0;
71 namespace OHOS {
72 namespace Codec {
73 namespace Omx {
74 CodecComponentConfig CodecComponentConfig::config_;
CodecComponentConfig()75 CodecComponentConfig::CodecComponentConfig()
76 {
77     node_.name = nullptr;
78     node_.hashValue = 0;
79     node_.attrData = nullptr;
80     node_.parent = nullptr;
81     node_.child = nullptr;
82     node_.sibling = nullptr;
83 }
84 
Init(const DeviceResourceNode & node)85 void CodecComponentConfig::Init(const DeviceResourceNode &node)
86 {
87     node_ = node;
88     const std::string codecGroupsNodeName[] = { NODE_VIDEO_HARDWARE_ENCODERS, NODE_VIDEO_HARDWARE_DECODERS,
89                                                 NODE_VIDEO_SOFTWARE_ENCODERS, NODE_VIDEO_SOFTWARE_DECODERS,
90                                                 NODE_AUDIO_HARDWARE_ENCODERS, NODE_AUDIO_HARDWARE_DECODERS,
91                                                 NODE_AUDIO_SOFTWARE_ENCODERS, NODE_AUDIO_SOFTWARE_DECODERS };
92     int count = sizeof(codecGroupsNodeName) / sizeof(std::string);
93     for (int index = 0; index < count; index++) {
94         GetGroupCapabilities(codecGroupsNodeName[index]);
95     }
96     CODEC_LOGD("Init Run....capList_.size=%{public}zu", capList_.size());
97 }
98 
CodecCompCapabilityInit()99 int32_t CodecComponentConfig::CodecCompCapabilityInit()
100 {
101     const struct DeviceResourceNode *rootNode = HdfGetHcsRootNode();
102     if (rootNode == nullptr) {
103         CODEC_LOGE("GetRootNode failed");
104         return HDF_FAILURE;
105     }
106     const struct DeviceResourceNode *codecNode = HcsGetNodeByMatchAttr(rootNode, CODEC_CONFIG_NAME);
107     if (codecNode == nullptr) {
108         CODEC_LOGE("codecNode is nullptr");
109         return HDF_FAILURE;
110     }
111     OHOS::Codec::Omx::CodecComponentConfig::GetInstance()->Init(*codecNode);
112     return HDF_SUCCESS;
113 }
114 
GetInstance()115 CodecComponentConfig *CodecComponentConfig::GetInstance()
116 {
117     return &config_;
118 }
119 
GetComponentNum(int32_t & count)120 int32_t CodecComponentConfig::GetComponentNum(int32_t &count)
121 {
122     count = static_cast<int32_t>(capList_.size());
123     CODEC_LOGD("enter, count = %{public}d", count);
124     return HDF_SUCCESS;
125 }
126 
GetComponentCapabilityList(std::vector<CodecCompCapability> & capList,int32_t count)127 int32_t CodecComponentConfig::GetComponentCapabilityList(std::vector<CodecCompCapability> &capList, int32_t count)
128 {
129     CODEC_LOGD("count[%{public}d], size[%{public}zu]", count, capList_.size());
130     if (count <= 0) {
131         CODEC_LOGE("count[%{public}d] is invalid", count);
132         return HDF_FAILURE;
133     }
134     if (count > static_cast<int32_t>(capList_.size())) {
135         CODEC_LOGW("count[%{public}d] is too large", count);
136         count = static_cast<int32_t>(capList_.size());
137     }
138     auto first = capList_.begin();
139     auto last = capList_.begin() + count;
140     capList.assign(first, last);
141     return HDF_SUCCESS;
142 }
143 
GetGroupCapabilities(const std::string & nodeName)144 int32_t CodecComponentConfig::GetGroupCapabilities(const std::string &nodeName)
145 {
146     bool isVideoGroup = true;
147     const struct DeviceResourceNode *codecGroupNode = nullptr;
148     struct DeviceResourceNode *childNode = nullptr;
149     struct DeviceResourceIface *iface = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
150     if ((iface == nullptr) || (iface->GetUint32 == nullptr) ||
151         (iface->GetBool == nullptr) || (iface->GetString == nullptr)) {
152         CODEC_LOGE(" failed, iface or its GetUint32 or GetBool or GetString is nullptr!");
153         return HDF_ERR_INVALID_PARAM;
154     }
155 
156     codecGroupNode = iface->GetChildNode(&node_, nodeName.c_str());
157     if (codecGroupNode == nullptr) {
158         CODEC_LOGE("failed to get child node: %{public}s!", nodeName.c_str());
159         return HDF_FAILURE;
160     }
161 
162     if (nodeName.find("Video") == std::string::npos) {
163         isVideoGroup = false;
164     }
165 
166     DEV_RES_NODE_FOR_EACH_CHILD_NODE(codecGroupNode, childNode)
167     {
168         CodecCompCapability cap;
169         if (GetOneCapability(*iface, *childNode, cap, isVideoGroup) != HDF_SUCCESS) {
170             CODEC_LOGE("GetOneCapability failed, role is %{public}d!", cap.role);
171         }
172         capList_.push_back(cap);
173     }
174 
175     return HDF_SUCCESS;
176 }
177 
GetOneCapability(const struct DeviceResourceIface & iface,const struct DeviceResourceNode & childNode,CodecCompCapability & cap,bool isVideoGroup)178 int32_t CodecComponentConfig::GetOneCapability(const struct DeviceResourceIface &iface,
179                                                const struct DeviceResourceNode &childNode,
180                                                CodecCompCapability &cap, bool isVideoGroup)
181 {
182     if (iface.GetUint32(&childNode, CODEC_CONFIG_KEY_ROLE, reinterpret_cast<uint32_t *>(&cap.role),
183                         MEDIA_ROLETYPE_INVALID) != HDF_SUCCESS) {
184         cap.role = MEDIA_ROLETYPE_INVALID;
185         CODEC_LOGE("failed to get mime for: %{public}s! Discarded", childNode.name);
186         return HDF_FAILURE;
187     }
188     if (iface.GetUint32(&childNode, CODEC_CONFIG_KEY_TYPE, reinterpret_cast<uint32_t *>(&cap.type), INVALID_TYPE) !=
189         HDF_SUCCESS) {
190         cap.role = MEDIA_ROLETYPE_INVALID;
191         cap.type = INVALID_TYPE;
192         CODEC_LOGE("failed to get type for: %{public}s! Discarded", childNode.name);
193         return HDF_FAILURE;
194     }
195 
196     const char *compName = nullptr;
197     if (iface.GetString(&childNode, CODEC_CONFIG_KEY_NAME, &compName, "") != HDF_SUCCESS) {
198         cap.role = MEDIA_ROLETYPE_INVALID;
199         CODEC_LOGE("get attr %{public}s err!", CODEC_CONFIG_KEY_NAME);
200         return HDF_FAILURE;
201     }
202     if (compName == nullptr || strlen(compName) == 0) {
203         cap.role = MEDIA_ROLETYPE_INVALID;
204         CODEC_LOGE("compName is nullptr or empty!");
205         return HDF_FAILURE;
206     }
207     cap.compName = compName;
208 
209     cap.isSoftwareCodec = iface.GetBool(&childNode, CODEC_CONFIG_KEY_IS_SOFTWARE_CODEC);
210     cap.canSwapWidthHeight = iface.GetBool(&childNode, CODEC_CONFIG_KEY_CAN_SWAP_WIDTH_HEIGHT);
211 
212     if (GetMiscOfCapability(iface, childNode, cap) != HDF_SUCCESS) {
213         cap.role = MEDIA_ROLETYPE_INVALID;
214         CODEC_LOGE("get misc cap  err!");
215         return HDF_FAILURE;
216     }
217     if (isVideoGroup) {
218         if (GetVideoPortCapability(iface, childNode, cap) != HDF_SUCCESS) {
219             cap.role = MEDIA_ROLETYPE_INVALID;
220             CODEC_LOGE("get video port cap  err!");
221             return HDF_FAILURE;
222         }
223     } else {
224         if (GetAudioPortCapability(iface, childNode, cap) != HDF_SUCCESS) {
225             cap.role = MEDIA_ROLETYPE_INVALID;
226             CODEC_LOGE("get audio port cap  err!");
227             return HDF_FAILURE;
228         }
229     }
230 
231     return HDF_SUCCESS;
232 }
233 
GetMiscOfCapability(const struct DeviceResourceIface & iface,const struct DeviceResourceNode & childNode,CodecCompCapability & cap)234 int32_t CodecComponentConfig::GetMiscOfCapability(const struct DeviceResourceIface &iface,
235                                                   const struct DeviceResourceNode &childNode, CodecCompCapability &cap)
236 {
237     ConfigUintArrayNodeAttr attr = {CODEC_CONFIG_KEY_SUPPORT_PROFILES, cap.supportProfiles};
238     if (GetUintTableConfig(iface, childNode, attr) != HDF_SUCCESS) {
239         CODEC_LOGE("get uint table config [%{public}s] err!", attr.attrName.c_str());
240         return HDF_FAILURE;
241     }
242 
243     if (iface.GetUint32(&childNode, CODEC_CONFIG_KEY_MAX_INST, reinterpret_cast<uint32_t *>(&cap.maxInst), 0) !=
244         HDF_SUCCESS) {
245         CODEC_LOGE("get uint32 config [%{public}s] err!", attr.attrName.c_str());
246         return HDF_FAILURE;
247     }
248     if (GetMaskedConfig(iface, childNode, CODEC_CONFIG_KEY_PROCESS_MODE_MASK,
249                         reinterpret_cast<uint32_t &>(cap.processModeMask)) != HDF_SUCCESS) {
250         CODEC_LOGE("get masked config [%{public}s] err!", attr.attrName.c_str());
251         return HDF_FAILURE;
252     }
253     if (GetMaskedConfig(iface, childNode, CODEC_CONFIG_KEY_CAPS_MASK, static_cast<uint32_t &>(cap.capsMask)) !=
254         HDF_SUCCESS) {
255         CODEC_LOGE("get masked config [%{public}s] err!", attr.attrName.c_str());
256         return HDF_FAILURE;
257     }
258     if (iface.GetUint32(&childNode, CODEC_CONFIG_KEY_MIN_BITRATE, reinterpret_cast<uint32_t *>(&cap.bitRate.min), 0) !=
259         HDF_SUCCESS) {
260         CODEC_LOGE("get uin32 config [%{public}s] err!", attr.attrName.c_str());
261         return HDF_FAILURE;
262     }
263     if (iface.GetUint32(&childNode, CODEC_CONFIG_KEY_MAX_BITRATE, reinterpret_cast<uint32_t *>(&cap.bitRate.max), 0) !=
264         HDF_SUCCESS) {
265         CODEC_LOGE("get uin32 config [%{public}s] err!", attr.attrName.c_str());
266         return HDF_FAILURE;
267     }
268 
269     return HDF_SUCCESS;
270 }
271 
GetUintTableConfig(const struct DeviceResourceIface & iface,const struct DeviceResourceNode & node,ConfigUintArrayNodeAttr & attr)272 int32_t CodecComponentConfig::GetUintTableConfig(const struct DeviceResourceIface &iface,
273                                                  const struct DeviceResourceNode &node, ConfigUintArrayNodeAttr &attr)
274 {
275     if (attr.attrName.empty()) {
276         CODEC_LOGE("failed, invalid attr!");
277         return HDF_ERR_INVALID_PARAM;
278     }
279 
280     int32_t count = iface.GetElemNum(&node, attr.attrName.c_str());
281     if (count < 0) {
282         CODEC_LOGE("%{public}s table size: count[%{public}d] < 0!", attr.attrName.c_str(), count);
283         return HDF_FAILURE;
284     }
285     if (count > 0) {
286         std::unique_ptr<int32_t[]> array = std::make_unique<int32_t[]>(count);
287         iface.GetUint32Array(&node, attr.attrName.c_str(), reinterpret_cast<uint32_t *>(array.get()), count, 0);
288         attr.vec.assign(array.get(), array.get() + count);
289     }
290     return HDF_SUCCESS;
291 }
292 
GetMaskedConfig(const struct DeviceResourceIface & iface,const struct DeviceResourceNode & node,const std::string & attrName,uint32_t & mask)293 int32_t CodecComponentConfig::GetMaskedConfig(const struct DeviceResourceIface &iface,
294                                               const struct DeviceResourceNode &node, const std::string &attrName,
295                                               uint32_t &mask)
296 {
297     int32_t count = iface.GetElemNum(&node, attrName.c_str());
298 
299     mask = 0;
300     if (count < 0 || count > MASK_NUM_LIMIT) {
301         CODEC_LOGE("failed, count %{public}d incorrect!", count);
302         return HDF_FAILURE;
303     }
304 
305     if (count > 0) {
306         std::unique_ptr<uint32_t[]> values = std::make_unique<uint32_t[]>(count);
307         iface.GetUint32Array(&node, attrName.c_str(), values.get(), count, 0);
308         for (int32_t index = 0; index < count; index++) {
309             mask |= values[index];
310         }
311     }
312 
313     return HDF_SUCCESS;
314 }
315 
GetVideoPortCapability(const struct DeviceResourceIface & iface,const struct DeviceResourceNode & childNode,CodecCompCapability & cap)316 int32_t CodecComponentConfig::GetVideoPortCapability(const struct DeviceResourceIface &iface,
317                                                      const struct DeviceResourceNode &childNode,
318                                                      CodecCompCapability &cap)
319 {
320     ConfigUintNodeAttr nodeAttrs[] = {
321         {CODEC_CONFIG_KEY_MIN_WIDTH, cap.port.video.minSize.width, 0},
322         {CODEC_CONFIG_KEY_MIN_HEIGHT, cap.port.video.minSize.height, 0},
323         {CODEC_CONFIG_KEY_MAX_WIDTH, cap.port.video.maxSize.width, 0},
324         {CODEC_CONFIG_KEY_MAX_HEIGHT, cap.port.video.maxSize.height, 0},
325         {CODEC_CONFIG_KEY_WIDTH_ALIGNMENT, cap.port.video.whAlignment.widthAlignment, 0},
326         {CODEC_CONFIG_KEY_HEIGHT_ALIGNMENT, cap.port.video.whAlignment.heightAlignment, 0},
327         {CODEC_CONFIG_KEY_MIN_BLOCK_COUNT, cap.port.video.blockCount.min, 0},
328         {CODEC_CONFIG_KEY_MAX_BLOCK_COUNT, cap.port.video.blockCount.max, 0},
329         {CODEC_CONFIG_KEY_MIN_BLOCKS_PER_SECOND, cap.port.video.blocksPerSecond.min, 0},
330         {CODEC_CONFIG_KEY_MAX_BLOCKS_PER_SECOND, cap.port.video.blocksPerSecond.max, 0},
331         {CODEC_CONFIG_KEY_BLOCK_SIZE_WIDTH, cap.port.video.blockSize.width, 0},
332         {CODEC_CONFIG_KEY_BLOCK_SIZE_HEIGHT, cap.port.video.blockSize.height, 0},
333         {CODEC_CONFIG_KEY_MIN_FRAME_RATE, cap.port.video.frameRate.min, 0},
334         {CODEC_CONFIG_KEY_MAX_FRAME_RATE, cap.port.video.frameRate.max, 0}};
335 
336     int32_t count = sizeof(nodeAttrs) / sizeof(ConfigUintNodeAttr);
337     for (int32_t i = 0; i < count; i++) {
338         if (iface.GetUint32(&childNode, nodeAttrs[i].attrName.c_str(),
339                             reinterpret_cast<uint32_t *>(&nodeAttrs[i].value),
340                             nodeAttrs[i].defaultValue) != HDF_SUCCESS) {
341             CODEC_LOGE("failed to get %{public}s.%{public}s!", childNode.name, nodeAttrs[i].attrName.c_str());
342             return HDF_FAILURE;
343         }
344     }
345     ConfigUintArrayNodeAttr arrayAttrs[] = {
346         {CODEC_CONFIG_KEY_SUPPORT_PIXEL_FMTS, cap.port.video.supportPixFmts},
347         {CODEC_CONFIG_KEY_BITE_RATE_MODE, reinterpret_cast<std::vector<int32_t> &>(cap.port.video.bitRatemode)},
348         {CODEC_CONFIG_KEY_MESURED_FRAME_RATE, cap.port.video.measuredFrameRate}};
349 
350     count = sizeof(arrayAttrs) / sizeof(ConfigUintArrayNodeAttr);
351     for (int32_t i = 0; i < count; i++) {
352         if (GetUintTableConfig(iface, childNode, arrayAttrs[i]) != HDF_SUCCESS) {
353             CODEC_LOGE("failed to get %{public}s.%{public}s!", childNode.name, nodeAttrs[i].attrName.c_str());
354             return HDF_FAILURE;
355         }
356     }
357     return HDF_SUCCESS;
358 }
359 
GetAudioPortCapability(const struct DeviceResourceIface & iface,const struct DeviceResourceNode & childNode,CodecCompCapability & cap)360 int32_t CodecComponentConfig::GetAudioPortCapability(const struct DeviceResourceIface &iface,
361                                                      const struct DeviceResourceNode &childNode,
362                                                      CodecCompCapability &cap)
363 {
364     ConfigUintArrayNodeAttr arrayAttrs[] = {{CODEC_CONFIG_KEY_SAMPLE_FORMATS, cap.port.audio.sampleFormats},
365                                             {CODEC_CONFIG_KEY_SAMPLE_RATE, cap.port.audio.sampleRate},
366                                             {CODEC_CONFIG_KEY_CHANNEL_LAYOUTS, cap.port.audio.channelLayouts},
367                                             {CODEC_CONFIG_KEY_CHANNEL_COUNT, cap.port.audio.channelCount}};
368 
369     int32_t count = sizeof(arrayAttrs) / sizeof(ConfigUintArrayNodeAttr);
370     for (int32_t i = 0; i < count; i++) {
371         if (GetUintTableConfig(iface, childNode, arrayAttrs[i]) != HDF_SUCCESS) {
372             CODEC_LOGE("failed to get %{public}s.%{public}s!", childNode.name, arrayAttrs[i].attrName.c_str());
373             return HDF_FAILURE;
374         }
375     }
376 
377     return HDF_SUCCESS;
378 }
379 }  // namespace Omx
380 }  // namespace Codec
381 }  // namespace OHOS