1 /*
2 * Copyright (c) 2020-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
16 #include "decoder.h"
17 #include <stdio.h>
18 #include "media_log.h"
19 #include "securec.h"
20 extern "C"
21 {
22 #include "codec_interface.h"
23 }
24
25 namespace OHOS {
26 namespace Media {
27 const int PARAM_MAX_NUM = 30;
Decoder()28 Decoder::Decoder()
29 : codecHandle_(nullptr)
30 {
31 }
32
~Decoder()33 Decoder::~Decoder()
34 {
35 }
36
GetCapbilityByMime(AvCodecMime mime,CodecType type,uint32_t flags,CodecCapbility & cap)37 int32_t Decoder::GetCapbilityByMime(AvCodecMime mime, CodecType type, uint32_t flags, CodecCapbility &cap)
38 {
39 int32_t ret = CodecGetCapbility(mime, type, flags, &cap);
40 if (ret != CODEC_SUCCESS) {
41 return CODEC_FAILURE;
42 }
43 return CODEC_SUCCESS;
44 }
45
ConvertAdecAttributToParams(AvAttribute & attr,Param * param,int maxLen,int & actualLen)46 static bool ConvertAdecAttributToParams(AvAttribute &attr, Param *param,
47 int maxLen, int &actualLen)
48 {
49 int32_t index = 0;
50 param[index].key = KEY_MIMETYPE;
51 param[index].val = (void *)&(attr.adecAttr.mime);
52 param[index].size = sizeof(attr.adecAttr.mime);
53 index++;
54 param[index].key = KEY_BUFFERSIZE;
55 param[index].val = (void *)&(attr.adecAttr.bufSize);
56 param[index].size = sizeof(attr.adecAttr.bufSize);
57 index++;
58 param[index].key = KEY_CODEC_TYPE;
59 param[index].val = (void *)&(attr.type);
60 param[index].size = sizeof(attr.type);
61 index++;
62 if (attr.adecAttr.mime == MEDIA_MIMETYPE_AUDIO_PCM) {
63 param[index].key = KEY_CHANNEL_COUNT;
64 param[index].val = (void *)&attr.adecAttr.channelCnt;
65 param[index].size = sizeof(uint32_t);
66 index++;
67 }
68 actualLen = index;
69 if (actualLen > maxLen) {
70 MEDIA_ERR_LOG("anylsis adec param too much");
71 return false;
72 }
73 return true;
74 }
75
ConvertVdecAttributToParams(AvAttribute & attr,Param * param,int maxLen,int & actualLen)76 static bool ConvertVdecAttributToParams(AvAttribute &attr, Param *param,
77 int maxLen, int &actualLen)
78 {
79 int index = 0;
80 param[index].key = KEY_MIMETYPE;
81 param[index].val = (void*)&(attr.vdecAttr.mime);
82 param[index].size = sizeof(attr.vdecAttr.mime);
83 index++;
84 param[index].key = KEY_WIDTH;
85 param[index].val = (void*)&(attr.vdecAttr.maxWidth);
86 param[index].size = sizeof(attr.vdecAttr.maxWidth);
87 index++;
88 param[index].key = KEY_HEIGHT;
89 param[index].val = (void*)&(attr.vdecAttr.maxHeight);
90 param[index].size = sizeof(attr.vdecAttr.maxHeight);
91 index++;
92 param[index].key = KEY_BUFFERSIZE;
93 param[index].val = (void*)&(attr.vdecAttr.bufSize);
94 param[index].size = sizeof(attr.vdecAttr.bufSize);
95 index++;
96 param[index].key = KEY_CODEC_TYPE;
97 param[index].val = (void*)&(attr.type);
98 param[index].size = sizeof(attr.type);
99 index++;
100 actualLen = index;
101 if (actualLen > maxLen) {
102 MEDIA_ERR_LOG("anylsis vdec param too much");
103 return false;
104 }
105 return true;
106 }
107
ConvertAttributeToParams(AvAttribute & attr,Param * param,int maxLen,int & actualLen)108 static bool ConvertAttributeToParams(AvAttribute &attr, Param *param,
109 int maxLen, int &actualLen)
110 {
111 if (param == nullptr) {
112 MEDIA_ERR_LOG("param NULL");
113 return false;
114 }
115
116 bool ret = false;
117 switch (attr.type) {
118 case AUDIO_DECODER: {
119 ret = ConvertAdecAttributToParams(attr, param, maxLen, actualLen);
120 break;
121 }
122 case VIDEO_DECODER: {
123 ret = ConvertVdecAttributToParams(attr, param, maxLen, actualLen);
124 break;
125 }
126 default: {
127 MEDIA_ERR_LOG("not support this type:%d", attr.type);
128 ret = false;
129 }
130 }
131 return ret;
132 }
133
CreateHandle(const std::string & name,AvAttribute & attr)134 int32_t Decoder::CreateHandle(const std::string &name, AvAttribute &attr)
135 {
136 int actualLen = 0;
137 Param param[PARAM_MAX_NUM];
138 memset_s(param, PARAM_MAX_NUM * sizeof(Param), 0x00, PARAM_MAX_NUM * sizeof(Param));
139 if (ConvertAttributeToParams(attr, param, PARAM_MAX_NUM, actualLen) == false) {
140 MEDIA_ERR_LOG("convert fail");
141 }
142
143 int32_t ret = CodecCreate(name.c_str(), param, actualLen, &codecHandle_);
144 if (ret != CODEC_SUCCESS) {
145 return CODEC_FAILURE;
146 }
147 return CODEC_SUCCESS;
148 }
149
DestroyHandle()150 int32_t Decoder::DestroyHandle()
151 {
152 int32_t ret = CodecDestroy(codecHandle_);
153 codecHandle_ = nullptr;
154 if (ret != CODEC_SUCCESS) {
155 return CODEC_FAILURE;
156 }
157 return CODEC_SUCCESS;
158 }
159
SetPortBufferMode(DirectionType type,BufferMode mode)160 int32_t Decoder::SetPortBufferMode(DirectionType type, BufferMode mode)
161 {
162 int32_t ret = CodecSetPortMode(codecHandle_, type, mode);
163 if (ret != CODEC_SUCCESS) {
164 return CODEC_FAILURE;
165 }
166 return CODEC_SUCCESS;
167 }
168
SetMetadata(const Param * params,int paramCnt)169 int32_t Decoder::SetMetadata(const Param *params, int paramCnt)
170 {
171 int32_t ret = CodecSetParameter(codecHandle_, params, paramCnt);
172 if (ret != CODEC_SUCCESS) {
173 return CODEC_FAILURE;
174 }
175 return CODEC_SUCCESS;
176 }
177
GetMetadata(Param * params,int paramCnt)178 int32_t Decoder::GetMetadata(Param *params, int paramCnt)
179 {
180 int32_t ret = CodecGetParameter(codecHandle_, params, paramCnt);
181 if (ret != CODEC_SUCCESS) {
182 return CODEC_FAILURE;
183 }
184 return CODEC_SUCCESS;
185 }
186
StartDec()187 int32_t Decoder::StartDec()
188 {
189 int32_t ret = CodecStart(codecHandle_);
190 if (ret != CODEC_SUCCESS) {
191 return CODEC_FAILURE;
192 }
193 return CODEC_SUCCESS;
194 }
195
StopDec()196 int32_t Decoder::StopDec()
197 {
198 int32_t ret = CodecStop(codecHandle_);
199 if (ret != CODEC_SUCCESS) {
200 return CODEC_FAILURE;
201 }
202 return CODEC_SUCCESS;
203 }
204
FlushDec()205 int32_t Decoder::FlushDec()
206 {
207 int32_t ret = CodecFlush(codecHandle_, ALL_TYPE);
208 if (ret != CODEC_SUCCESS) {
209 return CODEC_FAILURE;
210 }
211 return CODEC_SUCCESS;
212 }
213
QueueInputBuffer(InputInfo & inputData,uint32_t timeoutMs)214 int32_t Decoder::QueueInputBuffer(InputInfo &inputData, uint32_t timeoutMs)
215 {
216 int32_t ret = CodecQueueInput(codecHandle_, &inputData, timeoutMs);
217 return ret;
218 }
219
DequeInputBuffer(InputInfo & inputData,uint32_t timeoutMs)220 int32_t Decoder::DequeInputBuffer(InputInfo &inputData, uint32_t timeoutMs)
221 {
222 int32_t ret = CodecDequeInput(codecHandle_, timeoutMs, &inputData);
223 if (ret != CODEC_SUCCESS) {
224 return CODEC_FAILURE;
225 }
226 return CODEC_SUCCESS;
227 }
228
QueueOutputBuffer(OutputInfo & outInfo,uint32_t timeoutMs)229 int32_t Decoder::QueueOutputBuffer(OutputInfo &outInfo, uint32_t timeoutMs)
230 {
231 int32_t ret = CodecQueueOutput(codecHandle_, &outInfo, timeoutMs, -1);
232 if (ret != CODEC_SUCCESS) {
233 return CODEC_FAILURE;
234 }
235 return CODEC_SUCCESS;
236 }
237
DequeueOutputBuffer(OutputInfo & outInfo,uint32_t timeoutMs)238 int32_t Decoder::DequeueOutputBuffer(OutputInfo &outInfo, uint32_t timeoutMs)
239 {
240 int32_t ret = CodecDequeueOutput(codecHandle_, timeoutMs, nullptr, &outInfo);
241 return ret;
242 }
243
SetCallback(CodecCallback & cb,UINTPTR instance)244 int32_t Decoder::SetCallback(CodecCallback &cb, UINTPTR instance)
245 {
246 int32_t ret = CodecSetCallback(codecHandle_, &cb, instance);
247 return ret;
248 }
249 }
250 }
251