1 /*
2  * Copyright (C) 2024 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 <iostream>
17 #include <set>
18 #include <thread>
19 #include <dlfcn.h>
20 #include <malloc.h>
21 #include "syspara/parameters.h"
22 #include "securec.h"
23 #include "avcodec_trace.h"
24 #include "avcodec_log.h"
25 #include "utils.h"
26 #include "avcodec_codec_name.h"
27 #include "hevc_decoder.h"
28 #include <fstream>
29 #include <cstdarg>
30 
31 namespace OHOS {
32 namespace MediaAVCodec {
33 namespace Codec {
34 namespace {
35 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_FRAMEWORK, "HevcDecoderLoader"};
36 const char *HEVC_DEC_LIB_PATH = "libhevcdec_ohos.z.so";
37 const char *HEVC_DEC_CREATE_FUNC_NAME = "HEVC_CreateDecoder";
38 const char *HEVC_DEC_DECODE_FRAME_FUNC_NAME = "HEVC_DecodeFrame";
39 const char *HEVC_DEC_FLUSH_FRAME_FUNC_NAME = "HEVC_FlushFrame";
40 const char *HEVC_DEC_DELETE_FUNC_NAME = "HEVC_DeleteDecoder";
41 
42 constexpr uint32_t INDEX_INPUT = 0;
43 constexpr uint32_t INDEX_OUTPUT = 1;
44 constexpr int32_t DEFAULT_IN_BUFFER_CNT = 4;
45 constexpr int32_t DEFAULT_OUT_SURFACE_CNT = 4;
46 constexpr int32_t DEFAULT_OUT_BUFFER_CNT = 3;
47 constexpr int32_t DEFAULT_MIN_BUFFER_CNT = 2;
48 constexpr int32_t DEFAULT_MAX_BUFFER_CNT = 10;
49 constexpr uint32_t VIDEO_PIX_DEPTH_YUV = 3;
50 constexpr int32_t VIDEO_MIN_BUFFER_SIZE = 1474560; // 1280*768
51 constexpr int32_t VIDEO_MAX_BUFFER_SIZE = 3110400; // 1080p
52 constexpr int32_t VIDEO_MIN_SIZE = 2;
53 constexpr int32_t VIDEO_ALIGNMENT_SIZE = 2;
54 constexpr int32_t VIDEO_MAX_WIDTH_SIZE = 1920;
55 constexpr int32_t VIDEO_MAX_HEIGHT_SIZE = 1920;
56 constexpr int32_t DEFAULT_VIDEO_WIDTH = 1920;
57 constexpr int32_t DEFAULT_VIDEO_HEIGHT = 1080;
58 constexpr uint32_t DEFAULT_TRY_DECODE_TIME = 1;
59 constexpr uint32_t DEFAULT_TRY_REQ_TIME = 10;
60 constexpr int32_t VIDEO_INSTANCE_SIZE = 64;
61 constexpr int32_t VIDEO_BLOCKPERFRAME_SIZE = 36864;
62 constexpr int32_t VIDEO_BLOCKPERSEC_SIZE = 983040;
63 #ifdef BUILD_ENG_VERSION
64 constexpr uint32_t PATH_MAX_LEN = 128;
65 constexpr char DUMP_PATH[] = "/data/misc/hevcdecoderdump";
66 #endif
67 constexpr struct {
68     const std::string_view codecName;
69     const std::string_view mimeType;
70 } SUPPORT_HEVC_DECODER[] = {
71     {AVCodecCodecName::VIDEO_DECODER_HEVC_NAME, CodecMimeType::VIDEO_HEVC},
72 };
73 constexpr uint32_t SUPPORT_HEVC_DECODER_NUM = sizeof(SUPPORT_HEVC_DECODER) / sizeof(SUPPORT_HEVC_DECODER[0]);
74 } // namespace
75 using namespace OHOS::Media;
HevcDecoder(const std::string &name)76 HevcDecoder::HevcDecoder(const std::string &name) : codecName_(name), state_(State::UNINITIALIZED)
77 {
78     AVCODEC_SYNC_TRACE;
79     std::unique_lock<std::mutex> lock(decoderCountMutex_);
80     if (!freeIDSet_.empty()) {
81         decInstanceID_ = freeIDSet_[0];
82         freeIDSet_.erase(freeIDSet_.begin());
83         decInstanceIDSet_.push_back(decInstanceID_);
84     } else if (freeIDSet_.size() + decInstanceIDSet_.size() < VIDEO_INSTANCE_SIZE) {
85         decInstanceID_ = freeIDSet_.size() + decInstanceIDSet_.size();
86         decInstanceIDSet_.push_back(decInstanceID_);
87     } else {
88         decInstanceID_ = VIDEO_INSTANCE_SIZE + 1;
89     }
90     lock.unlock();
91 
92     if (decInstanceID_ < VIDEO_INSTANCE_SIZE) {
93         handle_ = dlopen(HEVC_DEC_LIB_PATH, RTLD_LAZY);
94         if (handle_ == nullptr) {
95             AVCODEC_LOGE("Load codec failed: %{public}s", HEVC_DEC_LIB_PATH);
96         }
97         HevcFuncMatch();
98         AVCODEC_LOGI("Num %{public}u HevcDecoder entered, state: Uninitialized", decInstanceID_);
99     } else {
100         AVCODEC_LOGE("HevcDecoder already has %{public}d instances, cannot has more instances", VIDEO_INSTANCE_SIZE);
101         state_ = State::ERROR;
102     }
103 
104     initParams_.logFxn = nullptr;
105     initParams_.uiChannelID = 0;
106     InitHevcParams();
107 }
108 
HevcFuncMatch()109 void HevcDecoder::HevcFuncMatch()
110 {
111     if (handle_ != nullptr) {
112         hevcDecoderCreateFunc_ = reinterpret_cast<CreateHevcDecoderFuncType>(dlsym(handle_,
113             HEVC_DEC_CREATE_FUNC_NAME));
114         hevcDecoderDecodecFrameFunc_ = reinterpret_cast<DecodeFuncType>(dlsym(handle_,
115             HEVC_DEC_DECODE_FRAME_FUNC_NAME));
116         hevcDecoderFlushFrameFunc_ = reinterpret_cast<FlushFuncType>(dlsym(handle_, HEVC_DEC_FLUSH_FRAME_FUNC_NAME));
117         hevcDecoderDeleteFunc_ = reinterpret_cast<DeleteFuncType>(dlsym(handle_, HEVC_DEC_DELETE_FUNC_NAME));
118         if (hevcDecoderCreateFunc_ == nullptr || hevcDecoderDecodecFrameFunc_ == nullptr ||
119             hevcDecoderDeleteFunc_ == nullptr || hevcDecoderFlushFrameFunc_ == nullptr) {
120                 AVCODEC_LOGE("HevcDecoder hevcFuncMatch_ failed!");
121                 ReleaseHandle();
122         }
123     }
124 }
125 
ReleaseHandle()126 void HevcDecoder::ReleaseHandle()
127 {
128     std::unique_lock<std::mutex> runLock(decRunMutex_);
129     hevcDecoderCreateFunc_ = nullptr;
130     hevcDecoderDecodecFrameFunc_ = nullptr;
131     hevcDecoderFlushFrameFunc_ = nullptr;
132     hevcDecoderDeleteFunc_ = nullptr;
133     if (handle_ != nullptr) {
134         dlclose(handle_);
135         handle_ = nullptr;
136     }
137     runLock.unlock();
138 }
139 
~HevcDecoder()140 HevcDecoder::~HevcDecoder()
141 {
142     ReleaseResource();
143     callback_ = nullptr;
144     ReleaseHandle();
145     if (decInstanceID_ < VIDEO_INSTANCE_SIZE) {
146         std::lock_guard<std::mutex> lock(decoderCountMutex_);
147         freeIDSet_.push_back(decInstanceID_);
148         auto it = std::find(decInstanceIDSet_.begin(), decInstanceIDSet_.end(), decInstanceID_);
149         if (it != decInstanceIDSet_.end()) {
150             decInstanceIDSet_.erase(it);
151         }
152     }
153 #ifdef BUILD_ENG_VERSION
154     if (dumpInFile_ != nullptr) {
155         dumpInFile_->close();
156     }
157     if (dumpOutFile_ != nullptr) {
158         dumpOutFile_->close();
159     }
160     if (dumpConvertFile_ != nullptr) {
161         dumpConvertFile_->close();
162     }
163 #endif
164     mallopt(M_FLUSH_THREAD_CACHE, 0);
165 }
166 
Initialize()167 int32_t HevcDecoder::Initialize()
168 {
169     AVCODEC_SYNC_TRACE;
170     CHECK_AND_RETURN_RET_LOG(!codecName_.empty(), AVCS_ERR_INVALID_VAL, "Init codec failed:  empty name");
171     std::string_view mime;
172     for (uint32_t i = 0; i < SUPPORT_HEVC_DECODER_NUM; ++i) {
173         if (SUPPORT_HEVC_DECODER[i].codecName == codecName_) {
174             mime = SUPPORT_HEVC_DECODER[i].mimeType;
175             break;
176         }
177     }
178     format_.PutStringValue(MediaDescriptionKey::MD_KEY_CODEC_MIME, mime);
179     format_.PutStringValue(MediaDescriptionKey::MD_KEY_CODEC_NAME, codecName_);
180     sendTask_ = std::make_shared<TaskThread>("SendFrame");
181     sendTask_->RegisterHandler([this] { SendFrame(); });
182 
183 #ifdef BUILD_ENG_VERSION
184     OpenDumpFile();
185 #endif
186     state_ = State::INITIALIZED;
187     AVCODEC_LOGI("Init codec successful,  state: Uninitialized -> Initialized");
188     return AVCS_ERR_OK;
189 }
190 
191 #ifdef BUILD_ENG_VERSION
OpenDumpFile()192 void HevcDecoder::OpenDumpFile()
193 {
194     std::string dumpModeStr = OHOS::system::GetParameter("hevcdecoder.dump", "0");
195     AVCODEC_LOGI("dumpModeStr %{public}s", dumpModeStr.c_str());
196 
197     char fileName[PATH_MAX_LEN] = {0};
198     if (dumpModeStr == "10" || dumpModeStr == "11") {
199         int ret = sprintf_s(fileName, sizeof(fileName), "%s/input_%p.h265", DUMP_PATH, this);
200         if (ret > 0) {
201             dumpInFile_ = std::make_shared<std::ofstream>();
202             dumpInFile_->open(fileName, std::ios::out | std::ios::binary);
203             if (!dumpInFile_->is_open()) {
204                 AVCODEC_LOGW("fail open file %{public}s", fileName);
205                 dumpInFile_ = nullptr;
206             }
207         }
208     }
209     if (dumpModeStr == "1" || dumpModeStr == "01" || dumpModeStr == "11") {
210         int ret = sprintf_s(fileName, sizeof(fileName), "%s/output_%p.yuv", DUMP_PATH, this);
211         if (ret > 0) {
212             dumpOutFile_ = std::make_shared<std::ofstream>();
213             dumpOutFile_->open(fileName, std::ios::out | std::ios::binary);
214             if (!dumpOutFile_->is_open()) {
215                 AVCODEC_LOGW("fail open file %{public}s", fileName);
216                 dumpOutFile_ = nullptr;
217             }
218         }
219         ret = sprintf_s(fileName, sizeof(fileName), "%s/outConvert_%p.data", DUMP_PATH, this);
220         if (ret > 0) {
221             dumpConvertFile_ = std::make_shared<std::ofstream>();
222             dumpConvertFile_->open(fileName, std::ios::out | std::ios::binary);
223             if (!dumpConvertFile_->is_open()) {
224                 AVCODEC_LOGW("fail open file %{public}s", fileName);
225                 dumpConvertFile_ = nullptr;
226             }
227         }
228     }
229 }
230 #endif
231 
ConfigureDefaultVal(const Format &format, const std::string_view &formatKey, int32_t minVal, int32_t maxVal)232 void HevcDecoder::ConfigureDefaultVal(const Format &format, const std::string_view &formatKey, int32_t minVal,
233                                       int32_t maxVal)
234 {
235     int32_t val32 = 0;
236     if (format.GetIntValue(formatKey, val32) && val32 >= minVal && val32 <= maxVal) {
237         format_.PutIntValue(formatKey, val32);
238     } else {
239         AVCODEC_LOGW("Set parameter failed: %{public}s, which minimum threshold=%{public}d, "
240                      "maximum threshold=%{public}d",
241                      formatKey.data(), minVal, maxVal);
242     }
243 }
244 
ConfigureSurface(const Format &format, const std::string_view &formatKey, FormatDataType formatType)245 void HevcDecoder::ConfigureSurface(const Format &format, const std::string_view &formatKey, FormatDataType formatType)
246 {
247     CHECK_AND_RETURN_LOG(formatType == FORMAT_TYPE_INT32, "Set parameter failed: type should be int32");
248 
249     int32_t val = 0;
250     CHECK_AND_RETURN_LOG(format.GetIntValue(formatKey, val), "Set parameter failed: get value fail");
251 
252     if (formatKey == MediaDescriptionKey::MD_KEY_PIXEL_FORMAT) {
253         VideoPixelFormat vpf = static_cast<VideoPixelFormat>(val);
254         CHECK_AND_RETURN_LOG(vpf == VideoPixelFormat::NV12 || vpf == VideoPixelFormat::NV21,
255             "Set parameter failed: pixel format value %{public}d invalid", val);
256         outputPixelFmt_ = vpf;
257         format_.PutIntValue(formatKey, val);
258     } else if (formatKey == MediaDescriptionKey::MD_KEY_ROTATION_ANGLE) {
259         VideoRotation sr = static_cast<VideoRotation>(val);
260         CHECK_AND_RETURN_LOG(sr == VideoRotation::VIDEO_ROTATION_0 || sr == VideoRotation::VIDEO_ROTATION_90 ||
261                                  sr == VideoRotation::VIDEO_ROTATION_180 || sr == VideoRotation::VIDEO_ROTATION_270,
262                              "Set parameter failed: rotation angle value %{public}d invalid", val);
263         format_.PutIntValue(MediaDescriptionKey::MD_KEY_ROTATION_ANGLE, val);
264     } else if (formatKey == MediaDescriptionKey::MD_KEY_SCALE_TYPE) {
265         ScalingMode scaleMode = static_cast<ScalingMode>(val);
266         CHECK_AND_RETURN_LOG(scaleMode == ScalingMode::SCALING_MODE_SCALE_TO_WINDOW ||
267                                  scaleMode == ScalingMode::SCALING_MODE_SCALE_CROP,
268                              "Set parameter failed: scale type value %{public}d invalid", val);
269         format_.PutIntValue(formatKey, val);
270     } else {
271         AVCODEC_LOGW("Set parameter failed: %{public}s, please check your parameter key", formatKey.data());
272         return;
273     }
274     AVCODEC_LOGI("Set parameter  %{public}s success, val %{public}d", formatKey.data(), val);
275 }
276 
Configure(const Format &format)277 int32_t HevcDecoder::Configure(const Format &format)
278 {
279     AVCODEC_SYNC_TRACE;
280     if (state_ == State::UNINITIALIZED) {
281         int32_t ret = Initialize();
282         CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, ret, "Init codec failed");
283     }
284     CHECK_AND_RETURN_RET_LOG((state_ == State::INITIALIZED), AVCS_ERR_INVALID_STATE,
285                              "Configure codec failed:  not in Initialized state");
286     format_.PutIntValue(MediaDescriptionKey::MD_KEY_WIDTH, DEFAULT_VIDEO_WIDTH);
287     format_.PutIntValue(MediaDescriptionKey::MD_KEY_HEIGHT, DEFAULT_VIDEO_HEIGHT);
288     format_.PutIntValue(MediaDescriptionKey::MD_KEY_MAX_OUTPUT_BUFFER_COUNT, DEFAULT_OUT_BUFFER_CNT);
289     format_.PutIntValue(MediaDescriptionKey::MD_KEY_MAX_INPUT_BUFFER_COUNT, DEFAULT_IN_BUFFER_CNT);
290     for (auto &it : format.GetFormatMap()) {
291         if (it.first == MediaDescriptionKey::MD_KEY_MAX_OUTPUT_BUFFER_COUNT) {
292             isOutBufSetted_ = true;
293             ConfigureDefaultVal(format, it.first, DEFAULT_MIN_BUFFER_CNT, DEFAULT_MAX_BUFFER_CNT);
294         } else if (it.first == MediaDescriptionKey::MD_KEY_MAX_INPUT_BUFFER_COUNT) {
295             ConfigureDefaultVal(format, it.first, DEFAULT_MIN_BUFFER_CNT, DEFAULT_MAX_BUFFER_CNT);
296         } else if (it.first == MediaDescriptionKey::MD_KEY_WIDTH) {
297             ConfigureDefaultVal(format, it.first, VIDEO_MIN_SIZE, VIDEO_MAX_WIDTH_SIZE);
298         } else if (it.first == MediaDescriptionKey::MD_KEY_HEIGHT) {
299             ConfigureDefaultVal(format, it.first, VIDEO_MIN_SIZE, VIDEO_MAX_HEIGHT_SIZE);
300         } else if (it.first == MediaDescriptionKey::MD_KEY_PIXEL_FORMAT ||
301                    it.first == MediaDescriptionKey::MD_KEY_ROTATION_ANGLE ||
302                    it.first == MediaDescriptionKey::MD_KEY_SCALE_TYPE) {
303             ConfigureSurface(format, it.first, it.second.type);
304         } else {
305             AVCODEC_LOGW("Set parameter failed: size:%{public}s, unsupport key", it.first.data());
306         }
307     }
308     format_.GetIntValue(MediaDescriptionKey::MD_KEY_WIDTH, width_);
309     format_.GetIntValue(MediaDescriptionKey::MD_KEY_HEIGHT, height_);
310 
311     initParams_.uiChannelID = decInstanceID_;
312     initParams_.logFxn = HevcDecLog;
313 
314     state_ = State::CONFIGURED;
315 
316     return AVCS_ERR_OK;
317 }
318 
IsActive() const319 bool HevcDecoder::IsActive() const
320 {
321     return state_ == State::RUNNING || state_ == State::FLUSHED || state_ == State::EOS;
322 }
323 
Start()324 int32_t HevcDecoder::Start()
325 {
326     AVCODEC_SYNC_TRACE;
327     CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, AVCS_ERR_INVALID_OPERATION, "Start codec failed: callback is null");
328     CHECK_AND_RETURN_RET_LOG((state_ == State::CONFIGURED || state_ == State::FLUSHED), AVCS_ERR_INVALID_STATE,
329                              "Start codec failed: not in Configured or Flushed state");
330 
331     std::unique_lock<std::mutex> runLock(decRunMutex_);
332     int32_t createRet = 0;
333     if (hevcSDecoder_ == nullptr && hevcDecoderCreateFunc_ != nullptr) {
334         createRet = hevcDecoderCreateFunc_(&hevcSDecoder_, &initParams_);
335     }
336     runLock.unlock();
337     CHECK_AND_RETURN_RET_LOG(createRet == 0 && hevcSDecoder_ != nullptr, AVCS_ERR_INVALID_OPERATION,
338                              "hevc deocder create failed");
339 
340     if (!isBufferAllocated_) {
341         for (int32_t i = 0; i < AV_NUM_DATA_POINTERS; i++) {
342             scaleData_[i] = nullptr;
343             scaleLineSize_[i] = 0;
344         }
345         isConverted_ = false;
346         int32_t ret = AllocateBuffers();
347         CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, ret, "Start codec failed: cannot allocate buffers");
348         isBufferAllocated_ = true;
349     }
350     state_ = State::RUNNING;
351     InitBuffers();
352     isSendEos_ = false;
353     sendTask_->Start();
354     AVCODEC_LOGI("Start codec successful, state: Running");
355     return AVCS_ERR_OK;
356 }
357 
InitBuffers()358 void HevcDecoder::InitBuffers()
359 {
360     inputAvailQue_->SetActive(true);
361     codecAvailQue_->SetActive(true);
362     if (sInfo_.surface != nullptr) {
363         renderAvailQue_->SetActive(true);
364     }
365     if (buffers_[INDEX_INPUT].size() > 0) {
366         for (uint32_t i = 0; i < buffers_[INDEX_INPUT].size(); i++) {
367             buffers_[INDEX_INPUT][i]->owner_ = HBuffer::Owner::OWNED_BY_USER;
368             callback_->OnInputBufferAvailable(i, buffers_[INDEX_INPUT][i]->avBuffer);
369             AVCODEC_LOGI("OnInputBufferAvailable frame index = %{public}d, owner = %{public}d", i,
370                          buffers_[INDEX_INPUT][i]->owner_.load());
371         }
372     }
373     if (buffers_[INDEX_OUTPUT].size() <= 0) {
374         return;
375     }
376     if (sInfo_.surface == nullptr) {
377         for (uint32_t i = 0; i < buffers_[INDEX_OUTPUT].size(); i++) {
378             buffers_[INDEX_OUTPUT][i]->owner_ = HBuffer::Owner::OWNED_BY_CODEC;
379             codecAvailQue_->Push(i);
380         }
381     } else {
382         for (uint32_t i = 0; i < buffers_[INDEX_OUTPUT].size(); i++) {
383             std::shared_ptr<FSurfaceMemory> surfaceMemory = buffers_[INDEX_OUTPUT][i]->sMemory;
384             if (surfaceMemory->GetSurfaceBuffer() == nullptr) {
385                 buffers_[INDEX_OUTPUT][i]->owner_ = HBuffer::Owner::OWNED_BY_SURFACE;
386                 renderAvailQue_->Push(i);
387             } else {
388                 buffers_[INDEX_OUTPUT][i]->owner_ = HBuffer::Owner::OWNED_BY_CODEC;
389                 codecAvailQue_->Push(i);
390             }
391         }
392     }
393     InitHevcParams();
394 }
395 
InitHevcParams()396 void HevcDecoder::InitHevcParams()
397 {
398     hevcDecoderInputArgs_.pStream = nullptr;
399     hevcDecoderInputArgs_.uiStreamLen = 0;
400     hevcDecoderInputArgs_.uiTimeStamp = 0;
401     hevcDecoderOutpusArgs_.pucOutYUV[0] = nullptr;
402     hevcDecoderOutpusArgs_.pucOutYUV[1] = nullptr; // 1 u channel
403     hevcDecoderOutpusArgs_.pucOutYUV[2] = nullptr; // 2 v channel
404     hevcDecoderOutpusArgs_.uiDecBitDepth = 0;
405     hevcDecoderOutpusArgs_.uiDecHeight = 0;
406     hevcDecoderOutpusArgs_.uiDecStride = 0;
407     hevcDecoderOutpusArgs_.uiDecWidth = 0;
408     hevcDecoderOutpusArgs_.uiTimeStamp = 0;
409 }
410 
ResetData()411 void HevcDecoder::ResetData()
412 {
413     if (scaleData_[0] != nullptr) {
414         if (isConverted_) {
415             av_free(scaleData_[0]);
416             isConverted_ = false;
417             scale_.reset();
418         }
419         for (int32_t i = 0; i < AV_NUM_DATA_POINTERS; i++) {
420             scaleData_[i] = nullptr;
421             scaleLineSize_[i] = 0;
422         }
423     }
424 }
425 
ResetBuffers()426 void HevcDecoder::ResetBuffers()
427 {
428     inputAvailQue_->Clear();
429     codecAvailQue_->Clear();
430     if (sInfo_.surface != nullptr) {
431         renderAvailQue_->Clear();
432         renderSurfaceBufferMap_.clear();
433     }
434     ResetData();
435 }
436 
StopThread()437 void HevcDecoder::StopThread()
438 {
439     if (inputAvailQue_ != nullptr) {
440         inputAvailQue_->SetActive(false, false);
441     }
442     if (codecAvailQue_ != nullptr) {
443         codecAvailQue_->SetActive(false, false);
444     }
445     if (sendTask_ != nullptr) {
446         sendTask_->Stop();
447     }
448     if (sInfo_.surface != nullptr && renderAvailQue_ != nullptr) {
449         renderAvailQue_->SetActive(false, false);
450     }
451 }
452 
Stop()453 int32_t HevcDecoder::Stop()
454 {
455     AVCODEC_SYNC_TRACE;
456     CHECK_AND_RETURN_RET_LOG((IsActive()), AVCS_ERR_INVALID_STATE, "Stop codec failed: not in executing state");
457     state_ = State::STOPPING;
458     inputAvailQue_->SetActive(false, false);
459     codecAvailQue_->SetActive(false, false);
460     sendTask_->Stop();
461 
462     if (sInfo_.surface != nullptr) {
463         renderAvailQue_->SetActive(false, false);
464     }
465 
466     std::unique_lock<std::mutex> runLock(decRunMutex_);
467     if (hevcSDecoder_ != nullptr && hevcDecoderDeleteFunc_ != nullptr) {
468         int ret = hevcDecoderDeleteFunc_(hevcSDecoder_);
469         if (ret != 0) {
470             AVCODEC_LOGE("Error: hevcDecoder delete error: %{public}d", ret);
471             callback_->OnError(AVCodecErrorType::AVCODEC_ERROR_INTERNAL, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN);
472             state_ = State::ERROR;
473         }
474         hevcSDecoder_ = nullptr;
475     }
476     runLock.unlock();
477 
478     ReleaseBuffers();
479     state_ = State::CONFIGURED;
480     AVCODEC_LOGI("Stop codec successful, state: Configured");
481     return AVCS_ERR_OK;
482 }
483 
Flush()484 int32_t HevcDecoder::Flush()
485 {
486     AVCODEC_SYNC_TRACE;
487     CHECK_AND_RETURN_RET_LOG((state_ == State::RUNNING || state_ == State::EOS), AVCS_ERR_INVALID_STATE,
488                              "Flush codec failed: not in running or Eos state");
489     state_ = State::FLUSHING;
490     inputAvailQue_->SetActive(false, false);
491     codecAvailQue_->SetActive(false, false);
492     sendTask_->Pause();
493 
494     if (sInfo_.surface != nullptr) {
495         renderAvailQue_->SetActive(false, false);
496     }
497 
498     ResetBuffers();
499     state_ = State::FLUSHED;
500     AVCODEC_LOGI("Flush codec successful, state: Flushed");
501     return AVCS_ERR_OK;
502 }
503 
Reset()504 int32_t HevcDecoder::Reset()
505 {
506     AVCODEC_SYNC_TRACE;
507     AVCODEC_LOGI("Reset codec called");
508     int32_t ret = Release();
509     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, ret, "Reset codec failed: cannot release codec");
510     ret = Initialize();
511     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, ret, "Reset codec failed: cannot init codec");
512     AVCODEC_LOGI("Reset codec successful, state: Initialized");
513     return AVCS_ERR_OK;
514 }
515 
ReleaseResource()516 void HevcDecoder::ReleaseResource()
517 {
518     StopThread();
519     ReleaseBuffers();
520     format_ = Format();
521     if (sInfo_.surface != nullptr) {
522         sInfo_.surface->CleanCache();
523         AVCODEC_LOGI("surface cleancache success");
524         int ret = UnRegisterListenerToSurface(sInfo_.surface);
525         if (ret != 0) {
526             callback_->OnError(AVCodecErrorType::AVCODEC_ERROR_INTERNAL, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN);
527             state_ = State::ERROR;
528         }
529     }
530     sInfo_.surface = nullptr;
531     std::unique_lock<std::mutex> runLock(decRunMutex_);
532     if (hevcSDecoder_ != nullptr && hevcDecoderDeleteFunc_ != nullptr) {
533         int ret = hevcDecoderDeleteFunc_(hevcSDecoder_);
534         if (ret != 0) {
535             AVCODEC_LOGE("Error: hevcDecoder delete error: %{public}d", ret);
536             callback_->OnError(AVCodecErrorType::AVCODEC_ERROR_INTERNAL, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN);
537             state_ = State::ERROR;
538         }
539         hevcSDecoder_ = nullptr;
540     }
541     runLock.unlock();
542 }
543 
Release()544 int32_t HevcDecoder::Release()
545 {
546     AVCODEC_SYNC_TRACE;
547     state_ = State::STOPPING;
548     ReleaseResource();
549     state_ = State::UNINITIALIZED;
550     AVCODEC_LOGI("Release codec successful, state: Uninitialized");
551     return AVCS_ERR_OK;
552 }
553 
SetSurfaceParameter(const Format &format, const std::string_view &formatKey, FormatDataType formatType)554 void HevcDecoder::SetSurfaceParameter(const Format &format, const std::string_view &formatKey,
555                                       FormatDataType formatType)
556 {
557     CHECK_AND_RETURN_LOG(formatType == FORMAT_TYPE_INT32, "Set parameter failed: type should be int32");
558     int32_t val = 0;
559     CHECK_AND_RETURN_LOG(format.GetIntValue(formatKey, val), "Set parameter failed: get value fail");
560     if (formatKey == MediaDescriptionKey::MD_KEY_PIXEL_FORMAT) {
561         VideoPixelFormat vpf = static_cast<VideoPixelFormat>(val);
562         CHECK_AND_RETURN_LOG(vpf == VideoPixelFormat::NV12 || vpf == VideoPixelFormat::NV21,
563             "Set parameter failed: pixel format value %{public}d invalid", val);
564         outputPixelFmt_ = vpf;
565         format_.PutIntValue(MediaDescriptionKey::MD_KEY_PIXEL_FORMAT, val);
566         GraphicPixelFormat surfacePixelFmt;
567         if (bitDepth_ == BIT_DEPTH10BIT) {
568             if (vpf == VideoPixelFormat::NV12) {
569                 surfacePixelFmt = GraphicPixelFormat::GRAPHIC_PIXEL_FMT_YCBCR_P010;
570             } else {
571                 surfacePixelFmt = GraphicPixelFormat::GRAPHIC_PIXEL_FMT_YCRCB_P010;
572             }
573         } else {
574             surfacePixelFmt = TranslateSurfaceFormat(vpf);
575         }
576         std::lock_guard<std::mutex> sLock(surfaceMutex_);
577         sInfo_.requestConfig.format = surfacePixelFmt;
578     } else if (formatKey == MediaDescriptionKey::MD_KEY_ROTATION_ANGLE) {
579         VideoRotation sr = static_cast<VideoRotation>(val);
580         CHECK_AND_RETURN_LOG(sr == VideoRotation::VIDEO_ROTATION_0 || sr == VideoRotation::VIDEO_ROTATION_90 ||
581                                  sr == VideoRotation::VIDEO_ROTATION_180 || sr == VideoRotation::VIDEO_ROTATION_270,
582                              "Set parameter failed: rotation angle value %{public}d invalid", val);
583         format_.PutIntValue(MediaDescriptionKey::MD_KEY_ROTATION_ANGLE, val);
584         std::lock_guard<std::mutex> sLock(surfaceMutex_);
585         sInfo_.surface->SetTransform(TranslateSurfaceRotation(sr));
586     } else if (formatKey == MediaDescriptionKey::MD_KEY_SCALE_TYPE) {
587         ScalingMode scaleMode = static_cast<ScalingMode>(val);
588         CHECK_AND_RETURN_LOG(scaleMode == ScalingMode::SCALING_MODE_SCALE_TO_WINDOW ||
589                                  scaleMode == ScalingMode::SCALING_MODE_SCALE_CROP,
590                              "Set parameter failed: scale type value %{public}d invalid", val);
591         format_.PutIntValue(MediaDescriptionKey::MD_KEY_SCALE_TYPE, val);
592         std::lock_guard<std::mutex> sLock(surfaceMutex_);
593         sInfo_.scalingMode = scaleMode;
594     } else {
595         AVCODEC_LOGW("Set parameter failed: %{public}s", formatKey.data());
596         return;
597     }
598     AVCODEC_LOGI("Set parameter %{public}s success, val %{publid}d", formatKey.data(), val);
599 }
600 
SetParameter(const Format &format)601 int32_t HevcDecoder::SetParameter(const Format &format)
602 {
603     AVCODEC_SYNC_TRACE;
604     for (auto &it : format.GetFormatMap()) {
605         if (sInfo_.surface != nullptr && it.second.type == FORMAT_TYPE_INT32) {
606             if (it.first == MediaDescriptionKey::MD_KEY_PIXEL_FORMAT ||
607                 it.first == MediaDescriptionKey::MD_KEY_ROTATION_ANGLE ||
608                 it.first == MediaDescriptionKey::MD_KEY_SCALE_TYPE) {
609                 SetSurfaceParameter(format, it.first, it.second.type);
610             }
611         } else {
612             AVCODEC_LOGW("Current Version, %{public}s is not supported", it.first.data());
613         }
614     }
615     AVCODEC_LOGI("Set parameter successful");
616     return AVCS_ERR_OK;
617 }
618 
GetOutputFormat(Format &format)619 int32_t HevcDecoder::GetOutputFormat(Format &format)
620 {
621     AVCODEC_SYNC_TRACE;
622     if ((!format_.ContainKey(MediaDescriptionKey::MD_KEY_MAX_INPUT_SIZE)) &&
623         hevcDecoderOutpusArgs_.uiDecStride != 0) {
624         int32_t stride = static_cast<int32_t>(hevcDecoderOutpusArgs_.uiDecStride);
625         int32_t maxInputSize = static_cast<int32_t>(static_cast<UINT32>(stride * height_ * VIDEO_PIX_DEPTH_YUV) >> 1);
626         format_.PutIntValue(MediaDescriptionKey::MD_KEY_MAX_INPUT_SIZE, maxInputSize);
627     }
628 
629     if (!format_.ContainKey(OHOS::Media::Tag::VIDEO_SLICE_HEIGHT)) {
630         format_.PutIntValue(OHOS::Media::Tag::VIDEO_SLICE_HEIGHT, height_);
631     }
632     if (!format_.ContainKey(OHOS::Media::Tag::VIDEO_PIC_WIDTH) ||
633         !format_.ContainKey(OHOS::Media::Tag::VIDEO_PIC_HEIGHT)) {
634         format_.PutIntValue(OHOS::Media::Tag::VIDEO_PIC_WIDTH, width_);
635         format_.PutIntValue(OHOS::Media::Tag::VIDEO_PIC_HEIGHT, height_);
636     }
637 
638     if (!format_.ContainKey(OHOS::Media::Tag::VIDEO_CROP_RIGHT) ||
639         !format_.ContainKey(OHOS::Media::Tag::VIDEO_CROP_BOTTOM)) {
640         format_.PutIntValue(OHOS::Media::Tag::VIDEO_CROP_RIGHT, width_-1);
641         format_.PutIntValue(OHOS::Media::Tag::VIDEO_CROP_BOTTOM, height_-1);
642         format_.PutIntValue(OHOS::Media::Tag::VIDEO_CROP_LEFT, 0);
643         format_.PutIntValue(OHOS::Media::Tag::VIDEO_CROP_TOP, 0);
644     }
645 
646     format = format_;
647     AVCODEC_LOGI("Get outputFormat successful");
648     return AVCS_ERR_OK;
649 }
650 
CalculateBufferSize()651 void HevcDecoder::CalculateBufferSize()
652 {
653     if ((static_cast<UINT32>(width_ * height_ * VIDEO_PIX_DEPTH_YUV) >> 1) <= VIDEO_MIN_BUFFER_SIZE) {
654         inputBufferSize_ = VIDEO_MIN_BUFFER_SIZE;
655     } else {
656         inputBufferSize_ = VIDEO_MAX_BUFFER_SIZE;
657     }
658     AVCODEC_LOGI("width = %{public}d, height = %{public}d, Input buffer size = %{public}d",
659                  width_, height_, inputBufferSize_);
660 }
661 
AllocateInputBuffer(int32_t bufferCnt, int32_t inBufferSize)662 int32_t HevcDecoder::AllocateInputBuffer(int32_t bufferCnt, int32_t inBufferSize)
663 {
664     int32_t valBufferCnt = 0;
665     for (int32_t i = 0; i < bufferCnt; i++) {
666         std::shared_ptr<HBuffer> buf = std::make_shared<HBuffer>();
667         std::shared_ptr<AVAllocator> allocator =
668             AVAllocatorFactory::CreateSharedAllocator(MemoryFlag::MEMORY_READ_WRITE);
669         CHECK_AND_CONTINUE_LOG(allocator != nullptr, "input buffer %{public}d allocator is nullptr", i);
670         buf->avBuffer = AVBuffer::CreateAVBuffer(allocator, inBufferSize);
671         CHECK_AND_CONTINUE_LOG(buf->avBuffer != nullptr, "Allocate input buffer failed, index=%{public}d", i);
672         AVCODEC_LOGI("Allocate input buffer success: index=%{public}d, size=%{public}d", i,
673                      buf->avBuffer->memory_->GetCapacity());
674 
675         buffers_[INDEX_INPUT].emplace_back(buf);
676         valBufferCnt++;
677     }
678     if (valBufferCnt < DEFAULT_MIN_BUFFER_CNT) {
679         AVCODEC_LOGE("Allocate input buffer failed: only %{public}d buffer is allocated, no memory", valBufferCnt);
680         buffers_[INDEX_INPUT].clear();
681         return AVCS_ERR_NO_MEMORY;
682     }
683     return AVCS_ERR_OK;
684 }
685 
SetSurfaceCfg(int32_t bufferCnt)686 int32_t HevcDecoder::SetSurfaceCfg(int32_t bufferCnt)
687 {
688     if (outputPixelFmt_ == VideoPixelFormat::UNKNOWN) {
689         format_.PutIntValue(MediaDescriptionKey::MD_KEY_PIXEL_FORMAT, static_cast<int32_t>(VideoPixelFormat::NV12));
690     }
691     int32_t val32 = 0;
692     format_.GetIntValue(MediaDescriptionKey::MD_KEY_PIXEL_FORMAT, val32);
693     GraphicPixelFormat surfacePixelFmt = TranslateSurfaceFormat(static_cast<VideoPixelFormat>(val32));
694     CHECK_AND_RETURN_RET_LOG(surfacePixelFmt != GraphicPixelFormat::GRAPHIC_PIXEL_FMT_BUTT, AVCS_ERR_UNSUPPORT,
695                              "Failed to allocate output buffer: unsupported surface format");
696     sInfo_.requestConfig.width = width_;
697     sInfo_.requestConfig.height = height_;
698     sInfo_.requestConfig.format = surfacePixelFmt;
699     if (sInfo_.surface != nullptr) {
700         CHECK_AND_RETURN_RET_LOG(sInfo_.surface->SetQueueSize(bufferCnt) == OHOS::SurfaceError::SURFACE_ERROR_OK,
701                                  AVCS_ERR_NO_MEMORY, "Surface set QueueSize=%{public}d failed", bufferCnt);
702 
703         format_.GetIntValue(MediaDescriptionKey::MD_KEY_SCALE_TYPE, val32);
704         sInfo_.scalingMode = static_cast<ScalingMode>(val32);
705         format_.GetIntValue(MediaDescriptionKey::MD_KEY_ROTATION_ANGLE, val32);
706         sInfo_.surface->SetTransform(TranslateSurfaceRotation(static_cast<VideoRotation>(val32)));
707     }
708     return AVCS_ERR_OK;
709 }
710 
AllocateOutputBuffer(int32_t bufferCnt)711 int32_t HevcDecoder::AllocateOutputBuffer(int32_t bufferCnt)
712 {
713     int32_t valBufferCnt = 0;
714     CHECK_AND_RETURN_RET_LOG(SetSurfaceCfg(bufferCnt) == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "SetSurfaceCfg failed");
715 
716     for (int i = 0; i < bufferCnt; i++) {
717         std::shared_ptr<HBuffer> buf = std::make_shared<HBuffer>();
718         if (sInfo_.surface == nullptr) {
719             std::shared_ptr<AVAllocator> allocator =
720                 AVAllocatorFactory::CreateSurfaceAllocator(sInfo_.requestConfig);
721             CHECK_AND_CONTINUE_LOG(allocator != nullptr, "output buffer %{public}d allocator is nullptr", i);
722             buf->avBuffer = AVBuffer::CreateAVBuffer(allocator, 0);
723             if (buf->avBuffer != nullptr) {
724                 AVCODEC_LOGI("Allocate output share buffer success: index=%{public}d, size=%{public}d", i,
725                              buf->avBuffer->memory_->GetCapacity());
726             }
727         } else {
728             buf->sMemory = std::make_shared<FSurfaceMemory>(&sInfo_);
729             CHECK_AND_CONTINUE_LOG(buf->sMemory->GetSurfaceBuffer() != nullptr,
730                                    "output surface memory %{public}d create fail", i);
731             outAVBuffer4Surface_.emplace_back(AVBuffer::CreateAVBuffer());
732             buf->avBuffer = AVBuffer::CreateAVBuffer(buf->sMemory->GetBase(), buf->sMemory->GetSize());
733             AVCODEC_LOGI("Allocate output surface buffer success: index=%{public}d, addr=%{public}p, size=%{public}d, "
734                          "stride=%{public}d",
735                          i, buf->sMemory->GetBase(), buf->sMemory->GetSize(),
736                          buf->sMemory->GetSurfaceBufferStride());
737         }
738         CHECK_AND_CONTINUE_LOG(buf->avBuffer != nullptr, "Allocate output buffer failed, index=%{public}d", i);
739 
740         buf->width = width_;
741         buf->height = height_;
742         buffers_[INDEX_OUTPUT].emplace_back(buf);
743         valBufferCnt++;
744     }
745     if (valBufferCnt < DEFAULT_MIN_BUFFER_CNT) {
746         AVCODEC_LOGE("Allocate output buffer failed: only %{public}d buffer is allocated, no memory", valBufferCnt);
747         buffers_[INDEX_INPUT].clear();
748         buffers_[INDEX_OUTPUT].clear();
749         return AVCS_ERR_NO_MEMORY;
750     }
751     return AVCS_ERR_OK;
752 }
753 
AllocateBuffers()754 int32_t HevcDecoder::AllocateBuffers()
755 {
756     AVCODEC_SYNC_TRACE;
757     CalculateBufferSize();
758     CHECK_AND_RETURN_RET_LOG(inputBufferSize_ > 0, AVCS_ERR_INVALID_VAL,
759                              "Allocate buffer with input size=%{public}d failed", inputBufferSize_);
760     if (sInfo_.surface != nullptr && isOutBufSetted_ == false) {
761         format_.PutIntValue(MediaDescriptionKey::MD_KEY_MAX_OUTPUT_BUFFER_COUNT, DEFAULT_OUT_SURFACE_CNT);
762     }
763     int32_t inputBufferCnt = 0;
764     int32_t outputBufferCnt = 0;
765     format_.GetIntValue(MediaDescriptionKey::MD_KEY_MAX_INPUT_BUFFER_COUNT, inputBufferCnt);
766     format_.GetIntValue(MediaDescriptionKey::MD_KEY_MAX_OUTPUT_BUFFER_COUNT, outputBufferCnt);
767     inputAvailQue_ = std::make_shared<BlockQueue<uint32_t>>("inputAvailQue", inputBufferCnt);
768     codecAvailQue_ = std::make_shared<BlockQueue<uint32_t>>("codecAvailQue", outputBufferCnt);
769     if (sInfo_.surface != nullptr) {
770         renderAvailQue_ = std::make_shared<BlockQueue<uint32_t>>("renderAvailQue", outputBufferCnt);
771     }
772     if (AllocateInputBuffer(inputBufferCnt, inputBufferSize_) == AVCS_ERR_NO_MEMORY ||
773         AllocateOutputBuffer(outputBufferCnt) == AVCS_ERR_NO_MEMORY) {
774         return AVCS_ERR_NO_MEMORY;
775     }
776     AVCODEC_LOGI("Allocate buffers successful");
777     return AVCS_ERR_OK;
778 }
779 
UpdateOutputBuffer(uint32_t index)780 int32_t HevcDecoder::UpdateOutputBuffer(uint32_t index)
781 {
782     std::shared_ptr<HBuffer> outputBuffer = buffers_[INDEX_OUTPUT][index];
783     if (width_ != outputBuffer->width || height_ != outputBuffer->height || bitDepth_ != outputBuffer->bitDepth) {
784         std::shared_ptr<AVAllocator> allocator =
785             AVAllocatorFactory::CreateSurfaceAllocator(sInfo_.requestConfig);
786         CHECK_AND_RETURN_RET_LOG(allocator != nullptr, AVCS_ERR_NO_MEMORY, "buffer %{public}d allocator is nullptr",
787                                  index);
788         outputBuffer->avBuffer = AVBuffer::CreateAVBuffer(allocator, 0);
789         CHECK_AND_RETURN_RET_LOG(outputBuffer->avBuffer != nullptr, AVCS_ERR_NO_MEMORY,
790                                  "Buffer allocate failed, index=%{public}d", index);
791         AVCODEC_LOGI("update output buffer success: index=%{public}d, size=%{public}d", index,
792                      outputBuffer->avBuffer->memory_->GetCapacity());
793 
794         outputBuffer->owner_ = HBuffer::Owner::OWNED_BY_CODEC;
795         outputBuffer->width = width_;
796         outputBuffer->height = height_;
797         outputBuffer->bitDepth = bitDepth_;
798     }
799     return AVCS_ERR_OK;
800 }
801 
UpdateSurfaceMemory(uint32_t index)802 int32_t HevcDecoder::UpdateSurfaceMemory(uint32_t index)
803 {
804     AVCODEC_SYNC_TRACE;
805     std::unique_lock<std::mutex> oLock(outputMutex_);
806     std::shared_ptr<HBuffer> outputBuffer = buffers_[INDEX_OUTPUT][index];
807     oLock.unlock();
808     if (width_ != outputBuffer->width || height_ != outputBuffer->height || bitDepth_ != outputBuffer->bitDepth) {
809         std::shared_ptr<FSurfaceMemory> surfaceMemory = outputBuffer->sMemory;
810         surfaceMemory->SetNeedRender(false);
811         surfaceMemory->ReleaseSurfaceBuffer();
812         while (state_ == State::RUNNING) {
813             std::unique_lock<std::mutex> sLock(surfaceMutex_);
814             sptr<SurfaceBuffer> surfaceBuffer = surfaceMemory->GetSurfaceBuffer();
815             sLock.unlock();
816             if (surfaceBuffer != nullptr) {
817                 break;
818             }
819             std::this_thread::sleep_for(std::chrono::milliseconds(DEFAULT_TRY_REQ_TIME));
820         }
821         outputBuffer->avBuffer =
822             AVBuffer::CreateAVBuffer(outputBuffer->sMemory->GetBase(), outputBuffer->sMemory->GetSize());
823         outputBuffer->width = width_;
824         outputBuffer->height = height_;
825         outputBuffer->bitDepth = bitDepth_;
826     }
827 
828     return AVCS_ERR_OK;
829 }
830 
CheckFormatChange(uint32_t index, int width, int height, int bitDepth)831 int32_t HevcDecoder::CheckFormatChange(uint32_t index, int width, int height, int bitDepth)
832 {
833     bool formatChanged = false;
834     if (width_ != width || height_ != height || bitDepth_ != bitDepth) {
835         AVCODEC_LOGI("format change, width: %{public}d->%{public}d, height: %{public}d->%{public}d, "
836                      "bitDepth: %{public}d->%{public}d", width_, width, height_, height, bitDepth_, bitDepth);
837         width_ = width;
838         height_ = height;
839         bitDepth_ = bitDepth;
840         ResetData();
841         scale_ = nullptr;
842         std::unique_lock<std::mutex> sLock(surfaceMutex_);
843         sInfo_.requestConfig.width = width_;
844         sInfo_.requestConfig.height = height_;
845         if (bitDepth_ == BIT_DEPTH10BIT) {
846             if (outputPixelFmt_ == VideoPixelFormat::NV12 || outputPixelFmt_ == VideoPixelFormat::UNKNOWN) {
847                 sInfo_.requestConfig.format = GraphicPixelFormat::GRAPHIC_PIXEL_FMT_YCBCR_P010;
848             } else {
849                 sInfo_.requestConfig.format = GraphicPixelFormat::GRAPHIC_PIXEL_FMT_YCRCB_P010;
850             }
851         }
852         sLock.unlock();
853         formatChanged = true;
854     }
855     if (sInfo_.surface == nullptr) {
856         std::lock_guard<std::mutex> oLock(outputMutex_);
857         CHECK_AND_RETURN_RET_LOG((UpdateOutputBuffer(index) == AVCS_ERR_OK), AVCS_ERR_NO_MEMORY,
858                                  "Update output buffer failed, index=%{public}u", index);
859     } else {
860         CHECK_AND_RETURN_RET_LOG((UpdateSurfaceMemory(index) == AVCS_ERR_OK), AVCS_ERR_NO_MEMORY,
861                                  "Update buffer failed");
862     }
863     if (!format_.ContainKey(OHOS::Media::Tag::VIDEO_STRIDE) || formatChanged) {
864         int32_t stride = GetSurfaceBufferStride(buffers_[INDEX_OUTPUT][index]);
865         CHECK_AND_RETURN_RET_LOG(stride > 0, AVCS_ERR_NO_MEMORY, "get GetSurfaceBufferStride failed");
866         format_.PutIntValue(OHOS::Media::Tag::VIDEO_STRIDE, stride);
867         format_.PutIntValue(MediaDescriptionKey::MD_KEY_WIDTH, width_);
868         format_.PutIntValue(MediaDescriptionKey::MD_KEY_HEIGHT, height_);
869         format_.PutIntValue(OHOS::Media::Tag::VIDEO_SLICE_HEIGHT, height_);
870         format_.PutIntValue(OHOS::Media::Tag::VIDEO_PIC_WIDTH, width_);
871         format_.PutIntValue(OHOS::Media::Tag::VIDEO_PIC_HEIGHT, height_);
872         format_.PutIntValue(OHOS::Media::Tag::VIDEO_CROP_RIGHT, width_-1);
873         format_.PutIntValue(OHOS::Media::Tag::VIDEO_CROP_BOTTOM, height_-1);
874         format_.PutIntValue(OHOS::Media::Tag::VIDEO_CROP_LEFT, 0);
875         format_.PutIntValue(OHOS::Media::Tag::VIDEO_CROP_TOP, 0);
876         callback_->OnOutputFormatChanged(format_);
877     }
878     return AVCS_ERR_OK;
879 }
880 
GetSurfaceBufferStride(const std::shared_ptr<HBuffer> &frameBuffer)881 int32_t HevcDecoder::GetSurfaceBufferStride(const std::shared_ptr<HBuffer> &frameBuffer)
882 {
883     int32_t surfaceBufferStride = 0;
884     if (sInfo_.surface == nullptr) {
885         auto surfaceBuffer = frameBuffer->avBuffer->memory_->GetSurfaceBuffer();
886         CHECK_AND_RETURN_RET_LOG(surfaceBuffer != nullptr, -1, "surfaceBuffer is nullptr");
887         auto bufferHandle = surfaceBuffer->GetBufferHandle();
888         CHECK_AND_RETURN_RET_LOG(bufferHandle != nullptr, -1, "fail to get bufferHandle");
889         surfaceBufferStride = bufferHandle->stride;
890     } else {
891         surfaceBufferStride = frameBuffer->sMemory->GetSurfaceBufferStride();
892     }
893     return surfaceBufferStride;
894 }
895 
ReleaseBuffers()896 void HevcDecoder::ReleaseBuffers()
897 {
898     ResetData();
899     if (!isBufferAllocated_) {
900         return;
901     }
902 
903     inputAvailQue_->Clear();
904     buffers_[INDEX_INPUT].clear();
905 
906     std::unique_lock<std::mutex> oLock(outputMutex_);
907     codecAvailQue_->Clear();
908     if (sInfo_.surface != nullptr) {
909         renderAvailQue_->Clear();
910         renderSurfaceBufferMap_.clear();
911         for (uint32_t i = 0; i < buffers_[INDEX_OUTPUT].size(); i++) {
912             std::shared_ptr<HBuffer> outputBuffer = buffers_[INDEX_OUTPUT][i];
913             if (outputBuffer->owner_ == HBuffer::Owner::OWNED_BY_CODEC) {
914                 std::shared_ptr<FSurfaceMemory> surfaceMemory = outputBuffer->sMemory;
915                 surfaceMemory->SetNeedRender(false);
916                 surfaceMemory->ReleaseSurfaceBuffer();
917                 outputBuffer->owner_ = HBuffer::Owner::OWNED_BY_SURFACE;
918             }
919         }
920     }
921     buffers_[INDEX_OUTPUT].clear();
922     oLock.unlock();
923     isBufferAllocated_ = false;
924 }
925 
QueueInputBuffer(uint32_t index)926 int32_t HevcDecoder::QueueInputBuffer(uint32_t index)
927 {
928     AVCODEC_SYNC_TRACE;
929     CHECK_AND_RETURN_RET_LOG(state_ == State::RUNNING, AVCS_ERR_INVALID_STATE,
930                              "Queue input buffer failed: not in Running state");
931     CHECK_AND_RETURN_RET_LOG(index < buffers_[INDEX_INPUT].size(), AVCS_ERR_INVALID_VAL,
932                              "Queue input buffer failed with bad index, index=%{public}u, buffer_size=%{public}zu",
933                              index, buffers_[INDEX_INPUT].size());
934     std::shared_ptr<HBuffer> inputBuffer = buffers_[INDEX_INPUT][index];
935     CHECK_AND_RETURN_RET_LOG(inputBuffer->owner_ == HBuffer::Owner::OWNED_BY_USER, AVCS_ERR_INVALID_OPERATION,
936                              "Queue input buffer failed: buffer with index=%{public}u is not available", index);
937 
938     inputBuffer->owner_ = HBuffer::Owner::OWNED_BY_CODEC;
939     inputAvailQue_->Push(index);
940     return AVCS_ERR_OK;
941 }
942 
SendFrame()943 void HevcDecoder::SendFrame()
944 {
945     if (state_ == State::STOPPING || state_ == State::FLUSHING) {
946         return;
947     } else if (state_ != State::RUNNING || isSendEos_) {
948         std::this_thread::sleep_for(std::chrono::milliseconds(DEFAULT_TRY_DECODE_TIME));
949         return;
950     }
951     uint32_t index = inputAvailQue_->Front();
952     CHECK_AND_RETURN_LOG(state_ == State::RUNNING, "Not in running state");
953     std::shared_ptr<HBuffer> &inputBuffer = buffers_[INDEX_INPUT][index];
954     std::shared_ptr<AVBuffer> &inputAVBuffer = inputBuffer->avBuffer;
955     if (inputAVBuffer->flag_ & AVCODEC_BUFFER_FLAG_EOS) {
956         hevcDecoderInputArgs_.pStream = nullptr;
957         isSendEos_ = true;
958         AVCODEC_LOGI("Send eos end");
959     } else {
960         hevcDecoderInputArgs_.pStream = inputAVBuffer->memory_->GetAddr();
961         hevcDecoderInputArgs_.uiStreamLen = static_cast<UINT32>(inputAVBuffer->memory_->GetSize());
962         hevcDecoderInputArgs_.uiTimeStamp = static_cast<UINT64>(inputAVBuffer->pts_);
963     }
964 
965 #ifdef BUILD_ENG_VERSION
966     if (dumpInFile_ && dumpInFile_->is_open() && !isSendEos_) {
967         dumpInFile_->write(reinterpret_cast<char*>(inputAVBuffer->memory_->GetAddr()),
968                            static_cast<int32_t>(inputAVBuffer->memory_->GetSize()));
969     }
970 #endif
971 
972     int32_t ret = 0;
973     std::unique_lock<std::mutex> runLock(decRunMutex_);
974     do {
975         ret = DecodeFrameOnce();
976     } while (ret == 0 && isSendEos_);
977     runLock.unlock();
978 
979     if (isSendEos_) {
980         auto outIndex = codecAvailQue_->Front();
981         std::shared_ptr<HBuffer> frameBuffer = buffers_[INDEX_OUTPUT][outIndex];
982         frameBuffer->avBuffer->flag_ = AVCODEC_BUFFER_FLAG_EOS;
983         FramePostProcess(buffers_[INDEX_OUTPUT][outIndex], outIndex, AVCS_ERR_OK, AVCS_ERR_OK);
984         state_ = State::EOS;
985     } else if (ret < 0) {
986         AVCODEC_LOGE("decode frame error: ret = %{public}d", ret);
987     }
988 
989     inputAvailQue_->Pop();
990     inputBuffer->owner_ = HBuffer::Owner::OWNED_BY_USER;
991     callback_->OnInputBufferAvailable(index, inputAVBuffer);
992 }
993 
DecodeFrameOnce()994 int32_t HevcDecoder::DecodeFrameOnce()
995 {
996     int32_t ret = 0;
997     if (hevcSDecoder_ != nullptr && hevcDecoderFlushFrameFunc_ != nullptr &&
998         hevcDecoderDecodecFrameFunc_ != nullptr) {
999         if (isSendEos_) {
1000             ret = hevcDecoderFlushFrameFunc_(hevcSDecoder_, &hevcDecoderOutpusArgs_);
1001         } else {
1002             ret = hevcDecoderDecodecFrameFunc_(hevcSDecoder_, &hevcDecoderInputArgs_, &hevcDecoderOutpusArgs_);
1003         }
1004     } else {
1005         AVCODEC_LOGW("hevcDecoderDecodecFrameFunc_ = nullptr || hevcSDecoder_ = nullptr || "
1006                         "hevcDecoderFlushFrameFunc_ = nullptr, cannot call decoder");
1007         ret = -1;
1008     }
1009     int32_t bitDepth = static_cast<int32_t>(hevcDecoderOutpusArgs_.uiDecBitDepth);
1010     if (ret == 0) {
1011         CHECK_AND_RETURN_RET_LOG(bitDepth == BIT_DEPTH8BIT || bitDepth == BIT_DEPTH10BIT, -1,
1012                                  "Unsupported bitDepth %{public}d", bitDepth);
1013         ConvertDecOutToAVFrame(bitDepth);
1014 #ifdef BUILD_ENG_VERSION
1015         DumpOutputBuffer(bitDepth);
1016 #endif
1017         auto index = codecAvailQue_->Front();
1018         CHECK_AND_RETURN_RET_LOG(state_ == State::RUNNING, -1, "Not in running state");
1019         std::shared_ptr<HBuffer> frameBuffer = buffers_[INDEX_OUTPUT][index];
1020         int32_t status = AVCS_ERR_OK;
1021         if (CheckFormatChange(index, cachedFrame_->width, cachedFrame_->height, bitDepth) == AVCS_ERR_OK) {
1022             CHECK_AND_RETURN_RET_LOG(state_ == State::RUNNING, -1, "Not in running state");
1023             frameBuffer = buffers_[INDEX_OUTPUT][index];
1024             status = FillFrameBuffer(frameBuffer);
1025         } else {
1026             CHECK_AND_RETURN_RET_LOG(state_ == State::RUNNING, -1, "Not in running state");
1027             callback_->OnError(AVCODEC_ERROR_EXTEND_START, AVCS_ERR_NO_MEMORY);
1028             return -1;
1029         }
1030         frameBuffer->avBuffer->flag_ = AVCODEC_BUFFER_FLAG_NONE;
1031         FramePostProcess(frameBuffer, index, status, AVCS_ERR_OK);
1032     }
1033     return ret;
1034 }
1035 
FillFrameBuffer(const std::shared_ptr<HBuffer> &frameBuffer)1036 int32_t HevcDecoder::FillFrameBuffer(const std::shared_ptr<HBuffer> &frameBuffer)
1037 {
1038     VideoPixelFormat targetPixelFmt = outputPixelFmt_;
1039     if (outputPixelFmt_ == VideoPixelFormat::UNKNOWN) {
1040         targetPixelFmt = VideoPixelFormat::NV12;
1041     }
1042     AVPixelFormat ffmpegFormat;
1043     if (bitDepth_ == BIT_DEPTH10BIT) {
1044         ffmpegFormat = AVPixelFormat::AV_PIX_FMT_P010LE;
1045     } else {
1046         ffmpegFormat = ConvertPixelFormatToFFmpeg(targetPixelFmt);
1047     }
1048     // yuv420 -> nv12 or nv21
1049     int32_t ret = ConvertVideoFrame(&scale_, cachedFrame_, scaleData_, scaleLineSize_, ffmpegFormat);
1050     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, ret, "Scale video frame failed: %{public}d", ret);
1051     isConverted_ = true;
1052 
1053     format_.PutIntValue(MediaDescriptionKey::MD_KEY_PIXEL_FORMAT, static_cast<int32_t>(targetPixelFmt));
1054     std::shared_ptr<AVMemory> &bufferMemory = frameBuffer->avBuffer->memory_;
1055     CHECK_AND_RETURN_RET_LOG(bufferMemory != nullptr, AVCS_ERR_INVALID_VAL, "bufferMemory is nullptr");
1056     bufferMemory->SetSize(0);
1057     struct SurfaceInfo surfaceInfo;
1058     surfaceInfo.scaleData = scaleData_;
1059     surfaceInfo.scaleLineSize = scaleLineSize_;
1060     int32_t surfaceStride = GetSurfaceBufferStride(frameBuffer);
1061     CHECK_AND_RETURN_RET_LOG(surfaceStride > 0, AVCS_ERR_INVALID_VAL, "get GetSurfaceBufferStride failed");
1062     surfaceInfo.surfaceStride = static_cast<uint32_t>(surfaceStride);
1063     if (sInfo_.surface) {
1064         surfaceInfo.surfaceFence = frameBuffer->sMemory->GetFence();
1065         ret = WriteSurfaceData(bufferMemory, surfaceInfo, format_);
1066     } else {
1067         Format bufferFormat;
1068         bufferFormat.PutIntValue(MediaDescriptionKey::MD_KEY_HEIGHT, height_);
1069         bufferFormat.PutIntValue(MediaDescriptionKey::MD_KEY_WIDTH, surfaceStride);
1070         bufferFormat.PutIntValue(MediaDescriptionKey::MD_KEY_PIXEL_FORMAT, static_cast<int32_t>(targetPixelFmt));
1071         ret = WriteBufferData(bufferMemory, scaleData_, scaleLineSize_, bufferFormat);
1072     }
1073 #ifdef BUILD_ENG_VERSION
1074     DumpConvertOut(surfaceInfo);
1075 #endif
1076     frameBuffer->avBuffer->pts_ = cachedFrame_->pts;
1077     AVCODEC_LOGD("Fill frame buffer successful");
1078     return ret;
1079 }
1080 
FramePostProcess(std::shared_ptr<HBuffer> &frameBuffer, uint32_t index, int32_t status, int ret)1081 void HevcDecoder::FramePostProcess(std::shared_ptr<HBuffer> &frameBuffer, uint32_t index, int32_t status, int ret)
1082 {
1083     if (status == AVCS_ERR_OK) {
1084         codecAvailQue_->Pop();
1085         frameBuffer->owner_ = HBuffer::Owner::OWNED_BY_USER;
1086         if (sInfo_.surface) {
1087             outAVBuffer4Surface_[index]->pts_ = frameBuffer->avBuffer->pts_;
1088             outAVBuffer4Surface_[index]->flag_ = frameBuffer->avBuffer->flag_;
1089         }
1090         callback_->OnOutputBufferAvailable(index, sInfo_.surface ?
1091             outAVBuffer4Surface_[index] : frameBuffer->avBuffer);
1092     } else if (status == AVCS_ERR_UNSUPPORT) {
1093         AVCODEC_LOGE("Recevie frame from codec failed: OnError");
1094         callback_->OnError(AVCodecErrorType::AVCODEC_ERROR_INTERNAL, AVCodecServiceErrCode::AVCS_ERR_UNSUPPORT);
1095         state_ = State::ERROR;
1096     } else {
1097         AVCODEC_LOGE("Recevie frame from codec failed");
1098         callback_->OnError(AVCodecErrorType::AVCODEC_ERROR_INTERNAL, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN);
1099         state_ = State::ERROR;
1100     }
1101 }
1102 
ConvertDecOutToAVFrame(int32_t bitDepth)1103 void HevcDecoder::ConvertDecOutToAVFrame(int32_t bitDepth)
1104 {
1105     if (cachedFrame_ == nullptr) {
1106         cachedFrame_ = std::shared_ptr<AVFrame>(av_frame_alloc(), [](AVFrame *p) { av_frame_free(&p); });
1107     }
1108     cachedFrame_->data[0] = hevcDecoderOutpusArgs_.pucOutYUV[0];
1109     cachedFrame_->data[1] = hevcDecoderOutpusArgs_.pucOutYUV[1]; // 1 u channel
1110     cachedFrame_->data[2] = hevcDecoderOutpusArgs_.pucOutYUV[2]; // 2 v channel
1111     if (bitDepth == BIT_DEPTH8BIT) {
1112         cachedFrame_->format = static_cast<int>(AVPixelFormat::AV_PIX_FMT_YUV420P);
1113         cachedFrame_->linesize[0] = static_cast<int32_t>(hevcDecoderOutpusArgs_.uiDecStride);
1114         cachedFrame_->linesize[1] = static_cast<int32_t>(hevcDecoderOutpusArgs_.uiDecStride >> 1); // 1 u channel
1115         cachedFrame_->linesize[2] = static_cast<int32_t>(hevcDecoderOutpusArgs_.uiDecStride >> 1); // 2 v channel
1116     } else {
1117         cachedFrame_->format = static_cast<int>(AVPixelFormat::AV_PIX_FMT_YUV420P10LE);
1118         cachedFrame_->linesize[0] =
1119             static_cast<int32_t>(hevcDecoderOutpusArgs_.uiDecStride * 2); // 2 10bit per pixel 2bytes
1120         cachedFrame_->linesize[1] = static_cast<int32_t>(hevcDecoderOutpusArgs_.uiDecStride); // 1 u channel
1121         cachedFrame_->linesize[2] = static_cast<int32_t>(hevcDecoderOutpusArgs_.uiDecStride); // 2 v channel
1122         if (outputPixelFmt_ == VideoPixelFormat::NV21) { // exchange uv channel
1123             cachedFrame_->data[1] = hevcDecoderOutpusArgs_.pucOutYUV[2]; // 2 u -> v
1124             cachedFrame_->data[2] = hevcDecoderOutpusArgs_.pucOutYUV[1]; // 2 v -> u
1125         }
1126     }
1127     cachedFrame_->width = static_cast<int32_t>(hevcDecoderOutpusArgs_.uiDecWidth);
1128     cachedFrame_->height = static_cast<int32_t>(hevcDecoderOutpusArgs_.uiDecHeight);
1129     cachedFrame_->pts = static_cast<int64_t>(hevcDecoderOutpusArgs_.uiTimeStamp);
1130 }
1131 
1132 #ifdef BUILD_ENG_VERSION
DumpOutputBuffer(int32_t bitDepth)1133 void HevcDecoder::DumpOutputBuffer(int32_t bitDepth)
1134 {
1135     if (!dumpOutFile_ || !dumpOutFile_->is_open()) {
1136         return;
1137     }
1138     int32_t pixelBytes = 1;
1139     if (bitDepth == BIT_DEPTH10BIT) {
1140         pixelBytes = 2; // 2
1141     }
1142     for (int32_t i = 0; i < cachedFrame_->height; i++) {
1143         dumpOutFile_->write(reinterpret_cast<char *>(cachedFrame_->data[0] + i * cachedFrame_->linesize[0]),
1144                             static_cast<int32_t>(cachedFrame_->width * pixelBytes));
1145     }
1146     for (int32_t i = 0; i < cachedFrame_->height / 2; i++) {  // 2
1147         dumpOutFile_->write(reinterpret_cast<char *>(cachedFrame_->data[1] + i * cachedFrame_->linesize[1]),
1148                             static_cast<int32_t>(cachedFrame_->width * pixelBytes / 2));  // 2
1149     }
1150     for (int32_t i = 0; i < cachedFrame_->height / 2; i++) {  // 2
1151         dumpOutFile_->write(reinterpret_cast<char *>(cachedFrame_->data[2] + i * cachedFrame_->linesize[2]),
1152                             static_cast<int32_t>(cachedFrame_->width * pixelBytes / 2)); // 2
1153     }
1154 }
1155 
DumpConvertOut(struct SurfaceInfo &surfaceInfo)1156 void HevcDecoder::DumpConvertOut(struct SurfaceInfo &surfaceInfo)
1157 {
1158     if (!dumpConvertFile_ || !dumpConvertFile_->is_open()) {
1159         return;
1160     }
1161     if (surfaceInfo.scaleData[0] != nullptr) {
1162         int32_t srcPos = 0;
1163         int32_t dataSize = surfaceInfo.scaleLineSize[0];
1164         int32_t writeSize = dataSize > static_cast<int32_t>(surfaceInfo.surfaceStride) ?
1165             static_cast<int32_t>(surfaceInfo.surfaceStride) : dataSize;
1166         for (int32_t i = 0; i < height_; i++) {
1167             dumpConvertFile_->write(reinterpret_cast<char *>(surfaceInfo.scaleData[0] + srcPos), writeSize);
1168             srcPos += dataSize;
1169         }
1170         srcPos = 0;
1171         dataSize = surfaceInfo.scaleLineSize[1];
1172         writeSize = dataSize > static_cast<int32_t>(surfaceInfo.surfaceStride) ?
1173             static_cast<int32_t>(surfaceInfo.surfaceStride) : dataSize;
1174         for (int32_t i = 0; i < height_ / 2; i++) {  // 2
1175             dumpConvertFile_->write(reinterpret_cast<char *>(surfaceInfo.scaleData[1] + srcPos), writeSize);
1176             srcPos += dataSize;
1177         }
1178     }
1179 }
1180 #endif
1181 
FindAvailIndex(uint32_t index)1182 void HevcDecoder::FindAvailIndex(uint32_t index)
1183 {
1184     uint32_t curQueSize = renderAvailQue_->Size();
1185     for (uint32_t i = 0u; i < curQueSize; i++) {
1186         uint32_t num = renderAvailQue_->Pop();
1187         if (num == index) {
1188             break;
1189         } else {
1190             renderAvailQue_->Push(num);
1191         }
1192     }
1193 }
1194 
RequestBufferFromConsumer()1195 void HevcDecoder::RequestBufferFromConsumer()
1196 {
1197     auto index = renderAvailQue_->Front();
1198     std::shared_ptr<HBuffer> outputBuffer = buffers_[INDEX_OUTPUT][index];
1199     std::shared_ptr<FSurfaceMemory> surfaceMemory = outputBuffer->sMemory;
1200     sptr<SurfaceBuffer> surfaceBuffer = surfaceMemory->GetSurfaceBuffer();
1201     if (surfaceBuffer == nullptr) {
1202         AVCODEC_LOGE("get buffer failed.");
1203         return;
1204     }
1205     auto queSize = renderAvailQue_->Size();
1206     uint32_t curIndex = 0;
1207     uint32_t i = 0;
1208     for (i = 0; i < queSize; i++) {
1209         curIndex = renderAvailQue_->Pop();
1210         if (surfaceMemory->GetBase() == buffers_[INDEX_OUTPUT][curIndex]->avBuffer->memory_->GetAddr() &&
1211             surfaceMemory->GetSize() == buffers_[INDEX_OUTPUT][curIndex]->avBuffer->memory_->GetCapacity()) {
1212             buffers_[INDEX_OUTPUT][index]->sMemory = buffers_[INDEX_OUTPUT][curIndex]->sMemory;
1213             buffers_[INDEX_OUTPUT][curIndex]->sMemory = surfaceMemory;
1214             break;
1215         } else {
1216             renderAvailQue_->Push(curIndex);
1217         }
1218     }
1219     if (i == queSize) {
1220         curIndex = index;
1221         outputBuffer->avBuffer = AVBuffer::CreateAVBuffer(surfaceMemory->GetBase(), surfaceMemory->GetSize());
1222         outputBuffer->width = width_;
1223         outputBuffer->height = height_;
1224         FindAvailIndex(curIndex);
1225     }
1226     buffers_[INDEX_OUTPUT][curIndex]->owner_ = HBuffer::Owner::OWNED_BY_CODEC;
1227     codecAvailQue_->Push(curIndex);
1228     if (renderSurfaceBufferMap_.count(curIndex)) {
1229         renderSurfaceBufferMap_.erase(curIndex);
1230     }
1231     AVCODEC_LOGD("Request output buffer success, index = %{public}u, queSize=%{public}zu, i=%{public}d", curIndex,
1232                  queSize, i);
1233 }
1234 
BufferReleasedByConsumer(uint64_t surfaceId)1235 GSError HevcDecoder::BufferReleasedByConsumer(uint64_t surfaceId)
1236 {
1237     CHECK_AND_RETURN_RET_LOG(state_ == State::RUNNING || state_ == State::EOS, GSERROR_NO_PERMISSION,
1238                              "In valid state");
1239     std::lock_guard<std::mutex> sLock(surfaceMutex_);
1240     CHECK_AND_RETURN_RET_LOG(renderAvailQue_->Size() > 0, GSERROR_NO_BUFFER, "No available buffer");
1241     CHECK_AND_RETURN_RET_LOG(surfaceId == sInfo_.surface->GetUniqueId(), GSERROR_INVALID_ARGUMENTS,
1242                              "Ignore callback from old surface");
1243     RequestBufferFromConsumer();
1244     return GSERROR_OK;
1245 }
1246 
UnRegisterListenerToSurface(const sptr<Surface> &surface)1247 int32_t HevcDecoder::UnRegisterListenerToSurface(const sptr<Surface> &surface)
1248 {
1249     GSError err = surface->UnRegisterReleaseListener();
1250     CHECK_AND_RETURN_RET_LOG(err == GSERROR_OK, AVCS_ERR_UNKNOWN,
1251                              "surface %{public}" PRIu64 ", UnRegisterReleaseListener failed, GSError=%{public}d",
1252                              surface->GetUniqueId(), err);
1253     return AVCS_ERR_OK;
1254 }
1255 
RegisterListenerToSurface(const sptr<Surface> &surface)1256 GSError HevcDecoder::RegisterListenerToSurface(const sptr<Surface> &surface)
1257 {
1258     uint64_t surfaceId = surface->GetUniqueId();
1259     wptr<HevcDecoder> wp = this;
1260     GSError err = surface->RegisterReleaseListener([wp, surfaceId](sptr<SurfaceBuffer> &) {
1261         sptr<HevcDecoder> codec = wp.promote();
1262         if (!codec) {
1263             AVCODEC_LOGD("decoder is gone");
1264             return GSERROR_OK;
1265         }
1266         return codec->BufferReleasedByConsumer(surfaceId);
1267     });
1268     return err;
1269 }
1270 
ReleaseOutputBuffer(uint32_t index)1271 int32_t HevcDecoder::ReleaseOutputBuffer(uint32_t index)
1272 {
1273     AVCODEC_SYNC_TRACE;
1274     std::unique_lock<std::mutex> oLock(outputMutex_);
1275     CHECK_AND_RETURN_RET_LOG(index < buffers_[INDEX_OUTPUT].size(), AVCS_ERR_INVALID_VAL,
1276                              "Failed to release output buffer: invalid index");
1277     std::shared_ptr<HBuffer> frameBuffer = buffers_[INDEX_OUTPUT][index];
1278     oLock.unlock();
1279     if (frameBuffer->owner_ == HBuffer::Owner::OWNED_BY_USER) {
1280         frameBuffer->owner_ = HBuffer::Owner::OWNED_BY_CODEC;
1281         codecAvailQue_->Push(index);
1282         return AVCS_ERR_OK;
1283     } else {
1284         AVCODEC_LOGE("Release output buffer failed: check your index=%{public}u", index);
1285         return AVCS_ERR_INVALID_VAL;
1286     }
1287 }
1288 
FlushSurfaceMemory(std::shared_ptr<FSurfaceMemory> &surfaceMemory, uint32_t index)1289 int32_t HevcDecoder::FlushSurfaceMemory(std::shared_ptr<FSurfaceMemory> &surfaceMemory, uint32_t index)
1290 {
1291     sptr<SurfaceBuffer> surfaceBuffer = surfaceMemory->GetSurfaceBuffer();
1292     CHECK_AND_RETURN_RET_LOG(surfaceBuffer != nullptr, AVCS_ERR_INVALID_VAL,
1293                              "Failed to update surface memory: surface buffer is NULL");
1294     OHOS::BufferFlushConfig flushConfig = {{0, 0, surfaceBuffer->GetWidth(), surfaceBuffer->GetHeight()},
1295         outAVBuffer4Surface_[index]->pts_, -1};
1296     surfaceMemory->SetNeedRender(true);
1297     surfaceMemory->UpdateSurfaceBufferScaleMode();
1298     if (outAVBuffer4Surface_[index]->meta_->Find(OHOS::Media::Tag::VIDEO_DECODER_DESIRED_PRESENT_TIMESTAMP) !=
1299         outAVBuffer4Surface_[index]->meta_->end()) {
1300         outAVBuffer4Surface_[index]->meta_->Get<OHOS::Media::Tag::VIDEO_DECODER_DESIRED_PRESENT_TIMESTAMP>(
1301             flushConfig.desiredPresentTimestamp);
1302         outAVBuffer4Surface_[index]->meta_->Remove(OHOS::Media::Tag::VIDEO_DECODER_DESIRED_PRESENT_TIMESTAMP);
1303     }
1304     auto res = sInfo_.surface->FlushBuffer(surfaceBuffer, -1, flushConfig);
1305     if (res != OHOS::SurfaceError::SURFACE_ERROR_OK) {
1306         AVCODEC_LOGW("Failed to update surface memory: %{public}d", res);
1307         surfaceMemory->SetNeedRender(false);
1308         surfaceMemory->ReleaseSurfaceBuffer();
1309         return AVCS_ERR_UNKNOWN;
1310     }
1311     renderSurfaceBufferMap_[index] = std::make_pair(surfaceBuffer, flushConfig);
1312     surfaceMemory->ReleaseSurfaceBuffer();
1313     return AVCS_ERR_OK;
1314 }
1315 
RenderOutputBuffer(uint32_t index)1316 int32_t HevcDecoder::RenderOutputBuffer(uint32_t index)
1317 {
1318     AVCODEC_SYNC_TRACE;
1319     CHECK_AND_RETURN_RET_LOG(sInfo_.surface != nullptr, AVCS_ERR_UNSUPPORT,
1320                              "RenderOutputBuffer fail, surface is nullptr");
1321     std::unique_lock<std::mutex> oLock(outputMutex_);
1322     CHECK_AND_RETURN_RET_LOG(index < buffers_[INDEX_OUTPUT].size(), AVCS_ERR_INVALID_VAL,
1323                              "Failed to render output buffer: invalid index");
1324     std::shared_ptr<HBuffer> frameBuffer = buffers_[INDEX_OUTPUT][index];
1325     oLock.unlock();
1326     std::lock_guard<std::mutex> sLock(surfaceMutex_);
1327     if (frameBuffer->owner_ == HBuffer::Owner::OWNED_BY_USER) {
1328         std::shared_ptr<FSurfaceMemory> surfaceMemory = frameBuffer->sMemory;
1329         int32_t ret = FlushSurfaceMemory(surfaceMemory, index);
1330         if (ret != AVCS_ERR_OK) {
1331             AVCODEC_LOGW("Update surface memory failed: %{public}d", static_cast<int32_t>(ret));
1332         } else {
1333             AVCODEC_LOGD("Update surface memory successful");
1334         }
1335         frameBuffer->owner_ = HBuffer::Owner::OWNED_BY_SURFACE;
1336         renderAvailQue_->Push(index);
1337         AVCODEC_LOGD("render output buffer with index, index=%{public}u", index);
1338         return AVCS_ERR_OK;
1339     } else {
1340         AVCODEC_LOGE("Failed to render output buffer with bad index, index=%{public}u", index);
1341         return AVCS_ERR_INVALID_VAL;
1342     }
1343 }
1344 
ReplaceOutputSurfaceWhenRunning(sptr<Surface> newSurface)1345 int32_t HevcDecoder::ReplaceOutputSurfaceWhenRunning(sptr<Surface> newSurface)
1346 {
1347     CHECK_AND_RETURN_RET_LOG(sInfo_.surface != nullptr, AV_ERR_OPERATE_NOT_PERMIT,
1348                              "Not support convert from AVBuffer Mode to Surface Mode");
1349     sptr<Surface> curSurface = sInfo_.surface;
1350     uint64_t oldId = curSurface->GetUniqueId();
1351     uint64_t newId = newSurface->GetUniqueId();
1352     AVCODEC_LOGI("surface %{public}" PRIu64 " -> %{public}" PRIu64 "", oldId, newId);
1353     if (oldId == newId) {
1354         return AVCS_ERR_OK;
1355     }
1356     GSError err = RegisterListenerToSurface(newSurface);
1357     CHECK_AND_RETURN_RET_LOG(err == GSERROR_OK, AVCS_ERR_UNKNOWN,
1358                              "surface %{public}" PRIu64 ", RegisterListenerToSurface failed, GSError=%{public}d",
1359                              newSurface->GetUniqueId(), err);
1360     int32_t outputBufferCnt = 0;
1361     format_.GetIntValue(MediaDescriptionKey::MD_KEY_MAX_OUTPUT_BUFFER_COUNT, outputBufferCnt);
1362     int32_t ret = SetQueueSize(newSurface, outputBufferCnt);
1363     if (ret != AVCS_ERR_OK) {
1364         UnRegisterListenerToSurface(newSurface);
1365         return ret;
1366     }
1367     std::unique_lock<std::mutex> sLock(surfaceMutex_);
1368     ret = SwitchBetweenSurface(newSurface);
1369     if (ret != AVCS_ERR_OK) {
1370         UnRegisterListenerToSurface(newSurface);
1371         sInfo_.surface = curSurface;
1372         return ret;
1373     }
1374     sLock.unlock();
1375     return AVCS_ERR_OK;
1376 }
1377 
SetQueueSize(const sptr<Surface> &surface, uint32_t targetSize)1378 int32_t HevcDecoder::SetQueueSize(const sptr<Surface> &surface, uint32_t targetSize)
1379 {
1380     int32_t err = surface->SetQueueSize(targetSize);
1381     if (err != 0) {
1382         AVCODEC_LOGE("surface %{public}" PRIu64 ", SetQueueSize to %{public}u failed, GSError=%{public}d",
1383             surface->GetUniqueId(), targetSize, err);
1384         return AVCS_ERR_UNKNOWN;
1385     }
1386     AVCODEC_LOGI("surface %{public}" PRIu64 ", SetQueueSize to %{public}u succ", surface->GetUniqueId(), targetSize);
1387     return AVCS_ERR_OK;
1388 }
1389 
SwitchBetweenSurface(const sptr<Surface> &newSurface)1390 int32_t HevcDecoder::SwitchBetweenSurface(const sptr<Surface> &newSurface)
1391 {
1392     sptr<Surface> curSurface = sInfo_.surface;
1393     newSurface->Connect(); // cleancache will work only if the surface is connected by us
1394     newSurface->CleanCache(); // make sure new surface is empty
1395     std::vector<uint32_t> ownedBySurfaceBufferIndex;
1396     uint64_t newId = newSurface->GetUniqueId();
1397     for (uint32_t index = 0; index < buffers_[INDEX_OUTPUT].size(); index++) {
1398         if (buffers_[INDEX_OUTPUT][index]->sMemory == nullptr) {
1399             continue;
1400         }
1401         sptr<SurfaceBuffer> surfaceBuffer = nullptr;
1402         if (buffers_[INDEX_OUTPUT][index]->owner_ == HBuffer::Owner::OWNED_BY_SURFACE) {
1403             if (renderSurfaceBufferMap_.count(index)) {
1404                 surfaceBuffer = renderSurfaceBufferMap_[index].first;
1405                 ownedBySurfaceBufferIndex.push_back(index);
1406             }
1407         } else {
1408             surfaceBuffer = buffers_[INDEX_OUTPUT][index]->sMemory->GetSurfaceBuffer();
1409         }
1410         if (surfaceBuffer == nullptr) {
1411             AVCODEC_LOGE("Get old surface buffer error!");
1412             return AVCS_ERR_UNKNOWN;
1413         }
1414         int32_t err = newSurface->AttachBufferToQueue(surfaceBuffer);
1415         if (err != 0) {
1416             AVCODEC_LOGE("surface %{public}" PRIu64 ", AttachBufferToQueue(seq=%{public}u) failed, GSError=%{public}d",
1417                 newId, surfaceBuffer->GetSeqNum(), err);
1418             return AVCS_ERR_UNKNOWN;
1419         }
1420     }
1421     int32_t videoRotation = 0;
1422     format_.GetIntValue(MediaDescriptionKey::MD_KEY_ROTATION_ANGLE, videoRotation);
1423     newSurface->SetTransform(TranslateSurfaceRotation(static_cast<VideoRotation>(videoRotation)));
1424     sInfo_.surface = newSurface;
1425 
1426     for (uint32_t index: ownedBySurfaceBufferIndex) {
1427         int32_t ret = RenderNewSurfaceWithOldBuffer(newSurface, index);
1428         if (ret != AVCS_ERR_OK) {
1429             return ret;
1430         }
1431     }
1432 
1433     int32_t ret = UnRegisterListenerToSurface(curSurface);
1434     if (ret != AVCS_ERR_OK) {
1435         return ret;
1436     }
1437 
1438     curSurface->CleanCache(true); // make sure old surface is empty and go black
1439     return AVCS_ERR_OK;
1440 }
1441 
RenderNewSurfaceWithOldBuffer(const sptr<Surface> &newSurface, uint32_t index)1442 int32_t HevcDecoder::RenderNewSurfaceWithOldBuffer(const sptr<Surface> &newSurface, uint32_t index)
1443 {
1444     std::shared_ptr<FSurfaceMemory> surfaceMemory = buffers_[INDEX_OUTPUT][index]->sMemory;
1445     sptr<SurfaceBuffer> surfaceBuffer = renderSurfaceBufferMap_[index].first;
1446     OHOS::BufferFlushConfig flushConfig = renderSurfaceBufferMap_[index].second;
1447     surfaceMemory->SetNeedRender(true);
1448     newSurface->SetScalingMode(surfaceBuffer->GetSeqNum(), sInfo_.scalingMode);
1449     auto res = newSurface->FlushBuffer(surfaceBuffer, -1, flushConfig);
1450     if (res != OHOS::SurfaceError::SURFACE_ERROR_OK) {
1451         AVCODEC_LOGE("Failed to update surface memory: %{public}d", res);
1452         surfaceMemory->SetNeedRender(false);
1453         return AVCS_ERR_UNKNOWN;
1454     }
1455     return AVCS_ERR_OK;
1456 }
1457 
SetOutputSurface(sptr<Surface> surface)1458 int32_t HevcDecoder::SetOutputSurface(sptr<Surface> surface)
1459 {
1460     AVCODEC_SYNC_TRACE;
1461     CHECK_AND_RETURN_RET_LOG(state_ != State::UNINITIALIZED, AV_ERR_INVALID_VAL,
1462                              "set output surface fail: not initialized or configured");
1463     CHECK_AND_RETURN_RET_LOG((state_ == State::CONFIGURED || state_ == State::FLUSHED ||
1464         state_ == State::RUNNING || state_ == State::EOS), AVCS_ERR_INVALID_STATE,
1465         "set output surface fail: state %{public}d not support set output surface",
1466         static_cast<int32_t>(state_.load()));
1467     if (surface == nullptr || surface->IsConsumer()) {
1468         AVCODEC_LOGE("Set surface fail");
1469         return AVCS_ERR_INVALID_VAL;
1470     }
1471     if (state_ == State::FLUSHED || state_ == State::RUNNING || state_ == State::EOS) {
1472         return ReplaceOutputSurfaceWhenRunning(surface);
1473     }
1474     sInfo_.surface = surface;
1475     GSError err = RegisterListenerToSurface(sInfo_.surface);
1476     CHECK_AND_RETURN_RET_LOG(err == GSERROR_OK, AVCS_ERR_UNKNOWN,
1477                              "surface %{public}" PRIu64 ", RegisterListenerToSurface failed, GSError=%{public}d",
1478                              sInfo_.surface->GetUniqueId(), err);
1479     if (!format_.ContainKey(MediaDescriptionKey::MD_KEY_SCALE_TYPE)) {
1480         format_.PutIntValue(MediaDescriptionKey::MD_KEY_SCALE_TYPE,
1481                             static_cast<int32_t>(ScalingMode::SCALING_MODE_SCALE_TO_WINDOW));
1482     }
1483     if (!format_.ContainKey(MediaDescriptionKey::MD_KEY_ROTATION_ANGLE)) {
1484         format_.PutIntValue(MediaDescriptionKey::MD_KEY_ROTATION_ANGLE,
1485                             static_cast<int32_t>(VideoRotation::VIDEO_ROTATION_0));
1486     }
1487     AVCODEC_LOGI("Set surface success");
1488     return AVCS_ERR_OK;
1489 }
1490 
SetCallback(const std::shared_ptr<MediaCodecCallback> &callback)1491 int32_t HevcDecoder::SetCallback(const std::shared_ptr<MediaCodecCallback> &callback)
1492 {
1493     AVCODEC_SYNC_TRACE;
1494     CHECK_AND_RETURN_RET_LOG(callback != nullptr, AVCS_ERR_INVALID_VAL, "Set callback failed: callback is NULL");
1495     callback_ = callback;
1496     return AVCS_ERR_OK;
1497 }
1498 
CheckHevcDecLibStatus()1499 int32_t HevcDecoder::CheckHevcDecLibStatus()
1500 {
1501     void* handle = dlopen(HEVC_DEC_LIB_PATH, RTLD_LAZY);
1502     if (handle != nullptr) {
1503         auto hevcDecoderCreateFunc = reinterpret_cast<CreateHevcDecoderFuncType>(
1504             dlsym(handle, HEVC_DEC_CREATE_FUNC_NAME));
1505         auto hevcDecoderDecodecFrameFunc = reinterpret_cast<DecodeFuncType>(
1506             dlsym(handle, HEVC_DEC_DECODE_FRAME_FUNC_NAME));
1507         auto hevcDecoderFlushFrameFunc = reinterpret_cast<FlushFuncType>(dlsym(handle, HEVC_DEC_FLUSH_FRAME_FUNC_NAME));
1508         auto hevcDecoderDeleteFunc = reinterpret_cast<DeleteFuncType>(dlsym(handle, HEVC_DEC_DELETE_FUNC_NAME));
1509         if (hevcDecoderCreateFunc == nullptr || hevcDecoderDecodecFrameFunc == nullptr ||
1510             hevcDecoderDeleteFunc == nullptr || hevcDecoderFlushFrameFunc == nullptr) {
1511                 AVCODEC_LOGE("HevcDecoder hevcFuncMatch_ failed!");
1512                 hevcDecoderCreateFunc = nullptr;
1513                 hevcDecoderDecodecFrameFunc = nullptr;
1514                 hevcDecoderFlushFrameFunc = nullptr;
1515                 hevcDecoderDeleteFunc = nullptr;
1516                 dlclose(handle);
1517                 handle = nullptr;
1518             }
1519     }
1520 
1521     if (handle == nullptr) {
1522         return AVCS_ERR_UNSUPPORT;
1523     }
1524     dlclose(handle);
1525     handle = nullptr;
1526 
1527     return AVCS_ERR_OK;
1528 }
1529 
GetCodecCapability(std::vector<CapabilityData> &capaArray)1530 int32_t HevcDecoder::GetCodecCapability(std::vector<CapabilityData> &capaArray)
1531 {
1532     CHECK_AND_RETURN_RET_LOG(CheckHevcDecLibStatus() == AVCS_ERR_OK, AVCS_ERR_UNSUPPORT,
1533                              "hevc decoder libs not available");
1534 
1535     for (uint32_t i = 0; i < SUPPORT_HEVC_DECODER_NUM; ++i) {
1536         CapabilityData capsData;
1537         capsData.codecName = static_cast<std::string>(SUPPORT_HEVC_DECODER[i].codecName);
1538         capsData.mimeType = static_cast<std::string>(SUPPORT_HEVC_DECODER[i].mimeType);
1539         capsData.codecType = AVCODEC_TYPE_VIDEO_DECODER;
1540         capsData.isVendor = false;
1541         capsData.maxInstance = VIDEO_INSTANCE_SIZE;
1542         capsData.alignment.width = VIDEO_ALIGNMENT_SIZE;
1543         capsData.alignment.height = VIDEO_ALIGNMENT_SIZE;
1544         capsData.width.minVal = VIDEO_MIN_SIZE;
1545         capsData.width.maxVal = VIDEO_MAX_WIDTH_SIZE;
1546         capsData.height.minVal = VIDEO_MIN_SIZE;
1547         capsData.height.maxVal = VIDEO_MAX_HEIGHT_SIZE;
1548         capsData.blockPerFrame.minVal = 1;
1549         capsData.blockPerFrame.maxVal = VIDEO_BLOCKPERFRAME_SIZE;
1550         capsData.blockPerSecond.minVal = 1;
1551         capsData.blockPerSecond.maxVal = VIDEO_BLOCKPERSEC_SIZE;
1552         capsData.blockSize.width = VIDEO_ALIGN_SIZE;
1553         capsData.blockSize.height = VIDEO_ALIGN_SIZE;
1554         capsData.pixFormat = {static_cast<int32_t>(VideoPixelFormat::NV12),
1555             static_cast<int32_t>(VideoPixelFormat::NV21)};
1556         capsData.profiles = {static_cast<int32_t>(HEVC_PROFILE_MAIN), static_cast<int32_t>(HEVC_PROFILE_MAIN_10)};
1557 
1558         std::vector<int32_t> levels;
1559         for (int32_t j = 0; j <= static_cast<int32_t>(HEVCLevel::HEVC_LEVEL_62); ++j) {
1560             levels.emplace_back(j);
1561         }
1562         capsData.profileLevelsMap.insert(std::make_pair(static_cast<int32_t>(HEVC_PROFILE_MAIN), levels));
1563         capsData.profileLevelsMap.insert(std::make_pair(static_cast<int32_t>(HEVC_PROFILE_MAIN_10), levels));
1564         capaArray.emplace_back(capsData);
1565     }
1566     return AVCS_ERR_OK;
1567 }
1568 
HevcDecLog(UINT32 channelId, IHW265VIDEO_ALG_LOG_LEVEL eLevel, INT8 *pMsg, ...)1569 void HevcDecLog(UINT32 channelId, IHW265VIDEO_ALG_LOG_LEVEL eLevel, INT8 *pMsg, ...)
1570 {
1571     va_list args;
1572     int32_t maxSize = 1024; // 1024 max size of one log
1573     std::vector<char> buf(maxSize);
1574     va_start(args, reinterpret_cast<const char*>(pMsg));
1575     int32_t size = vsnprintf_s(buf.data(), buf.size(), buf.size()-1, reinterpret_cast<const char*>(pMsg), args);
1576     va_end(args);
1577     if (size >= maxSize) {
1578         size = maxSize - 1;
1579     }
1580 
1581     auto msg = std::string(buf.data(), size);
1582 
1583     if (eLevel <= IHW265VIDEO_ALG_LOG_ERROR) {
1584         switch (eLevel) {
1585             case IHW265VIDEO_ALG_LOG_ERROR: {
1586                 AVCODEC_LOGE("%{public}s", msg.c_str());
1587                 break;
1588             }
1589             case IHW265VIDEO_ALG_LOG_WARNING: {
1590                 AVCODEC_LOGW("%{public}s", msg.c_str());
1591                 break;
1592             }
1593             case IHW265VIDEO_ALG_LOG_INFO: {
1594                 AVCODEC_LOGI("%{public}s", msg.c_str());
1595                 break;
1596             }
1597             case IHW265VIDEO_ALG_LOG_DEBUG: {
1598                 AVCODEC_LOGD("%{public}s", msg.c_str());
1599                 break;
1600             }
1601             default: {
1602                 AVCODEC_LOGI("%{public}s", msg.c_str());
1603                 break;
1604             }
1605         }
1606     }
1607 
1608     return;
1609 }
1610 
1611 std::mutex HevcDecoder::decoderCountMutex_;
1612 std::vector<uint32_t> HevcDecoder::decInstanceIDSet_;
1613 std::vector<uint32_t> HevcDecoder::freeIDSet_;
1614 
1615 } // namespace Codec
1616 } // namespace MediaAVCodec
1617 } // namespace OHOS
1618