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