• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 *)&param->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 
CodecSerPackBufferInfo(struct HdfSBuf * reply,const CodecBufferInfo * buffer)231 static int32_t CodecSerPackBufferInfo(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 (!HdfSbufWriteUint32(reply, (uint32_t)buffer->type)) {
238         HDF_LOGE("%{public}s: Write tempType failed!", __func__);
239         return HDF_FAILURE;
240     }
241     if (buffer->type == BUFFER_TYPE_VIRTUAL) {
242         if (!HdfSbufWriteBuffer(reply, (void *)buffer->buf, buffer->length)) {
243             HDF_LOGE("%{public}s: Write addr failed!", __func__);
244             return HDF_FAILURE;
245         }
246     } else if (buffer->type == BUFFER_TYPE_FD) {
247         if (!HdfSbufWriteFileDescriptor(reply, (int32_t)buffer->buf)) {
248             HDF_LOGE("%{public}s: Write fd failed!", __func__);
249             return HDF_FAILURE;
250         }
251     } else if (buffer->type == BUFFER_TYPE_HANDLE) {
252         if (!PackBufferHandle(reply, (BufferHandle *)buffer->buf)) {
253             return HDF_FAILURE;
254         }
255     }
256     if (!HdfSbufWriteUint32(reply, buffer->offset)) {
257         HDF_LOGE("%{public}s: Write offset failed!", __func__);
258         return HDF_FAILURE;
259     }
260     if (!HdfSbufWriteUint32(reply, buffer->length)) {
261         HDF_LOGE("%{public}s: Write length failed!", __func__);
262         return HDF_FAILURE;
263     }
264     if (!HdfSbufWriteUint32(reply, buffer->capacity)) {
265         HDF_LOGE("%{public}s: Write size failed!", __func__);
266         return HDF_FAILURE;
267     }
268     return HDF_SUCCESS;
269 }
270 
CodecSerParseBufferInfo(struct HdfSBuf * data,CodecBufferInfo * buffer)271 static int32_t CodecSerParseBufferInfo(struct HdfSBuf *data, CodecBufferInfo *buffer)
272 {
273     uint32_t readLen = 0;
274     if (data == NULL || buffer == NULL) {
275         HDF_LOGE("%{public}s: params null!", __func__);
276         return HDF_FAILURE;
277     }
278     if (!HdfSbufReadUint32(data, (uint32_t *)&buffer->type)) {
279         HDF_LOGE("%{public}s: read type failed!", __func__);
280         return HDF_FAILURE;
281     }
282     if (buffer->type == BUFFER_TYPE_VIRTUAL) {
283         void *buf = (void *)buffer->buf;
284         if (!HdfSbufReadBuffer(data, (const void **)&buf, &readLen)) {
285             HDF_LOGE("%{public}s: read addr failed!", __func__);
286             return HDF_FAILURE;
287         }
288     } else if (buffer->type == BUFFER_TYPE_FD) {
289         buffer->buf = (intptr_t)HdfSbufReadFileDescriptor(data);
290         if (buffer->buf < 0) {
291             HDF_LOGE("%{public}s: read fd failed!", __func__);
292             return HDF_FAILURE;
293         }
294     } else if (buffer->type == BUFFER_TYPE_HANDLE) {
295         if (!ParseBufferHandle(data, (BufferHandle **)&buffer->buf)) {
296             return HDF_FAILURE;
297         }
298     }
299     if (!HdfSbufReadUint32(data, &buffer->offset)) {
300         HDF_LOGE("%{public}s: read offset failed!", __func__);
301         return HDF_FAILURE;
302     }
303     if (!HdfSbufReadUint32(data, &buffer->length)) {
304         HDF_LOGE("%{public}s: read length failed!", __func__);
305         return HDF_FAILURE;
306     }
307     if (!HdfSbufReadUint32(data, &buffer->capacity)) {
308         HDF_LOGE("%{public}s: read size failed!", __func__);
309         return HDF_FAILURE;
310     }
311     return HDF_SUCCESS;
312 }
313 
CodecSerParseCodecBuffer(struct HdfSBuf * data,CodecBuffer * codecBuffer)314 int32_t CodecSerParseCodecBuffer(struct HdfSBuf *data, CodecBuffer *codecBuffer)
315 {
316     if (data == NULL || codecBuffer == NULL) {
317         HDF_LOGE("%{public}s: params null!", __func__);
318         return HDF_FAILURE;
319     }
320     if (!HdfSbufReadUint32(data, &codecBuffer->bufferId)) {
321         HDF_LOGE("%{public}s: read sequence failed!", __func__);
322         return HDF_FAILURE;
323     }
324     if (!HdfSbufReadInt64(data, &codecBuffer->timeStamp)) {
325         HDF_LOGE("%{public}s: read timeStamp failed!", __func__);
326         return HDF_FAILURE;
327     }
328     if (!HdfSbufReadUint32(data, &codecBuffer->flag)) {
329         HDF_LOGE("%{public}s: read flag failed!", __func__);
330         return HDF_FAILURE;
331     }
332     for (uint32_t i = 0; i < codecBuffer->bufferCnt; i++) {
333         if (CodecSerParseBufferInfo(data, &codecBuffer->buffer[i])) {
334             HDF_LOGE("%{public}s: read buffers failed!", __func__);
335             return HDF_FAILURE;
336         }
337     }
338     return HDF_SUCCESS;
339 }
340 
CodecSerPackCodecBuffer(struct HdfSBuf * reply,const CodecBuffer * codecBuffer)341 int32_t CodecSerPackCodecBuffer(struct HdfSBuf *reply, const CodecBuffer *codecBuffer)
342 {
343     if (reply == NULL || codecBuffer == NULL) {
344         HDF_LOGE("%{public}s: params null!", __func__);
345         return HDF_FAILURE;
346     }
347     if (!HdfSbufWriteUint32(reply, codecBuffer->bufferId)) {
348         HDF_LOGE("%{public}s: write sequence failed!", __func__);
349         return HDF_FAILURE;
350     }
351     if (!HdfSbufWriteInt64(reply, codecBuffer->timeStamp)) {
352         HDF_LOGE("%{public}s: write timeStamp failed!", __func__);
353         return HDF_FAILURE;
354     }
355     if (!HdfSbufWriteUint32(reply, codecBuffer->flag)) {
356         HDF_LOGE("%{public}s: write flag failed!", __func__);
357         return HDF_FAILURE;
358     }
359     for (uint32_t i = 0; i < codecBuffer->bufferCnt; i++) {
360         if (CodecSerPackBufferInfo(reply, &codecBuffer->buffer[i])) {
361             HDF_LOGE("%{public}s: write buffers failed!", __func__);
362             return HDF_FAILURE;
363         }
364     }
365     return HDF_SUCCESS;
366 }
367 
CodecSerParseFenceFd(struct HdfSBuf * data,int32_t * fenceFd)368 int32_t CodecSerParseFenceFd(struct HdfSBuf *data, int32_t *fenceFd)
369 {
370     uint8_t validFd = 0;
371     if (!HdfSbufReadUint8(data, &validFd)) {
372         HDF_LOGE("%{public}s: read validFd failed!", __func__);
373         return HDF_FAILURE;
374     }
375     if (validFd != 0) {
376         *fenceFd = HdfSbufReadFileDescriptor(data);
377     } else {
378         *fenceFd = -1;
379     }
380     return HDF_SUCCESS;
381 }
382 
CodecSerPackFenceFd(struct HdfSBuf * reply,int32_t fenceFd)383 int32_t CodecSerPackFenceFd(struct HdfSBuf *reply, int32_t fenceFd)
384 {
385     uint8_t validFd = fenceFd >= 0;
386     if (!HdfSbufWriteUint8(reply, validFd)) {
387         HDF_LOGE("%{public}s: write validFd flag failed!", __func__);
388         return HDF_ERR_INVALID_PARAM;
389     }
390     if (validFd != 0 && !HdfSbufWriteFileDescriptor(reply, fenceFd)) {
391         HDF_LOGE("%{public}s: write fenceFd failed!", __func__);
392         return HDF_ERR_INVALID_PARAM;
393     }
394     return HDF_SUCCESS;
395 }
396