• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
16 #include "config_parser.h"
17 #include <osal_mem.h>
18 #include "hdf_base.h"
19 #include "hdf_log.h"
20 #include "codec_utils.h"
21 
22 #define HDF_LOG_TAG "codec_config_parser"
23 
GetGroupCapabilitiesNumber(const struct DeviceResourceNode * node,const char * nodeName,int32_t * num)24 static int32_t GetGroupCapabilitiesNumber(const struct DeviceResourceNode *node,
25     const char *nodeName, int32_t *num)
26 {
27     int result = 0;
28     const struct DeviceResourceNode *codecGroupNode = NULL;
29     struct DeviceResourceNode *childNode = NULL;
30     struct DeviceResourceIface *iface = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
31 
32     *num = 0;
33     if (iface ==NULL || node == NULL || nodeName == NULL) {
34         HDF_LOGE("%{public}s, failed for codecs %{public}s, variable NULL!", __func__, nodeName);
35         return HDF_FAILURE;
36     }
37 
38     codecGroupNode = iface->GetChildNode(node, nodeName);
39     if (codecGroupNode == NULL) {
40         HDF_LOGE("%{public}s, failed to get child node %{public}s,!", __func__, nodeName);
41         return HDF_FAILURE;
42     }
43     DEV_RES_NODE_FOR_EACH_CHILD_NODE(codecGroupNode, childNode) {
44         result++;
45     }
46     *num = result;
47 
48     return HDF_SUCCESS;
49 }
50 
GetUintTableConfig(const struct DeviceResourceIface * iface,const struct DeviceResourceNode * node,const char * attrName,ResizableArray * table)51 static int32_t GetUintTableConfig(const struct DeviceResourceIface *iface,
52     const struct DeviceResourceNode *node, const char *attrName, ResizableArray *table)
53 {
54     int32_t count = iface->GetElemNum(node, attrName);
55 
56     table->actualLen = 0;
57     if (count <= 0) {
58         return HDF_FAILURE;
59     }
60     if (count > ELEMENT_MAX_LEN) {
61         HDF_LOGE("%{public}s, table size: %{public}d, exceeded max size %{public}d!",
62             __func__, count, ELEMENT_MAX_LEN);
63         return HDF_FAILURE;
64     }
65 
66     iface->GetUint32Array(node, attrName, table->element, count, 0);
67     table->actualLen = count;
68 
69     return HDF_SUCCESS;
70 }
71 
GetMaskedConfig(const struct DeviceResourceIface * iface,const struct DeviceResourceNode * node,const char * attrName,uint32_t * mask)72 static int32_t GetMaskedConfig(const struct DeviceResourceIface *iface,
73     const struct DeviceResourceNode *node, const char *attrName, uint32_t *mask)
74 {
75     int32_t index = 0;
76     uint32_t *values = NULL;
77     int32_t count = iface->GetElemNum(node, attrName);
78 
79     *mask = 0;
80     if (count <= 0) {
81         return HDF_FAILURE;
82     }
83 
84     values = (uint32_t *)OsalMemAlloc(sizeof(uint32_t) * count);
85     if (values == NULL) {
86         HDF_LOGE("%{public}s, failed to allocate mem for %{public}s!", __func__, attrName);
87         return HDF_FAILURE;
88     }
89     iface->GetUint32Array(node, attrName, values, count, 0);
90     for (index = 0; index < count; index++) {
91         *mask |= values[index];
92     }
93     OsalMemFree(values);
94 
95     return HDF_SUCCESS;
96 }
97 
GetOneCapability(const struct DeviceResourceIface * iface,const struct DeviceResourceNode * childNode,CodecCapbility * cap)98 static int32_t GetOneCapability(const struct DeviceResourceIface *iface,
99     const struct DeviceResourceNode *childNode, CodecCapbility *cap)
100 {
101     if (iface == NULL || childNode == NULL || cap == NULL) {
102         return HDF_FAILURE;
103     }
104     if (iface->GetUint32(childNode, CODEC_CONFIG_KEY_MIME,
105         (uint32_t*)&cap->mime, MEDIA_MIMETYPE_INVALID) != HDF_SUCCESS) {
106         cap->mime = MEDIA_MIMETYPE_INVALID;
107         HDF_LOGE("%{public}s, failed to get mime for: %{public}s! Discarded", __func__, childNode->name);
108         return HDF_FAILURE;
109     }
110 
111     if (iface->GetUint32(childNode, CODEC_CONFIG_KEY_TYPE, (uint32_t*)&cap->type, INVALID_TYPE) != HDF_SUCCESS) {
112         cap->mime = MEDIA_MIMETYPE_INVALID;
113         cap->type = INVALID_TYPE;
114         HDF_LOGE("%{public}s, failed to get type for: %{public}s! Discarded", __func__, childNode->name);
115         return HDF_FAILURE;
116     }
117 
118     if (iface->GetUint32(childNode, CODEC_CONFIG_KEY_WIDTH_ALGINMENT,
119         (uint32_t*)&cap->whAlignment.widthAlginment, 0) != HDF_SUCCESS
120         || iface->GetUint32(childNode, CODEC_CONFIG_KEY_HEIGHT_ALGINMENT,
121             (uint32_t*)&cap->whAlignment.heightAlginment, 0) != HDF_SUCCESS
122         || iface->GetUint32(childNode, CODEC_CONFIG_KEY_MIN_WIDTH, (uint32_t*)&cap->minSize.width, 0) != HDF_SUCCESS
123         || iface->GetUint32(childNode, CODEC_CONFIG_KEY_MIN_HEIGHT, (uint32_t*)&cap->minSize.height, 0) != HDF_SUCCESS
124         || iface->GetUint32(childNode, CODEC_CONFIG_KEY_MAX_WIDTH, (uint32_t*)&cap->maxSize.width, 0) != HDF_SUCCESS
125         || iface->GetUint32(childNode, CODEC_CONFIG_KEY_MAX_HEIGHT, (uint32_t*)&cap->maxSize.height, 0) != HDF_SUCCESS
126         || iface->GetUint32(childNode, CODEC_CONFIG_KEY_MIN_BITRATE, (uint32_t*)&cap->minBitRate, 0) != HDF_SUCCESS
127         || iface->GetUint32(childNode, CODEC_CONFIG_KEY_MAX_BITRATE, (uint32_t*)&cap->maxBitRate, 0) != HDF_SUCCESS
128         || GetUintTableConfig(iface, childNode, CODEC_CONFIG_KEY_SUPPORT_PROFILES,
129             &(cap->supportProfiles)) != HDF_SUCCESS
130         || GetUintTableConfig(iface, childNode, CODEC_CONFIG_KEY_SUPPORT_LEVELS, &(cap->supportLevels)) != HDF_SUCCESS
131         || GetUintTableConfig(iface, childNode, CODEC_CONFIG_KEY_SUPPORT_PIXELF_MTS,
132             &(cap->supportPixelFormats)) != HDF_SUCCESS
133         || iface->GetUint32(childNode, CODEC_CONFIG_KEY_MIN_INPUT_BUFFER_NUM,
134             &cap->minInputBufferNum, 0) != HDF_SUCCESS
135         || iface->GetUint32(childNode, CODEC_CONFIG_KEY_MIN_OUTPUT_BUFFER_NUM,
136             &cap->minOutputBufferNum, 0) != HDF_SUCCESS
137         || GetMaskedConfig(iface, childNode, CODEC_CONFIG_KEY_ALLOCATE_MASK, &cap->allocateMask) != HDF_SUCCESS
138         || GetMaskedConfig(iface, childNode, CODEC_CONFIG_KEY_CAPS_MASK, &cap->capsMask) != HDF_SUCCESS) {
139             cap->mime = MEDIA_MIMETYPE_INVALID;
140             return HDF_FAILURE;
141     }
142 
143     return HDF_SUCCESS;
144 }
145 
GetGroupCapabilities(const struct DeviceResourceNode * node,const char * nodeName,CodecCapablityGroup * capsGroup)146 static int32_t GetGroupCapabilities(const struct DeviceResourceNode *node,
147     const char *nodeName, CodecCapablityGroup *capsGroup)
148 {
149     CodecCapbility *cap;
150     int32_t index = 0;
151     const struct DeviceResourceNode *codecGroupNode = NULL;
152     struct DeviceResourceNode *childNode = NULL;
153     struct DeviceResourceIface *iface = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
154 
155     if (iface ==NULL || node == NULL || nodeName == NULL) {
156         HDF_LOGE("%{public}s, failed for node %{public}s, variable NULL!", __func__, nodeName);
157         return HDF_FAILURE;
158     }
159 
160     codecGroupNode = iface->GetChildNode(node, nodeName);
161     if (codecGroupNode == NULL) {
162         HDF_LOGE("%{public}s, failed to get child node: %{public}s!", __func__, nodeName);
163         return HDF_FAILURE;
164     }
165     DEV_RES_NODE_FOR_EACH_CHILD_NODE(codecGroupNode, childNode) {
166         cap = &(capsGroup->capablitis[index++]);
167         GetOneCapability(iface, childNode, cap);
168     }
169 
170     return HDF_SUCCESS;
171 }
172 
LoadCodecCapabilityFromHcs(const struct DeviceResourceNode * node,CodecCapablites * caps)173 int32_t LoadCodecCapabilityFromHcs(const struct DeviceResourceNode *node, CodecCapablites *caps)
174 {
175     CodecCapablityGroup *codecCapGroup;
176     int32_t index;
177     int32_t codecNum = 0;
178 
179     if (node == NULL) {
180         HDF_LOGE("%{public}s, load capability failed, node is null!", __func__);
181         return HDF_FAILURE;
182     }
183 
184     char *codecGroupsNodeName[] = {
185         NODE_VIDEO_HARDWARE_ENCODERS, NODE_VIDEO_HARDWARE_DECODERS,
186         NODE_VIDEO_SOFTWARE_ENCODERS, NODE_VIDEO_SOFTWARE_DECODERS,
187         NODE_AUDIO_HARDWARE_ENCODERS, NODE_AUDIO_HARDWARE_DECODERS,
188         NODE_AUDIO_SOFTWARE_ENCODERS, NODE_AUDIO_SOFTWARE_DECODERS
189     };
190     CodecCapablityGroup *codecCapGroups[] = {
191         &(caps->videoHwEncoderGroup), &(caps->videoHwDecoderGroup),
192         &(caps->videoSwEncoderGroup), &(caps->videoSwDecoderGroup),
193         &(caps->audioHwEncoderGroup), &(caps->audioHwDecoderGroup),
194         &(caps->audioSwEncoderGroup), &(caps->audioSwDecoderGroup)
195     };
196 
197     for (index = 0; index < CODEC_CAPABLITY_GROUP_NUM; index++) {
198         if (GetGroupCapabilitiesNumber(node, codecGroupsNodeName[index], &codecNum) == HDF_SUCCESS) {
199             codecCapGroup = codecCapGroups[index];
200             codecCapGroup->num = codecNum;
201             if (codecNum > 0) {
202                 codecCapGroup->capablitis
203                     = (CodecCapbility *)OsalMemAlloc(sizeof(CodecCapbility) * codecNum);
204             } else {
205                 codecCapGroup->capablitis = NULL;
206             }
207             if (codecNum > 0 && codecCapGroup->capablitis == NULL) {
208                 codecCapGroup->num = 0;
209                 HDF_LOGE("%{public}s, MemAlloc for capability group failed!", __func__);
210                 return HDF_FAILURE;
211             }
212         }
213     }
214 
215     for (index = 0; index < CODEC_CAPABLITY_GROUP_NUM; index++) {
216         if (GetGroupCapabilities(node, codecGroupsNodeName[index], codecCapGroups[index]) != HDF_SUCCESS) {
217             return HDF_FAILURE;
218         }
219     }
220 
221     caps->inited = true;
222     return HDF_SUCCESS;
223 }
224 
ClearCapabilityGroup(CodecCapablites * caps)225 int32_t ClearCapabilityGroup(CodecCapablites *caps)
226 {
227     int32_t index;
228     CodecCapablityGroup *codecCapGroup;
229     CodecCapablityGroup *codecCapGroups[] = {
230         &(caps->videoHwEncoderGroup), &(caps->videoHwDecoderGroup),
231         &(caps->videoSwEncoderGroup), &(caps->videoSwDecoderGroup),
232         &(caps->audioHwEncoderGroup), &(caps->audioHwDecoderGroup),
233         &(caps->audioSwEncoderGroup), &(caps->audioSwDecoderGroup)
234     };
235     for (index = 0; index < CODEC_CAPABLITY_GROUP_NUM; index++) {
236         codecCapGroup = codecCapGroups[index];
237         if (codecCapGroup->capablitis != NULL) {
238             OsalMemFree(codecCapGroup->capablitis);
239             codecCapGroup->num = 0;
240             codecCapGroup->capablitis = NULL;
241         }
242     }
243     caps->inited = false;
244 
245     return HDF_SUCCESS;
246 }