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