1 /*
2 * Copyright (c) 2021-2023 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 #include "stub_msgproc.h"
16 #include <hdf_log.h>
17 #include <osal_mem.h>
18 #include "common_msgproc.h"
19
20 #define HDF_LOG_TAG codec_hdi_stub
21
CodecSerPackAlignment(struct HdfSBuf * reply,Alignment * alignment)22 int32_t CodecSerPackAlignment(struct HdfSBuf *reply, Alignment *alignment)
23 {
24 if (reply == NULL || alignment == NULL) {
25 HDF_LOGE("%{public}s: params null!", __func__);
26 return HDF_ERR_INVALID_PARAM;
27 }
28 if (!HdfSbufWriteInt32(reply, alignment->widthAlignment)) {
29 HDF_LOGE("%{public}s: Write widthAlignment failed!", __func__);
30 return HDF_FAILURE;
31 }
32 if (!HdfSbufWriteInt32(reply, alignment->heightAlignment)) {
33 HDF_LOGE("%{public}s: Write heightAlignment failed!", __func__);
34 return HDF_FAILURE;
35 }
36 return HDF_SUCCESS;
37 }
38
CodecSerPackRect(struct HdfSBuf * reply,Rect * rectangle)39 int32_t CodecSerPackRect(struct HdfSBuf *reply, Rect *rectangle)
40 {
41 if (reply == NULL || rectangle == NULL) {
42 HDF_LOGE("%{public}s: params null!", __func__);
43 return HDF_ERR_INVALID_PARAM;
44 }
45 if (!HdfSbufWriteInt32(reply, rectangle->width)) {
46 HDF_LOGE("%{public}s: Write width failed!", __func__);
47 return HDF_FAILURE;
48 }
49 if (!HdfSbufWriteInt32(reply, rectangle->height)) {
50 HDF_LOGE("%{public}s: Write height failed!", __func__);
51 return HDF_FAILURE;
52 }
53 return HDF_SUCCESS;
54 }
55
CodecCapabilityBaseMarshalling(struct HdfSBuf * reply,CodecCapability * cap)56 static bool CodecCapabilityBaseMarshalling(struct HdfSBuf *reply, CodecCapability *cap)
57 {
58 if (!HdfSbufWriteUint32(reply, (uint32_t)cap->mime)) {
59 HDF_LOGE("%{public}s: write cap->mime failed!", __func__);
60 return false;
61 }
62 if (!HdfSbufWriteUint32(reply, (uint32_t)cap->type)) {
63 HDF_LOGE("%{public}s: write cap->type failed!", __func__);
64 return false;
65 }
66 for (uint32_t i = 0; i < NAME_LENGTH; i++) {
67 if (!HdfSbufWriteUint8(reply, (uint8_t)(cap->name)[i])) {
68 HDF_LOGE("%{public}s: write (cap->name)[i] failed!", __func__);
69 return false;
70 }
71 }
72 for (uint32_t i = 0; i < PROFILE_NUM; i++) {
73 if (!HdfSbufWriteInt32(reply, (cap->supportProfiles)[i])) {
74 HDF_LOGE("%{public}s: write (cap->supportProfiles)[i] failed!", __func__);
75 return false;
76 }
77 }
78 if (!HdfSbufWriteInt8(reply, cap->isSoftwareCodec ? 1 : 0)) {
79 HDF_LOGE("%{public}s: write cap->isSoftwareCodec failed!", __func__);
80 return false;
81 }
82 if (!HdfSbufWriteInt32(reply, cap->processModeMask)) {
83 HDF_LOGE("%{public}s: write cap->processModeMask failed!", __func__);
84 return false;
85 }
86 if (!HdfSbufWriteUint32(reply, cap->capsMask)) {
87 HDF_LOGE("%{public}s: write cap->capsMask failed!", __func__);
88 return false;
89 }
90 if (!HdfSbufWriteUint32(reply, cap->allocateMask)) {
91 HDF_LOGE("%{public}s: write cap->allocateMask failed!", __func__);
92 return false;
93 }
94 return true;
95 }
96
CodecCapabilityRangeValueMarshalling(struct HdfSBuf * reply,const RangeValue * dataBlock)97 static bool CodecCapabilityRangeValueMarshalling(struct HdfSBuf *reply, const RangeValue *dataBlock)
98 {
99 if (!HdfSbufWriteInt32(reply, dataBlock->min)) {
100 HDF_LOGE("%{public}s: write dataBlock->min failed!", __func__);
101 return false;
102 }
103
104 if (!HdfSbufWriteInt32(reply, dataBlock->max)) {
105 HDF_LOGE("%{public}s: write dataBlock->max failed!", __func__);
106 return false;
107 }
108
109 return true;
110 }
111
CodecCapabilityPortMarshalling(struct HdfSBuf * reply,CodecCapability * cap)112 static bool CodecCapabilityPortMarshalling(struct HdfSBuf *reply, CodecCapability *cap)
113 {
114 if (cap->type < AUDIO_DECODER) {
115 if (!HdfSbufWriteUnpadBuffer(reply, (const uint8_t *)&cap->port, sizeof(VideoPortCap))) {
116 HDF_LOGE("%{public}s: write video failed!", __func__);
117 return false;
118 }
119 } else if (cap->type < INVALID_TYPE) {
120 if (!HdfSbufWriteUnpadBuffer(reply, (const uint8_t *)&cap->port, sizeof(AudioPortCap))) {
121 HDF_LOGE("%{public}s: write audio failed!", __func__);
122 return false;
123 }
124 } else {
125 if (!HdfSbufWriteUnpadBuffer(reply, (const uint8_t *)&cap->port, sizeof(cap->port))) {
126 HDF_LOGE("%{public}s: write port failed!", __func__);
127 return false;
128 }
129 }
130 return true;
131 }
132
CodecSerPackCapability(struct HdfSBuf * reply,CodecCapability * cap)133 int32_t CodecSerPackCapability(struct HdfSBuf *reply, CodecCapability *cap)
134 {
135 if (reply == NULL || cap == NULL) {
136 HDF_LOGE("%{public}s: params null!", __func__);
137 return HDF_ERR_INVALID_PARAM;
138 }
139 if (!CodecCapabilityBaseMarshalling(reply, cap)) {
140 return HDF_FAILURE;
141 }
142 if (!CodecCapabilityRangeValueMarshalling(reply, &cap->inputBufferNum)) {
143 HDF_LOGE("%{public}s: write cap->inputBufferNum failed!", __func__);
144 return HDF_FAILURE;
145 }
146 if (!CodecCapabilityRangeValueMarshalling(reply, &cap->outputBufferNum)) {
147 HDF_LOGE("%{public}s: write cap->outputBufferNum failed!", __func__);
148 return HDF_FAILURE;
149 }
150 if (!CodecCapabilityRangeValueMarshalling(reply, &cap->bitRate)) {
151 HDF_LOGE("%{public}s: write cap->bitRate failed!", __func__);
152 return HDF_FAILURE;
153 }
154 if (!HdfSbufWriteInt32(reply, cap->inputBufferSize)) {
155 HDF_LOGE("%{public}s: write cap->inputBufferSize failed!", __func__);
156 return false;
157 }
158 if (!HdfSbufWriteInt32(reply, cap->outputBufferSize)) {
159 HDF_LOGE("%{public}s: write cap->outputBufferSize failed!", __func__);
160 return false;
161 }
162 if (!CodecCapabilityPortMarshalling(reply, cap)) {
163 HDF_LOGE("%{public}s: write cap->port failed!", __func__);
164 return HDF_FAILURE;
165 }
166 return HDF_SUCCESS;
167 }
168
CodecSerParseParam(struct HdfSBuf * data,Param * param)169 int32_t CodecSerParseParam(struct HdfSBuf *data, Param *param)
170 {
171 if (data == NULL || param == NULL) {
172 HDF_LOGE("%{public}s: params null!", __func__);
173 return HDF_ERR_INVALID_PARAM;
174 }
175 if (!HdfSbufReadInt32(data, (int32_t *)¶m->key)) {
176 HDF_LOGE("%{public}s: read param->key failed!", __func__);
177 return HDF_FAILURE;
178 }
179
180 int8_t *valCp = NULL;
181 int32_t valCpLen = 0;
182 if (!HdfSbufReadInt32(data, &valCpLen)) {
183 HDF_LOGE("%{public}s: read size failed!", __func__);
184 return HDF_FAILURE;
185 }
186 if (valCpLen > 0) {
187 valCp = (int8_t *)OsalMemCalloc(sizeof(int8_t) * valCpLen);
188 if (valCp == NULL) {
189 return HDF_FAILURE;
190 }
191 for (int32_t i = 0; i < valCpLen; i++) {
192 if (!HdfSbufReadInt8(data, &valCp[i])) {
193 HDF_LOGE("%{public}s: read valCp[i] failed!", __func__);
194 OsalMemFree(valCp);
195 return HDF_FAILURE;
196 }
197 }
198 }
199 param->val = (void *)valCp;
200 param->size = valCpLen;
201
202 return HDF_SUCCESS;
203 }
204
CodecSerPackParam(struct HdfSBuf * reply,Param * param)205 int32_t CodecSerPackParam(struct HdfSBuf *reply, Param *param)
206 {
207 if (reply == NULL || param == NULL) {
208 HDF_LOGE("%{public}s: params null!", __func__);
209 return HDF_ERR_INVALID_PARAM;
210 }
211
212 if (!HdfSbufWriteInt32(reply, (int32_t)param->key)) {
213 HDF_LOGE("%{public}s: write param->key failed!", __func__);
214 return false;
215 }
216
217 if (!HdfSbufWriteInt32(reply, param->size)) {
218 HDF_LOGE("%{public}s: write param->size failed!", __func__);
219 return false;
220 }
221 for (int32_t i = 0; i < param->size; i++) {
222 if (!HdfSbufWriteInt8(reply, ((int8_t *)(param->val))[i])) {
223 HDF_LOGE("%{public}s: write (param->val)[i] failed!", __func__);
224 return false;
225 }
226 }
227
228 return HDF_SUCCESS;
229 }
230
CodecSerPackBufOfBufferInfo(struct HdfSBuf * reply,const CodecBufferInfo * buffer)231 static int32_t CodecSerPackBufOfBufferInfo(struct HdfSBuf *reply, const CodecBufferInfo *buffer)
232 {
233 if (reply == NULL || buffer == NULL) {
234 HDF_LOGE("%{public}s: params null!", __func__);
235 return HDF_FAILURE;
236 }
237 if (buffer->type == BUFFER_TYPE_VIRTUAL) {
238 if (!HdfSbufWriteBuffer(reply, (void *)buffer->buf, buffer->length)) {
239 HDF_LOGE("%{public}s: Write virtual buffer failed!", __func__);
240 return HDF_FAILURE;
241 }
242 } else if (buffer->type == BUFFER_TYPE_FD) {
243 uint8_t validFd = buffer->buf >= 0 ? 1 : 0;
244 if (!HdfSbufWriteUint8(reply, validFd)) {
245 HDF_LOGE("%{public}s: write validFd failed!", __func__);
246 return HDF_FAILURE;
247 }
248 if (validFd && !HdfSbufWriteFileDescriptor(reply, (int32_t)buffer->buf)) {
249 HDF_LOGE("%{public}s: Write fd failed!", __func__);
250 return HDF_FAILURE;
251 }
252 } else if (buffer->type == BUFFER_TYPE_HANDLE) {
253 uint8_t validHandle = buffer->buf != 0 ? 1 : 0;
254 if (!HdfSbufWriteUint8(reply, validHandle)) {
255 HDF_LOGE("%{public}s: write validHandle failed!", __func__);
256 return HDF_FAILURE;
257 }
258 if (validHandle && !PackBufferHandle(reply, (BufferHandle *)buffer->buf)) {
259 return HDF_FAILURE;
260 }
261 } else {
262 HDF_LOGE("%{public}s: buffer->type incorrect!", __func__);
263 return HDF_FAILURE;
264 }
265
266 return HDF_SUCCESS;
267 }
268
CodecSerPackBufferInfo(struct HdfSBuf * reply,const CodecBufferInfo * buffer)269 static int32_t CodecSerPackBufferInfo(struct HdfSBuf *reply, const CodecBufferInfo *buffer)
270 {
271 if (reply == NULL || buffer == NULL) {
272 HDF_LOGE("%{public}s: params null!", __func__);
273 return HDF_FAILURE;
274 }
275 if (!HdfSbufWriteUint32(reply, (uint32_t)buffer->type)) {
276 HDF_LOGE("%{public}s: Write tempType failed!", __func__);
277 return HDF_FAILURE;
278 }
279 int32_t ret = CodecSerPackBufOfBufferInfo(reply, buffer);
280 if (ret != HDF_SUCCESS) {
281 return ret;
282 }
283 if (!HdfSbufWriteUint32(reply, buffer->offset)) {
284 HDF_LOGE("%{public}s: Write offset failed!", __func__);
285 return HDF_FAILURE;
286 }
287 if (!HdfSbufWriteUint32(reply, buffer->length)) {
288 HDF_LOGE("%{public}s: Write length failed!", __func__);
289 return HDF_FAILURE;
290 }
291 if (!HdfSbufWriteUint32(reply, buffer->capacity)) {
292 HDF_LOGE("%{public}s: Write capacity failed!", __func__);
293 return HDF_FAILURE;
294 }
295 return HDF_SUCCESS;
296 }
297
CodecSerParseBufOfBufferInfo(struct HdfSBuf * data,CodecBufferInfo * buffer)298 static int32_t CodecSerParseBufOfBufferInfo(struct HdfSBuf *data, CodecBufferInfo *buffer)
299 {
300 if (data == NULL || buffer == NULL) {
301 HDF_LOGE("%{public}s: params null!", __func__);
302 return HDF_FAILURE;
303 }
304 uint32_t readLen = 0;
305 if (buffer->type == BUFFER_TYPE_VIRTUAL) {
306 void *buf = (void *)buffer->buf;
307 if (!HdfSbufReadBuffer(data, (const void **)&buf, &readLen)) {
308 HDF_LOGE("%{public}s: read addr failed!", __func__);
309 return HDF_FAILURE;
310 }
311 } else if (buffer->type == BUFFER_TYPE_FD) {
312 uint8_t validFd = 0;
313 if (!HdfSbufReadUint8(data, &validFd)) {
314 HDF_LOGE("%{public}s: read validFd failed!", __func__);
315 return HDF_FAILURE;
316 }
317 if (validFd != 0) {
318 buffer->buf = (intptr_t)HdfSbufReadFileDescriptor(data);
319 if (buffer->buf < 0) {
320 HDF_LOGE("%{public}s: read fd failed!", __func__);
321 return HDF_FAILURE;
322 }
323 } else {
324 buffer->buf = (intptr_t)(-1);
325 }
326 } else if (buffer->type == BUFFER_TYPE_HANDLE) {
327 uint8_t validHandle = 0;
328 if (!HdfSbufReadUint8(data, &validHandle)) {
329 HDF_LOGE("%{public}s: read validHandle failed!", __func__);
330 return HDF_FAILURE;
331 }
332 if (validHandle != 0) {
333 if (!ParseBufferHandle(data, (BufferHandle **)&buffer->buf)) {
334 return HDF_FAILURE;
335 }
336 } else {
337 buffer->buf = (intptr_t)(0);
338 }
339 } else {
340 HDF_LOGE("%{public}s: buffer->type incorrect!", __func__);
341 return HDF_FAILURE;
342 }
343
344 return HDF_SUCCESS;
345 }
346
CodecSerParseBufferInfo(struct HdfSBuf * data,CodecBufferInfo * buffer)347 static int32_t CodecSerParseBufferInfo(struct HdfSBuf *data, CodecBufferInfo *buffer)
348 {
349 if (data == NULL || buffer == NULL) {
350 HDF_LOGE("%{public}s: params null!", __func__);
351 return HDF_FAILURE;
352 }
353 if (!HdfSbufReadUint32(data, (uint32_t *)&buffer->type)) {
354 HDF_LOGE("%{public}s: read type failed!", __func__);
355 return HDF_FAILURE;
356 }
357 int32_t ret = CodecSerParseBufOfBufferInfo(data, buffer);
358 if (ret != HDF_SUCCESS) {
359 return ret;
360 }
361 if (!HdfSbufReadUint32(data, &buffer->offset)) {
362 HDF_LOGE("%{public}s: read offset failed!", __func__);
363 return HDF_FAILURE;
364 }
365 if (!HdfSbufReadUint32(data, &buffer->length)) {
366 HDF_LOGE("%{public}s: read length failed!", __func__);
367 return HDF_FAILURE;
368 }
369 if (!HdfSbufReadUint32(data, &buffer->capacity)) {
370 HDF_LOGE("%{public}s: read capacity failed!", __func__);
371 return HDF_FAILURE;
372 }
373 return HDF_SUCCESS;
374 }
375
CodecSerParseCodecBuffer(struct HdfSBuf * data,CodecBuffer * codecBuffer)376 int32_t CodecSerParseCodecBuffer(struct HdfSBuf *data, CodecBuffer *codecBuffer)
377 {
378 if (data == NULL || codecBuffer == NULL) {
379 HDF_LOGE("%{public}s: params null!", __func__);
380 return HDF_FAILURE;
381 }
382 if (!HdfSbufReadUint32(data, &codecBuffer->bufferId)) {
383 HDF_LOGE("%{public}s: read sequence failed!", __func__);
384 return HDF_FAILURE;
385 }
386 if (!HdfSbufReadInt64(data, &codecBuffer->timeStamp)) {
387 HDF_LOGE("%{public}s: read timeStamp failed!", __func__);
388 return HDF_FAILURE;
389 }
390 if (!HdfSbufReadUint32(data, &codecBuffer->flag)) {
391 HDF_LOGE("%{public}s: read flag failed!", __func__);
392 return HDF_FAILURE;
393 }
394 for (uint32_t i = 0; i < codecBuffer->bufferCnt; i++) {
395 if (CodecSerParseBufferInfo(data, &codecBuffer->buffer[i])) {
396 HDF_LOGE("%{public}s: read buffers failed!", __func__);
397 return HDF_FAILURE;
398 }
399 }
400 return HDF_SUCCESS;
401 }
402
CodecSerPackCodecBuffer(struct HdfSBuf * reply,const CodecBuffer * codecBuffer)403 int32_t CodecSerPackCodecBuffer(struct HdfSBuf *reply, const CodecBuffer *codecBuffer)
404 {
405 if (reply == NULL || codecBuffer == NULL) {
406 HDF_LOGE("%{public}s: params null!", __func__);
407 return HDF_FAILURE;
408 }
409 if (!HdfSbufWriteUint32(reply, codecBuffer->bufferId)) {
410 HDF_LOGE("%{public}s: write sequence failed!", __func__);
411 return HDF_FAILURE;
412 }
413 if (!HdfSbufWriteInt64(reply, codecBuffer->timeStamp)) {
414 HDF_LOGE("%{public}s: write timeStamp failed!", __func__);
415 return HDF_FAILURE;
416 }
417 if (!HdfSbufWriteUint32(reply, codecBuffer->flag)) {
418 HDF_LOGE("%{public}s: write flag failed!", __func__);
419 return HDF_FAILURE;
420 }
421 for (uint32_t i = 0; i < codecBuffer->bufferCnt; i++) {
422 if (CodecSerPackBufferInfo(reply, &codecBuffer->buffer[i])) {
423 HDF_LOGE("%{public}s: write buffers failed!", __func__);
424 return HDF_FAILURE;
425 }
426 }
427 return HDF_SUCCESS;
428 }
429
CodecSerParseFenceFd(struct HdfSBuf * data,int32_t * fenceFd)430 int32_t CodecSerParseFenceFd(struct HdfSBuf *data, int32_t *fenceFd)
431 {
432 uint8_t validFd = 0;
433 if (!HdfSbufReadUint8(data, &validFd)) {
434 HDF_LOGE("%{public}s: read validFd failed!", __func__);
435 return HDF_FAILURE;
436 }
437 if (validFd != 0) {
438 *fenceFd = HdfSbufReadFileDescriptor(data);
439 } else {
440 *fenceFd = -1;
441 }
442 return HDF_SUCCESS;
443 }
444
CodecSerPackFenceFd(struct HdfSBuf * reply,int32_t fenceFd)445 int32_t CodecSerPackFenceFd(struct HdfSBuf *reply, int32_t fenceFd)
446 {
447 uint8_t validFd = fenceFd >= 0;
448 if (!HdfSbufWriteUint8(reply, validFd)) {
449 HDF_LOGE("%{public}s: write validFd flag failed!", __func__);
450 return HDF_ERR_INVALID_PARAM;
451 }
452 if (validFd != 0 && !HdfSbufWriteFileDescriptor(reply, fenceFd)) {
453 HDF_LOGE("%{public}s: write fenceFd failed!", __func__);
454 return HDF_ERR_INVALID_PARAM;
455 }
456 return HDF_SUCCESS;
457 }
458