1 /*
2 * Copyright (c) 2022 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 #include <hdf_base.h>
17 #include <hdf_log.h>
18 #include <memory>
19 #include "codec_adapter_if.h"
20 #include "codec_capability_parser.h"
21 #include "codec_component_capability.h"
22 #include "component_manager.h"
23 #include "component_node.h"
24 #include "codec_omx_ext.h"
25 using namespace OHOS::Codec::CodecAdapter;
26
27 #define HDF_LOG_TAG codec_hdi_adapter
28
29 static ComponentManager g_mgr;
30 struct CodecComponentNode {
31 std::shared_ptr<ComponentNode> node;
32 };
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
CodecAdapterCodecInit()38 int32_t CodecAdapterCodecInit()
39 {
40 return g_mgr.Init();
41 }
42
CodecAdapterCodecDeinit()43 int32_t CodecAdapterCodecDeinit()
44 {
45 return g_mgr.Deinit();
46 }
47
CodecAdapterCreateComponent(struct CodecComponentNode ** codecNode,const char * compName,int64_t appData,const struct CodecCallbackType * callbacks)48 int32_t CodecAdapterCreateComponent(struct CodecComponentNode **codecNode, const char *compName, int64_t appData,
49 const struct CodecCallbackType *callbacks)
50 {
51 if (compName == nullptr || callbacks == nullptr) {
52 HDF_LOGE("%{public}s compName or callbacks is null", __func__);
53 return HDF_ERR_INVALID_PARAM;
54 }
55
56 CodecExInfo exInfo;
57 auto ret = GetBasicInfoByCompName(reinterpret_cast<uint8_t *>(&exInfo), compName);
58 if (ret != HDF_SUCCESS) {
59 HDF_LOGE("%{public}s GetBasicInfoByCompName error", __func__);
60 return ret;
61 }
62
63 CODEC_HANDLETYPE comp = nullptr;
64 CodecComponentNode *tempNode = new CodecComponentNode;
65 if (tempNode == nullptr) {
66 HDF_LOGE("%{public}s create CodecComponentNode error", __func__);
67 return HDF_ERR_MALLOC_FAIL;
68 }
69
70 ret = g_mgr.CreateComponentInstance(compName, comp);
71 if (ret != HDF_SUCCESS) {
72 HDF_LOGE("%{public}s ceate component instance ret[%{public}d]", __func__, ret);
73 delete tempNode;
74 tempNode = nullptr;
75 return ret;
76 }
77
78 tempNode->node = std::make_shared<ComponentNode>(comp, exInfo);
79 if (tempNode->node == nullptr) {
80 HDF_LOGE("fail to init ComponentNode");
81 return HDF_FAILURE;
82 }
83 ret = tempNode->node->SetCallbacks(callbacks, appData);
84 if (ret != HDF_SUCCESS) {
85 HDF_LOGE("%{public}s SetCallbacks error", __func__);
86 g_mgr.DeleteComponentInstance(comp);
87 tempNode->node = nullptr;
88 delete tempNode;
89 tempNode = nullptr;
90 return ret;
91 }
92
93 tempNode->node->SetState(OMX_StateLoaded);
94
95 *codecNode = tempNode;
96 return HDF_SUCCESS;
97 }
98
CodecAdapterDestroyComponent(struct CodecComponentNode * codecNode)99 int32_t CodecAdapterDestroyComponent(struct CodecComponentNode *codecNode)
100 {
101 if (codecNode == nullptr || codecNode->node == nullptr) {
102 HDF_LOGE("%{public}s codecNode is null or node is null", __func__);
103 return HDF_ERR_INVALID_PARAM;
104 }
105 auto ret = g_mgr.DeleteComponentInstance(codecNode->node->GetHandle());
106 if (ret != HDF_SUCCESS) {
107 HDF_LOGE("%{public}s DeleteComponentInstance ret[%{public}d]", __func__, ret);
108 return ret;
109 }
110 codecNode->node = nullptr;
111 delete codecNode;
112 codecNode = nullptr;
113 return HDF_SUCCESS;
114 }
115
CodecAdapterGetComponentVersion(const struct CodecComponentNode * codecNode,struct CompVerInfo * verInfo)116 int32_t CodecAdapterGetComponentVersion(const struct CodecComponentNode *codecNode, struct CompVerInfo *verInfo)
117 {
118 if (codecNode == nullptr || codecNode->node == nullptr || verInfo == nullptr) {
119 HDF_LOGE("%{public}s codecNode, node or verInfois is null", __func__);
120 return HDF_ERR_INVALID_PARAM;
121 }
122 return codecNode->node->GetComponentVersion(*verInfo);
123 }
124
CodecAdapterSendCommand(const struct CodecComponentNode * codecNode,OMX_COMMANDTYPE cmd,uint32_t param,int8_t * cmdData,uint32_t cmdDataLen)125 int32_t CodecAdapterSendCommand(const struct CodecComponentNode *codecNode, OMX_COMMANDTYPE cmd, uint32_t param,
126 int8_t *cmdData, uint32_t cmdDataLen)
127 {
128 if (codecNode == nullptr || codecNode->node == nullptr) {
129 HDF_LOGE("%{public}s codecNode or node is null", __func__);
130 return HDF_ERR_INVALID_PARAM;
131 }
132 return codecNode->node->SendCommand(cmd, param, cmdData, cmdDataLen);
133 }
134
CodecAdapterGetParameter(const struct CodecComponentNode * codecNode,OMX_INDEXTYPE paramIndex,int8_t * param,uint32_t paramLen)135 int32_t CodecAdapterGetParameter(
136 const struct CodecComponentNode *codecNode, OMX_INDEXTYPE paramIndex, int8_t *param, uint32_t paramLen)
137 {
138 if (codecNode == nullptr || codecNode->node == nullptr || param == nullptr) {
139 HDF_LOGE("%{public}s codecNode, node or param is null", __func__);
140 return HDF_ERR_INVALID_PARAM;
141 }
142 if (!CheckParamStructLen(paramIndex, paramLen)) {
143 HDF_LOGE("%{public}s param is invalid", __func__);
144 return HDF_ERR_INVALID_PARAM;
145 }
146
147 return codecNode->node->GetParameter(paramIndex, param, paramLen);
148 }
149
CodecAdapterSetParameter(const struct CodecComponentNode * codecNode,OMX_INDEXTYPE index,const int8_t * param,uint32_t paramLen)150 int32_t CodecAdapterSetParameter(
151 const struct CodecComponentNode *codecNode, OMX_INDEXTYPE index, const int8_t *param, uint32_t paramLen)
152 {
153 if (codecNode == nullptr || codecNode->node == nullptr || param == nullptr) {
154 HDF_LOGE("%{public}s codecNode, node or param is null", __func__);
155 return HDF_ERR_INVALID_PARAM;
156 }
157 if (!CheckParamStructLen(index, paramLen)) {
158 HDF_LOGE("%{public}s param is invalid", __func__);
159 return HDF_ERR_INVALID_PARAM;
160 }
161
162 return codecNode->node->SetParameter(index, param, paramLen);
163 }
164
CodecAdapterGetConfig(const struct CodecComponentNode * codecNode,OMX_INDEXTYPE index,int8_t * config,uint32_t configLen)165 int32_t CodecAdapterGetConfig(
166 const struct CodecComponentNode *codecNode, OMX_INDEXTYPE index, int8_t *config, uint32_t configLen)
167 {
168 if (codecNode == nullptr || codecNode->node == nullptr || config == nullptr) {
169 HDF_LOGE("%{public}s codecNode, node or config is null", __func__);
170 return HDF_ERR_INVALID_PARAM;
171 }
172 return codecNode->node->GetConfig(index, config, configLen);
173 }
174
CodecAdapterSetConfig(const struct CodecComponentNode * codecNode,OMX_INDEXTYPE index,const int8_t * config,uint32_t configLen)175 int32_t CodecAdapterSetConfig(
176 const struct CodecComponentNode *codecNode, OMX_INDEXTYPE index, const int8_t *config, uint32_t configLen)
177 {
178 if (codecNode == nullptr || codecNode->node == nullptr || config == nullptr) {
179 HDF_LOGE("%{public}s codecNode, node or config is null", __func__);
180 return HDF_ERR_INVALID_PARAM;
181 }
182 return codecNode->node->SetConfig(index, config, configLen);
183 }
184
CodecAdapterGetExtensionIndex(const struct CodecComponentNode * codecNode,const char * parameterName,OMX_INDEXTYPE * indexType)185 int32_t CodecAdapterGetExtensionIndex(
186 const struct CodecComponentNode *codecNode, const char *parameterName, OMX_INDEXTYPE *indexType)
187 {
188 if (codecNode == nullptr || codecNode->node == nullptr || parameterName == nullptr || indexType == nullptr) {
189 HDF_LOGE("%{public}s codecNode, node, parameterName or indexType is null", __func__);
190 return HDF_ERR_INVALID_PARAM;
191 }
192 return codecNode->node->GetExtensionIndex(parameterName, indexType);
193 }
194
CodecAdapterGetState(const struct CodecComponentNode * codecNode,OMX_STATETYPE * state)195 int32_t CodecAdapterGetState(const struct CodecComponentNode *codecNode, OMX_STATETYPE *state)
196 {
197 if (codecNode == nullptr || codecNode->node == nullptr || state == nullptr) {
198 HDF_LOGE("%{public}s codecNode, node or state is null", __func__);
199 return HDF_ERR_INVALID_PARAM;
200 }
201 return codecNode->node->GetState(state);
202 }
203
CodecAdapterComponentTunnelRequest(const struct CodecComponentNode * codecNode,uint32_t port,int32_t omxHandleTypeTunneledComp,uint32_t tunneledPort,struct OMX_TUNNELSETUPTYPE * tunnelSetup)204 int32_t CodecAdapterComponentTunnelRequest(const struct CodecComponentNode *codecNode, uint32_t port,
205 int32_t omxHandleTypeTunneledComp, uint32_t tunneledPort, struct OMX_TUNNELSETUPTYPE *tunnelSetup)
206 {
207 if (codecNode == nullptr || codecNode->node == nullptr || tunnelSetup == nullptr) {
208 HDF_LOGE("%{public}s codecNode, node or tunnelSetup is null", __func__);
209 return HDF_ERR_INVALID_PARAM;
210 }
211 return codecNode->node->ComponentTunnelRequest(port, omxHandleTypeTunneledComp, tunneledPort, tunnelSetup);
212 }
213
CodecAdapterUseBuffer(const struct CodecComponentNode * codecNode,uint32_t portIndex,struct OmxCodecBuffer * omxBuffer)214 int32_t CodecAdapterUseBuffer(
215 const struct CodecComponentNode *codecNode, uint32_t portIndex, struct OmxCodecBuffer *omxBuffer)
216 {
217 if (codecNode == nullptr || codecNode->node == nullptr || omxBuffer == nullptr) {
218 HDF_LOGE("%{public}s codecNode, node or omxBuffer is null", __func__);
219 return HDF_ERR_INVALID_PARAM;
220 }
221 return codecNode->node->UseBuffer(portIndex, *omxBuffer);
222 }
223
CodecAdapterAllocateBuffer(const struct CodecComponentNode * codecNode,uint32_t portIndex,struct OmxCodecBuffer * omxBuffer)224 int32_t CodecAdapterAllocateBuffer(
225 const struct CodecComponentNode *codecNode, uint32_t portIndex, struct OmxCodecBuffer *omxBuffer)
226 {
227 if (codecNode == nullptr || codecNode->node == nullptr || omxBuffer == nullptr) {
228 HDF_LOGE("%{public}s codecNode, node or omxBuffer is null", __func__);
229 return HDF_ERR_INVALID_PARAM;
230 }
231 return codecNode->node->AllocateBuffer(portIndex, *omxBuffer);
232 }
233
CodecAdapterFreeBuffer(const struct CodecComponentNode * codecNode,uint32_t portIndex,const struct OmxCodecBuffer * omxBuffer)234 int32_t CodecAdapterFreeBuffer(
235 const struct CodecComponentNode *codecNode, uint32_t portIndex, const struct OmxCodecBuffer *omxBuffer)
236 {
237 if (codecNode == nullptr || codecNode->node == nullptr || omxBuffer == nullptr) {
238 HDF_LOGE("%{public}s codecNode, node or omxBuffer is null", __func__);
239 return HDF_ERR_INVALID_PARAM;
240 }
241 return codecNode->node->FreeBuffer(portIndex, *omxBuffer);
242 }
243
CodecAdapterEmptyThisBuffer(const struct CodecComponentNode * codecNode,const struct OmxCodecBuffer * omxBuffer)244 int32_t CodecAdapterEmptyThisBuffer(const struct CodecComponentNode *codecNode, const struct OmxCodecBuffer *omxBuffer)
245 {
246 if (codecNode == nullptr || codecNode->node == nullptr || omxBuffer == nullptr) {
247 HDF_LOGE("%{public}s codecNode, node or omxBuffer is null", __func__);
248 return HDF_ERR_INVALID_PARAM;
249 }
250 return codecNode->node->EmptyThisBuffer(*omxBuffer);
251 }
252
CodecAdapterFillThisBuffer(const struct CodecComponentNode * codecNode,const struct OmxCodecBuffer * omxBuffer)253 int32_t CodecAdapterFillThisBuffer(const struct CodecComponentNode *codecNode, const struct OmxCodecBuffer *omxBuffer)
254 {
255 if (codecNode == nullptr || codecNode->node == nullptr || omxBuffer == nullptr) {
256 HDF_LOGE("%{public}s codecNode, node or omxBuffer is null", __func__);
257 return HDF_ERR_INVALID_PARAM;
258 }
259 return codecNode->node->FillThisBuffer(*omxBuffer);
260 }
261
CodecAdapterSetCallbacks(const struct CodecComponentNode * codecNode,struct CodecCallbackType * omxCallback,int64_t appData)262 int32_t CodecAdapterSetCallbacks(
263 const struct CodecComponentNode *codecNode, struct CodecCallbackType *omxCallback, int64_t appData)
264 {
265 if (codecNode == nullptr || codecNode->node == nullptr || omxCallback == nullptr) {
266 HDF_LOGE("%{public}s codecNode, node or omxCallback is null", __func__);
267 return HDF_ERR_INVALID_PARAM;
268 }
269 return codecNode->node->SetCallbacks(omxCallback, appData);
270 }
271
CodecAdapterComponentDeInit(const struct CodecComponentNode * codecNode)272 int32_t CodecAdapterComponentDeInit(const struct CodecComponentNode *codecNode)
273 {
274 if (codecNode == nullptr || codecNode->node == nullptr) {
275 HDF_LOGE("%{public}s codecNode or node is null", __func__);
276 return HDF_ERR_INVALID_PARAM;
277 }
278 return codecNode->node->ComponentDeInit();
279 }
280
CodecAdapterUseEglImage(const struct CodecComponentNode * codecNode,struct OmxCodecBuffer * buffer,uint32_t portIndex,int8_t * eglImage,uint32_t eglImageLen)281 int32_t CodecAdapterUseEglImage(const struct CodecComponentNode *codecNode, struct OmxCodecBuffer *buffer,
282 uint32_t portIndex, int8_t *eglImage, uint32_t eglImageLen)
283 {
284 if (codecNode == nullptr || codecNode->node == nullptr || buffer == nullptr || eglImage == nullptr) {
285 HDF_LOGE("%{public}s codecNode, node, buffer or eglImage is null", __func__);
286 return HDF_ERR_INVALID_PARAM;
287 }
288 return codecNode->node->UseEglImage(*buffer, portIndex, eglImage, eglImageLen);
289 }
290
CodecAdapterComponentRoleEnum(const struct CodecComponentNode * codecNode,uint8_t * role,uint32_t roleLen,uint32_t index)291 int32_t CodecAdapterComponentRoleEnum(
292 const struct CodecComponentNode *codecNode, uint8_t *role, uint32_t roleLen, uint32_t index)
293 {
294 if (codecNode == nullptr || codecNode->node == nullptr || role == nullptr) {
295 HDF_LOGE("%{public}s codecNode, node or role is null", __func__);
296 return HDF_ERR_INVALID_PARAM;
297 }
298 return codecNode->node->ComponentRoleEnum(role, roleLen, index);
299 }
300
CheckParamStructLen(int32_t paramIndex,uint32_t paramLen)301 bool CheckParamStructLen(int32_t paramIndex, uint32_t paramLen)
302 {
303 uint32_t paramStructLen = 0;
304 switch (paramIndex) {
305 case OMX_IndexParamPortDefinition:
306 paramStructLen = sizeof(OMX_PARAM_PORTDEFINITIONTYPE);
307 break;
308 case OMX_IndexParamAudioPortFormat:
309 paramStructLen = sizeof(OMX_AUDIO_PARAM_PORTFORMATTYPE);
310 break;
311 case OMX_IndexParamAudioPcm:
312 paramStructLen = sizeof(OMX_AUDIO_PARAM_PCMMODETYPE);
313 break;
314 case OMX_IndexParamAudioAac:
315 paramStructLen = sizeof(OMX_AUDIO_PARAM_AACPROFILETYPE);
316 break;
317 case OMX_IndexParamAudioMp3:
318 paramStructLen = sizeof(OMX_AUDIO_PARAM_MP3TYPE);
319 break;
320 case OMX_IndexParamAudioG726:
321 paramStructLen = sizeof(OMX_AUDIO_PARAM_G726TYPE);
322 break;
323 case OMX_IndexParamImagePortFormat:
324 paramStructLen = sizeof(OMX_IMAGE_PARAM_PORTFORMATTYPE);
325 break;
326 case OMX_IndexParamQFactor:
327 paramStructLen = sizeof(OMX_IMAGE_PARAM_QFACTORTYPE);
328 break;
329 case OMX_IndexParamVideoPortFormat:
330 paramStructLen = sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE);
331 break;
332 case OMX_IndexParamVideoMpeg2:
333 paramStructLen = sizeof(OMX_VIDEO_PARAM_MPEG2TYPE);
334 break;
335 case OMX_IndexParamVideoMpeg4:
336 paramStructLen = sizeof(OMX_VIDEO_PARAM_MPEG4TYPE);
337 break;
338 case OMX_IndexParamVideoAvc:
339 paramStructLen = sizeof(OMX_VIDEO_PARAM_AVCTYPE);
340 break;
341 case OMX_IndexParamVideoBitrate:
342 paramStructLen = sizeof(OMX_VIDEO_PARAM_BITRATETYPE);
343 break;
344 case OMX_IndexParamPassthrough:
345 paramStructLen = sizeof(PassthroughParam);
346 break;
347
348 default:
349 return false;
350 }
351 return (paramStructLen == paramLen);
352 }
353 #ifdef __cplusplus
354 };
355 #endif
356