1 /*
2 * Copyright (c) 2022-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
16 #include "codec_hdi_adapter_decode.h"
17 #include <dlfcn.h>
18 #include <osal_time.h>
19 #include <sys/mman.h>
20 #include <unistd.h>
21 #include <chrono>
22 #include <sys/stat.h>
23 #include "codec_type.h"
24 #include "codec_omx_ext.h"
25
26 using namespace std;
27 using namespace OHOS;
28 namespace {
29 constexpr int32_t FD_SIZE = sizeof(int);
30 constexpr int32_t FRAME = (30 << 16);
31 constexpr const char *DECODER_AVC = "rk.video_decoder.avc";
32 constexpr const char *DECODER_HEVC = "rk.video_decoder.hevc";
33 constexpr int32_t START_CODE_OFFSET_ONE = -1;
34 constexpr int32_t START_CODE_OFFSET_SEC = -2;
35 constexpr int32_t START_CODE_OFFSET_THIRD = -3;
36 constexpr int32_t START_CODE_SIZE_FRAME = 4;
37 constexpr int32_t START_CODE_SIZE_SLICE = 3;
38 constexpr int32_t START_CODE = 1;
39 constexpr int32_t USLEEP_TIME = 10000;
40 constexpr int32_t FRAME_SIZE_OPERATOR = 2;
41 constexpr int32_t FRAME_SIZE_MULTI = 3;
42 }
43
44 #define HDF_LOG_TAG codec_omx_hdi_dec
45
46 #define AV_COLOR_FORMAT OMX_COLOR_FormatYUV420SemiPlanar
47
48 static CodecHdiAdapterDecode *g_core = nullptr;
CodecHdiAdapterDecode()49 CodecHdiAdapterDecode::CodecHdiAdapterDecode() : fpIn_(nullptr), fpOut_(nullptr)
50 {
51 client_ = nullptr;
52 callback_ = nullptr;
53 omxMgr_ = nullptr;
54 exit_ = false;
55 width_ = 0;
56 height_ = 0;
57 codecMime_ = codecMime::AVC;
58 count_ = 0;
59 useBufferHandle_ = false;
60 componentId_ = 0;
61 inputBufferSize_ = 0;
62 needSplit_ = 0;
63 srcFileSize_ = 0;
64 totalSrcSize_ = 0;
65 }
66
~CodecHdiAdapterDecode()67 CodecHdiAdapterDecode::~CodecHdiAdapterDecode()
68 {
69 if (fpOut_ != nullptr) {
70 fclose(fpOut_);
71 fpOut_ = nullptr;
72 }
73
74 if (fpIn_ != nullptr) {
75 fclose(fpIn_);
76 fpIn_ = nullptr;
77 }
78 }
79
WaitForStatusChanged()80 void CodecHdiAdapterDecode::WaitForStatusChanged()
81 {
82 unique_lock<mutex> autoLock(statusLock_);
83 statusCondition_.wait(autoLock);
84 }
85
OnStatusChanged()86 void CodecHdiAdapterDecode::OnStatusChanged()
87 {
88 statusCondition_.notify_one();
89 }
90
DumpOutputToFile(FILE * fp,uint8_t * addr)91 void CodecHdiAdapterDecode::DumpOutputToFile(FILE *fp, uint8_t *addr)
92 {
93 size_t bufferSize = (stride_ * height_ * FRAME_SIZE_MULTI) / FRAME_SIZE_OPERATOR;
94 size_t ret = fwrite(addr, 1, bufferSize, fp);
95 if (ret != bufferSize) {
96 HDF_LOGE("%{public}s: Dump frame failed, ret: %{public}zu", __func__, ret);
97 }
98 }
99
ReadOnePacket(FILE * fp,uint8_t * buf,uint32_t & filledCount)100 bool CodecHdiAdapterDecode::ReadOnePacket(FILE *fp, uint8_t *buf, uint32_t &filledCount)
101 {
102 filledCount = fread(buf, 1, inputBufferSize_, fp);
103 if (filledCount <= 0) {
104 return true;
105 }
106 return false;
107 }
108
ReadOneFrameFromFile(FILE * fp,uint8_t * buf,uint32_t & filledCount)109 bool CodecHdiAdapterDecode::ReadOneFrameFromFile(FILE *fp, uint8_t *buf, uint32_t &filledCount)
110 {
111 int32_t readSize = 0;
112 // read start code first
113 size_t t = fread(buf, 1, START_CODE_SIZE_FRAME, fp);
114 if (t < START_CODE_SIZE_FRAME) {
115 return true;
116 }
117 uint8_t *temp = buf;
118 temp += START_CODE_SIZE_FRAME;
119 while (!feof(fp)) {
120 t = fread(temp, 1, 1, fp);
121 if (t != 1) {
122 continue;
123 }
124
125 if (*temp == START_CODE) {
126 // check start code
127 if ((temp[START_CODE_OFFSET_ONE] == 0) && (temp[START_CODE_OFFSET_SEC] == 0) &&
128 (temp[START_CODE_OFFSET_THIRD] == 0)) {
129 fseek(fp, -START_CODE_SIZE_FRAME, SEEK_CUR);
130 temp -= (START_CODE_SIZE_FRAME - 1);
131 break;
132 } else if ((temp[START_CODE_OFFSET_ONE] == 0) && (temp[START_CODE_OFFSET_SEC] == 0)) {
133 fseek(fp, -START_CODE_SIZE_SLICE, SEEK_CUR);
134 temp -= (START_CODE_SIZE_SLICE - 1);
135 break;
136 }
137 }
138 temp++;
139 }
140 readSize = (temp - buf);
141 filledCount = readSize;
142 totalSrcSize_ += readSize;
143 return (totalSrcSize_ >= srcFileSize_);
144 }
145
Init(CommandOpt & opt)146 bool CodecHdiAdapterDecode::Init(CommandOpt &opt)
147 {
148 this->width_ = opt.width;
149 this->height_ = opt.height;
150 this->codecMime_ = opt.codec;
151 this->stride_ = AlignUp(opt.width);
152 this->useBufferHandle_ = opt.useBuffer;
153 HDF_LOGI("width[%{public}d], height[%{public}d],stride_[%{public}d],infile[%{public}s],outfile[%{public}s]",
154 width_, height_, stride_, opt.fileInput.c_str(), opt.fileOutput.c_str());
155
156 struct stat fileStat = {0};
157 stat(opt.fileInput.c_str(), &fileStat);
158 srcFileSize_ = fileStat.st_size;
159 fpIn_ = fopen(opt.fileInput.c_str(), "rb");
160 fpOut_ = fopen(opt.fileOutput.c_str(), "wb+");
161 if ((fpIn_ == nullptr) || (fpOut_ == nullptr)) {
162 HDF_LOGE("%{public}s failed to open file %{public}s or %{public}s", __func__, opt.fileInput.c_str(),
163 opt.fileOutput.c_str());
164 return false;
165 }
166
167 omxMgr_ = GetCodecComponentManager();
168
169 callback_ = CodecCallbackTypeGet(nullptr);
170 if ((omxMgr_ == nullptr) || (callback_ == nullptr)) {
171 HDF_LOGE("%{public}s omxMgr_ is null or callback_ is null", __func__);
172 return false;
173 }
174
175 callback_->EventHandler = &CodecHdiAdapterDecode::OnEvent;
176 callback_->EmptyBufferDone = &CodecHdiAdapterDecode::OnEmptyBufferDone;
177 callback_->FillBufferDone = &CodecHdiAdapterDecode::OnFillBufferDone;
178 int32_t ret = HDF_SUCCESS;
179 if (codecMime_ == codecMime::AVC) {
180 ret = omxMgr_->CreateComponent(
181 &client_, &componentId_, const_cast<char *>(DECODER_AVC), (int64_t)this, callback_);
182 } else {
183 ret = omxMgr_->CreateComponent(
184 &client_, &componentId_, const_cast<char *>(DECODER_HEVC), (int64_t)this, callback_);
185 }
186
187 if (ret != HDF_SUCCESS || client_ == nullptr) {
188 HDF_LOGE("%{public}s errNo[%{public}d] CreateComponent or client is null", __func__, ret);
189 return false;
190 }
191
192 return true;
193 }
194
ConfigMppPassthrough()195 int32_t CodecHdiAdapterDecode::ConfigMppPassthrough()
196 {
197 if (client_ == nullptr) {
198 HDF_LOGE("%{public}s error,client_ is null", __func__);
199 return HDF_FAILURE;
200 }
201 PassthroughParam param;
202 int32_t ret = memset_s(¶m, sizeof(PassthroughParam), 0, sizeof(PassthroughParam));
203 if (ret != EOK) {
204 HDF_LOGE("%{public}s: memset_s param err [%{public}d].", __func__, ret);
205 return ret;
206 }
207 CodecType ct = VIDEO_DECODER;
208 param.key = KEY_CODEC_TYPE;
209 param.val = &ct;
210 param.size = sizeof(ct);
211 ret = client_->SetParameter(client_, OMX_IndexParamPassthrough,
212 reinterpret_cast<int8_t *>(¶m), sizeof(param));
213 if (ret != HDF_SUCCESS) {
214 HDF_LOGE("%{public}s errNo[%{public}d] key is KEY_CODEC_TYPE", __func__, ret);
215 return ret;
216 }
217
218 return ret;
219 }
220
ConfigPortDefine()221 int32_t CodecHdiAdapterDecode::ConfigPortDefine()
222 {
223 if (client_ == nullptr) {
224 HDF_LOGE("%{public}s error,client_ is null", __func__);
225 return HDF_FAILURE;
226 }
227 // set width, height and color format on output port
228 OMX_PARAM_PORTDEFINITIONTYPE param;
229 InitParam(param);
230 param.nPortIndex = static_cast<uint32_t>(PortIndex::PORT_INDEX_OUTPUT);
231 auto ret = client_->GetParameter(client_, OMX_IndexParamPortDefinition, (int8_t *)¶m, sizeof(param));
232 if (ret != HDF_SUCCESS) {
233 HDF_LOGE("%{public}s errNo[%{public}d] to GetParameter OMX_IndexParamPortDefinition", __func__, ret);
234 return ret;
235 }
236 HDF_LOGI("get format: eCompressionFormat = %{public}d, eColorFormat=%{public}d",
237 param.format.video.eCompressionFormat, param.format.video.eColorFormat);
238 param.format.video.nFrameWidth = width_;
239 param.format.video.nFrameHeight = height_;
240 param.format.video.nStride = stride_;
241 param.format.video.nSliceHeight = height_;
242 param.format.video.eColorFormat = AV_COLOR_FORMAT; // YUV420SP
243 ret = client_->SetParameter(client_, OMX_IndexParamPortDefinition, (int8_t *)¶m, sizeof(param));
244 if (ret != HDF_SUCCESS) {
245 HDF_LOGE("%{public}s errNo[%{public}d] to SetParameter OMX_IndexParamPortDefinition", __func__, ret);
246 return ret;
247 }
248 return ret;
249 }
250
Configure()251 bool CodecHdiAdapterDecode::Configure()
252 {
253 if (client_ == nullptr) {
254 HDF_LOGE("%{public}s error,client_ is null", __func__);
255 return false;
256 }
257 if (ConfigMppPassthrough() != HDF_SUCCESS) {
258 HDF_LOGE("%{public}s error,ConfigMppPassthrough failed", __func__);
259 return false;
260 }
261 if (ConfigPortDefine() != HDF_SUCCESS) {
262 HDF_LOGE("%{public}s error,ConfigPortDefine failed", __func__);
263 return false;
264 }
265
266 OMX_VIDEO_PARAM_PORTFORMATTYPE param;
267 InitParam(param);
268 param.nPortIndex = static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT);
269 auto ret = client_->GetParameter(client_, OMX_IndexParamVideoPortFormat,
270 reinterpret_cast<int8_t *>(¶m), sizeof(param));
271 if (ret != HDF_SUCCESS) {
272 HDF_LOGE("%{public}s errNo[%{public}d] to GetParameter OMX_IndexParamVideoPortFormat", __func__, ret);
273 return false;
274 }
275 HDF_LOGI("set Format eCompressionFormat = %{public}d, eColorFormat=%{public}d",
276 param.eCompressionFormat, param.eColorFormat);
277 param.xFramerate = FRAME; // 30fps,Q16 format
278 if (codecMime_ == codecMime::AVC) {
279 param.eCompressionFormat = OMX_VIDEO_CodingAVC; // H264
280 } else {
281 param.eCompressionFormat = (OMX_VIDEO_CODINGTYPE)CODEC_OMX_VIDEO_CodingHEVC; // H265
282 }
283
284 ret = client_->SetParameter(client_, OMX_IndexParamVideoPortFormat,
285 reinterpret_cast<int8_t *>(¶m), sizeof(param));
286 if (ret != HDF_SUCCESS) {
287 HDF_LOGE("%{public}s errNo[%{public}d] to SetParameter OMX_IndexParamVideoPortFormat", __func__, ret);
288 return false;
289 }
290
291 return true;
292 }
293
UseBuffers()294 bool CodecHdiAdapterDecode::UseBuffers()
295 {
296 if (client_ == nullptr) {
297 HDF_LOGE("%{public}s error,client_ is null", __func__);
298 return HDF_FAILURE;
299 }
300 HDF_LOGI("...command to IDLE....");
301 auto ret = client_->SendCommand(client_, OMX_CommandStateSet, OMX_StateIdle, NULL, 0);
302 if (ret != HDF_SUCCESS) {
303 HDF_LOGE("%{public}s errNo[%{public}d] to SendCommand with StateSet:OMX_StateIdle", __func__, ret);
304 return false;
305 }
306
307 ret = UseBufferOnPort(PortIndex::PORT_INDEX_INPUT);
308 if (ret != HDF_SUCCESS) {
309 HDF_LOGE("%{public}s errNo[%{public}d] UseBufferOnPort PortIndex::PORT_INDEX_INPUT", __func__, ret);
310 return false;
311 }
312
313 ret = UseBufferOnPort(PortIndex::PORT_INDEX_OUTPUT);
314 if (ret != HDF_SUCCESS) {
315 HDF_LOGE("%{public}s errNo[%{public}d] UseBufferOnPort PortIndex::PORT_INDEX_OUTPUT", __func__, ret);
316 return false;
317 }
318
319 HDF_LOGI("Wait for OMX_StateIdle status");
320 enum OMX_STATETYPE status;
321 ret = client_->GetState(client_, &status);
322 if (ret != HDF_SUCCESS) {
323 HDF_LOGE("%{public}s GetState ret [%{public}x]", __func__, ret);
324 return false;
325 }
326 if (status != OMX_StateIdle) {
327 HDF_LOGI("Wait for OMX_StateLoaded status");
328 this->WaitForStatusChanged();
329 } else {
330 HDF_LOGI("status is %{public}d", status);
331 }
332
333 return true;
334 }
335
UseBufferOnPort(PortIndex portIndex,int bufferCount,int bufferSize)336 int32_t CodecHdiAdapterDecode::UseBufferOnPort(PortIndex portIndex, int bufferCount, int bufferSize)
337 {
338 if (client_ == nullptr || bufferCount <= 0 || bufferSize <= 0) {
339 return HDF_ERR_INVALID_PARAM;
340 }
341 for (int i = 0; i < bufferCount; i++) {
342 std::shared_ptr<OmxCodecBuffer> omxBuffer = std::make_shared<OmxCodecBuffer>();
343 omxBuffer->size = sizeof(OmxCodecBuffer);
344 omxBuffer->version.s.nVersionMajor = 1;
345 omxBuffer->bufferType = CODEC_BUFFER_TYPE_AVSHARE_MEM_FD;
346 int fd = AshmemCreate(0, bufferSize);
347 shared_ptr<Ashmem> sharedMem = make_shared<Ashmem>(fd, bufferSize);
348 omxBuffer->bufferLen = FD_SIZE;
349 omxBuffer->buffer = reinterpret_cast<uint8_t *>(fd);
350 omxBuffer->allocLen = bufferSize;
351 omxBuffer->fenceFd = -1;
352 omxBuffer->pts = 0;
353 omxBuffer->flag = 0;
354
355 if (portIndex == PortIndex::PORT_INDEX_INPUT) {
356 omxBuffer->type = READ_ONLY_TYPE;
357 sharedMem->MapReadAndWriteAshmem();
358 } else {
359 omxBuffer->type = READ_WRITE_TYPE;
360 sharedMem->MapReadOnlyAshmem();
361 }
362 auto ret = client_->AllocateBuffer(client_, static_cast<uint32_t>(portIndex), omxBuffer.get());
363 if (ret != HDF_SUCCESS) {
364 HDF_LOGE(
365 "%{public}s errNo[%{public}d] to AllocateBuffer with portIndex[%{public}d]", __func__, ret, portIndex);
366 sharedMem->UnmapAshmem();
367 sharedMem->CloseAshmem();
368 sharedMem = nullptr;
369 return ret;
370 }
371 omxBuffer->bufferLen = 0;
372
373 std::shared_ptr<BufferInfo> bufferInfo = std::make_shared<BufferInfo>();
374 bufferInfo->omxBuffer = omxBuffer;
375 bufferInfo->avSharedPtr = sharedMem;
376 bufferInfo->portIndex = portIndex;
377 omxBuffers_.emplace(std::make_pair(omxBuffer->bufferId, bufferInfo));
378 }
379
380 return HDF_SUCCESS;
381 }
382
UseBufferOnPort(PortIndex portIndex)383 int32_t CodecHdiAdapterDecode::UseBufferOnPort(PortIndex portIndex)
384 {
385 if (client_ == nullptr) {
386 HDF_LOGE("%{public}s error,client_ is null", __func__);
387 return HDF_FAILURE;
388 }
389 int32_t bufferSize = 0;
390 int32_t bufferCount = 0;
391 bool portEnable = false;
392
393 OMX_PARAM_PORTDEFINITIONTYPE param;
394 InitParam(param);
395 param.nPortIndex = (OMX_U32)portIndex;
396 auto ret = client_->GetParameter(client_, OMX_IndexParamPortDefinition, (int8_t *)¶m, sizeof(param));
397 if (ret != HDF_SUCCESS) {
398 HDF_LOGE("%{public}s errNo[%{public}d] GetParameter with OMX_IndexParamPortDefinition:portIndex[%{public}d]",
399 __func__, ret, portIndex);
400 return ret;
401 }
402
403 bufferSize = param.nBufferSize;
404 if (portIndex == PortIndex::PORT_INDEX_INPUT) {
405 if (bufferSize == 0) {
406 bufferSize = width_ * height_;
407 }
408 inputBufferSize_ = bufferSize;
409 } else if (portIndex == PortIndex::PORT_INDEX_OUTPUT) {
410 bufferSize = width_ * height_ * FRAME_SIZE_OPERATOR;
411 }
412 bufferCount = param.nBufferCountActual;
413 portEnable = param.bEnabled;
414 HDF_LOGI("buffer index [%{public}d], buffer size [%{public}d], "
415 "buffer count [%{public}d], portEnable[%{public}d], ret [%{public}d]",
416 portIndex, bufferSize, bufferCount, portEnable, ret);
417
418 ret = UseBufferOnPort(portIndex, bufferCount, bufferSize);
419 if (ret != HDF_SUCCESS) {
420 HDF_LOGE("%{public}s UseBufferOnPort ret[%{public}x]", __func__, ret);
421 return ret;
422 }
423
424 return HDF_SUCCESS;
425 }
426
FreeBuffers()427 void CodecHdiAdapterDecode::FreeBuffers()
428 {
429 if (client_ == nullptr) {
430 HDF_LOGE("%{public}s error,client_ is null", __func__);
431 return;
432 }
433 // command to loaded
434 (void)client_->SendCommand(client_, OMX_CommandStateSet, OMX_StateLoaded, nullptr, 0);
435
436 // release all the buffers
437 auto iter = omxBuffers_.begin();
438 while (iter != omxBuffers_.end()) {
439 auto bufferInfo = iter->second;
440 iter = omxBuffers_.erase(iter);
441 (void)client_->FreeBuffer(client_, static_cast<uint32_t>(bufferInfo->portIndex), bufferInfo->omxBuffer.get());
442 bufferInfo = nullptr;
443 }
444
445 unUsedInBuffers_.clear();
446 unUsedOutBuffers_.clear();
447
448 enum OMX_STATETYPE status;
449 auto ret = client_->GetState(client_, &status);
450 if (ret != HDF_SUCCESS) {
451 HDF_LOGE("%{public}s GetState error [%{public}x]", __func__, ret);
452 return;
453 }
454 // wait loaded
455 if (status != OMX_StateLoaded) {
456 HDF_LOGI("Wait for OMX_StateLoaded status");
457 this->WaitForStatusChanged();
458 } else {
459 HDF_LOGI("status is %{public}d", status);
460 }
461 }
462
Release()463 void CodecHdiAdapterDecode::Release()
464 {
465 omxMgr_->DestroyComponent(componentId_);
466 client_ = nullptr;
467 CodecComponentManagerRelease();
468 }
469
GetFreeBufferId()470 int CodecHdiAdapterDecode::GetFreeBufferId()
471 {
472 int bufferID = -1;
473 unique_lock<mutex> ulk(lockInputBuffers_);
474 size_t nSize = this->unUsedInBuffers_.size();
475 if (nSize > 0) {
476 bufferID = unUsedInBuffers_.front();
477 unUsedInBuffers_.pop_front();
478 }
479 return bufferID;
480 }
481
start()482 void CodecHdiAdapterDecode::start()
483 {
484 if (client_ == nullptr) {
485 HDF_LOGE("%{public}s error,client_ is null", __func__);
486 return;
487 }
488 auto ret = client_->SendCommand(client_, OMX_CommandStateSet, OMX_StateExecuting, NULL, 0);
489 if (ret != HDF_SUCCESS) {
490 HDF_LOGE("%{public}s errNo[%{public}d] to SendCommand with StateSet:OMX_StateIdle", __func__, ret);
491 return;
492 }
493 }
494
Run()495 void CodecHdiAdapterDecode::Run()
496 {
497 CodecHdiAdapterDecode::start();
498 auto t1 = std::chrono::system_clock::now();
499 bool eosFlag = false;
500 while (!eosFlag) {
501 int bufferID = GetFreeBufferId();
502 if (this->exit_) {
503 break;
504 }
505 if (bufferID < 0) {
506 usleep(USLEEP_TIME);
507 continue;
508 }
509 auto iter = omxBuffers_.find(bufferID);
510 if (iter == omxBuffers_.end()) {
511 continue;
512 }
513 auto bufferInfo = iter->second;
514 void *sharedAddr = const_cast<void *>(bufferInfo->avSharedPtr->ReadFromAshmem(0, 0));
515 if (needSplit_ == 1) {
516 eosFlag = this->ReadOnePacket(fpIn_, reinterpret_cast<uint8_t *>(sharedAddr),
517 bufferInfo->omxBuffer->filledLen);
518 } else {
519 eosFlag = this->ReadOneFrameFromFile(fpIn_, reinterpret_cast<uint8_t *>(sharedAddr),
520 bufferInfo->omxBuffer->filledLen);
521 }
522 bufferInfo->omxBuffer->offset = 0;
523 if (eosFlag) {
524 bufferInfo->omxBuffer->flag = OMX_BUFFERFLAG_EOS;
525 }
526 auto ret = client_->EmptyThisBuffer(client_, bufferInfo->omxBuffer.get());
527 if (ret != HDF_SUCCESS) {
528 HDF_LOGE("%{public}s EmptyThisBuffer error", __func__);
529 return;
530 }
531 }
532 // wait
533 while (!this->exit_) {
534 usleep(USLEEP_TIME);
535 }
536 auto t2 = std::chrono::system_clock::now();
537 std::chrono::duration<double> diff = t2 - t1;
538 HDF_LOGI("cost %{public}f, count=%{public}d", diff.count(), count_);
539 // command to IDLE
540 (void)client_->SendCommand(client_, OMX_CommandStateSet, OMX_StateIdle, NULL, 0);
541 return;
542 }
543
OnEvent(struct CodecCallbackType * self,OMX_EVENTTYPE event,struct EventInfo * info)544 int32_t CodecHdiAdapterDecode::OnEvent(struct CodecCallbackType *self, OMX_EVENTTYPE event, struct EventInfo *info)
545 {
546 if (event == OMX_EventCmdComplete) {
547 OMX_COMMANDTYPE cmd = (OMX_COMMANDTYPE)info->data1;
548 if (OMX_CommandStateSet == cmd) {
549 HDF_LOGI("OMX_CommandStateSet reached, status is %{public}d", info->data2);
550 g_core->OnStatusChanged();
551 }
552 }
553
554 return HDF_SUCCESS;
555 }
556
OnEmptyBufferDone(struct CodecCallbackType * self,int64_t appData,const struct OmxCodecBuffer * buffer)557 int32_t CodecHdiAdapterDecode::OnEmptyBufferDone(
558 struct CodecCallbackType *self, int64_t appData, const struct OmxCodecBuffer *buffer)
559 {
560 return g_core->OnEmptyBufferDone(*buffer);
561 }
562
OnFillBufferDone(struct CodecCallbackType * self,int64_t appData,const struct OmxCodecBuffer * buffer)563 int32_t CodecHdiAdapterDecode::OnFillBufferDone(
564 struct CodecCallbackType *self, int64_t appData, const struct OmxCodecBuffer *buffer)
565 {
566 return g_core->OnFillBufferDone(*buffer);
567 }
568
OnEmptyBufferDone(const struct OmxCodecBuffer & buffer)569 int32_t CodecHdiAdapterDecode::OnEmptyBufferDone(const struct OmxCodecBuffer &buffer)
570 {
571 unique_lock<mutex> ulk(lockInputBuffers_);
572 unUsedInBuffers_.push_back(buffer.bufferId);
573 return HDF_SUCCESS;
574 }
575
OnFillBufferDone(const struct OmxCodecBuffer & buffer)576 int32_t CodecHdiAdapterDecode::OnFillBufferDone(const struct OmxCodecBuffer &buffer)
577 {
578 if (client_ == nullptr) {
579 HDF_LOGE("%{public}s error,client_ is null", __func__);
580 return HDF_FAILURE;
581 }
582 if (exit_) {
583 return HDF_SUCCESS;
584 }
585
586 auto iter = omxBuffers_.find(buffer.bufferId);
587 if ((iter == omxBuffers_.end()) || (iter->second == nullptr)) {
588 return HDF_SUCCESS;
589 }
590 count_++;
591 // read buffer
592 auto bufferInfo = iter->second;
593 if (bufferInfo->avSharedPtr != nullptr) {
594 const void *addr = bufferInfo->avSharedPtr->ReadFromAshmem(buffer.filledLen, buffer.offset);
595 DumpOutputToFile(fpOut_, reinterpret_cast<uint8_t *>(const_cast<void *>(addr)));
596 }
597
598 (void)fflush(fpOut_);
599 if (buffer.flag == OMX_BUFFERFLAG_EOS) {
600 // end
601 exit_ = true;
602 HDF_LOGI("OnFillBufferDone the END coming");
603 return HDF_SUCCESS;
604 }
605 // call fillthisbuffer again
606 auto ret = client_->FillThisBuffer(client_, bufferInfo->omxBuffer.get());
607 if (ret != HDF_SUCCESS) {
608 HDF_LOGE("%{public}s FillThisBuffer error", __func__);
609 return HDF_SUCCESS;
610 }
611 return HDF_SUCCESS;
612 }
613
main(int argc,char * argv[])614 int main(int argc, char *argv[])
615 {
616 CommandOpt opt;
617 CommandAdapterParse parse;
618 if (!parse.Parse(argc, argv, opt)) {
619 return HDF_FAILURE;
620 }
621 if (g_core == nullptr) {
622 g_core = new CodecHdiAdapterDecode();
623 }
624 // Init width, height, input file
625 if (!g_core->Init(opt)) {
626 delete g_core;
627 g_core = nullptr;
628 return HDF_FAILURE;
629 }
630
631 if (!g_core->Configure()) {
632 delete g_core;
633 g_core = nullptr;
634 return HDF_FAILURE;
635 }
636
637 if (!g_core->UseBuffers()) {
638 delete g_core;
639 g_core = nullptr;
640 return HDF_FAILURE;
641 }
642
643 g_core->Run();
644 g_core->FreeBuffers();
645 g_core->Release();
646 delete g_core;
647 g_core = nullptr;
648 }
649