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 int ret = UnRegisterListenerToSurface(sInfo_.surface);
523 if (ret != 0) {
524 callback_->OnError(AVCodecErrorType::AVCODEC_ERROR_INTERNAL, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN);
525 state_ = State::ERROR;
526 }
527 }
528 sInfo_.surface = nullptr;
529 std::unique_lock<std::mutex> runLock(decRunMutex_);
530 if (hevcSDecoder_ != nullptr && hevcDecoderDeleteFunc_ != nullptr) {
531 int ret = hevcDecoderDeleteFunc_(hevcSDecoder_);
532 if (ret != 0) {
533 AVCODEC_LOGE("Error: hevcDecoder delete error: %{public}d", ret);
534 callback_->OnError(AVCodecErrorType::AVCODEC_ERROR_INTERNAL, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN);
535 state_ = State::ERROR;
536 }
537 hevcSDecoder_ = nullptr;
538 }
539 runLock.unlock();
540 }
541
Release()542 int32_t HevcDecoder::Release()
543 {
544 AVCODEC_SYNC_TRACE;
545 state_ = State::STOPPING;
546 ReleaseResource();
547 state_ = State::UNINITIALIZED;
548 AVCODEC_LOGI("Release codec successful, state: Uninitialized");
549 return AVCS_ERR_OK;
550 }
551
SetSurfaceParameter(const Format & format,const std::string_view & formatKey,FormatDataType formatType)552 void HevcDecoder::SetSurfaceParameter(const Format &format, const std::string_view &formatKey,
553 FormatDataType formatType)
554 {
555 CHECK_AND_RETURN_LOG(formatType == FORMAT_TYPE_INT32, "Set parameter failed: type should be int32");
556 int32_t val = 0;
557 CHECK_AND_RETURN_LOG(format.GetIntValue(formatKey, val), "Set parameter failed: get value fail");
558 if (formatKey == MediaDescriptionKey::MD_KEY_PIXEL_FORMAT) {
559 VideoPixelFormat vpf = static_cast<VideoPixelFormat>(val);
560 CHECK_AND_RETURN_LOG(vpf == VideoPixelFormat::NV12 || vpf == VideoPixelFormat::NV21,
561 "Set parameter failed: pixel format value %{public}d invalid", val);
562 outputPixelFmt_ = vpf;
563 format_.PutIntValue(MediaDescriptionKey::MD_KEY_PIXEL_FORMAT, val);
564 GraphicPixelFormat surfacePixelFmt;
565 if (bitDepth_ == BIT_DEPTH10BIT) {
566 if (vpf == VideoPixelFormat::NV12) {
567 surfacePixelFmt = GraphicPixelFormat::GRAPHIC_PIXEL_FMT_YCBCR_P010;
568 } else {
569 surfacePixelFmt = GraphicPixelFormat::GRAPHIC_PIXEL_FMT_YCRCB_P010;
570 }
571 } else {
572 surfacePixelFmt = TranslateSurfaceFormat(vpf);
573 }
574 std::lock_guard<std::mutex> sLock(surfaceMutex_);
575 sInfo_.requestConfig.format = surfacePixelFmt;
576 } else if (formatKey == MediaDescriptionKey::MD_KEY_ROTATION_ANGLE) {
577 VideoRotation sr = static_cast<VideoRotation>(val);
578 CHECK_AND_RETURN_LOG(sr == VideoRotation::VIDEO_ROTATION_0 || sr == VideoRotation::VIDEO_ROTATION_90 ||
579 sr == VideoRotation::VIDEO_ROTATION_180 || sr == VideoRotation::VIDEO_ROTATION_270,
580 "Set parameter failed: rotation angle value %{public}d invalid", val);
581 format_.PutIntValue(MediaDescriptionKey::MD_KEY_ROTATION_ANGLE, val);
582 std::lock_guard<std::mutex> sLock(surfaceMutex_);
583 sInfo_.surface->SetTransform(TranslateSurfaceRotation(sr));
584 } else if (formatKey == MediaDescriptionKey::MD_KEY_SCALE_TYPE) {
585 ScalingMode scaleMode = static_cast<ScalingMode>(val);
586 CHECK_AND_RETURN_LOG(scaleMode == ScalingMode::SCALING_MODE_SCALE_TO_WINDOW ||
587 scaleMode == ScalingMode::SCALING_MODE_SCALE_CROP,
588 "Set parameter failed: scale type value %{public}d invalid", val);
589 format_.PutIntValue(MediaDescriptionKey::MD_KEY_SCALE_TYPE, val);
590 std::lock_guard<std::mutex> sLock(surfaceMutex_);
591 sInfo_.scalingMode = scaleMode;
592 } else {
593 AVCODEC_LOGW("Set parameter failed: %{public}s", formatKey.data());
594 return;
595 }
596 AVCODEC_LOGI("Set parameter %{public}s success, val %{publid}d", formatKey.data(), val);
597 }
598
SetParameter(const Format & format)599 int32_t HevcDecoder::SetParameter(const Format &format)
600 {
601 AVCODEC_SYNC_TRACE;
602 for (auto &it : format.GetFormatMap()) {
603 if (sInfo_.surface != nullptr && it.second.type == FORMAT_TYPE_INT32) {
604 if (it.first == MediaDescriptionKey::MD_KEY_PIXEL_FORMAT ||
605 it.first == MediaDescriptionKey::MD_KEY_ROTATION_ANGLE ||
606 it.first == MediaDescriptionKey::MD_KEY_SCALE_TYPE) {
607 SetSurfaceParameter(format, it.first, it.second.type);
608 }
609 } else {
610 AVCODEC_LOGW("Current Version, %{public}s is not supported", it.first.data());
611 }
612 }
613 AVCODEC_LOGI("Set parameter successful");
614 return AVCS_ERR_OK;
615 }
616
GetOutputFormat(Format & format)617 int32_t HevcDecoder::GetOutputFormat(Format &format)
618 {
619 AVCODEC_SYNC_TRACE;
620 if ((!format_.ContainKey(MediaDescriptionKey::MD_KEY_MAX_INPUT_SIZE)) &&
621 hevcDecoderOutpusArgs_.uiDecStride != 0) {
622 int32_t stride = static_cast<int32_t>(hevcDecoderOutpusArgs_.uiDecStride);
623 int32_t maxInputSize = static_cast<int32_t>(static_cast<UINT32>(stride * height_ * VIDEO_PIX_DEPTH_YUV) >> 1);
624 format_.PutIntValue(MediaDescriptionKey::MD_KEY_MAX_INPUT_SIZE, maxInputSize);
625 }
626
627 if (!format_.ContainKey(OHOS::Media::Tag::VIDEO_SLICE_HEIGHT)) {
628 format_.PutIntValue(OHOS::Media::Tag::VIDEO_SLICE_HEIGHT, height_);
629 }
630 if (!format_.ContainKey(OHOS::Media::Tag::VIDEO_PIC_WIDTH) ||
631 !format_.ContainKey(OHOS::Media::Tag::VIDEO_PIC_HEIGHT)) {
632 format_.PutIntValue(OHOS::Media::Tag::VIDEO_PIC_WIDTH, width_);
633 format_.PutIntValue(OHOS::Media::Tag::VIDEO_PIC_HEIGHT, height_);
634 }
635
636 if (!format_.ContainKey(OHOS::Media::Tag::VIDEO_CROP_RIGHT) ||
637 !format_.ContainKey(OHOS::Media::Tag::VIDEO_CROP_BOTTOM)) {
638 format_.PutIntValue(OHOS::Media::Tag::VIDEO_CROP_RIGHT, width_-1);
639 format_.PutIntValue(OHOS::Media::Tag::VIDEO_CROP_BOTTOM, height_-1);
640 format_.PutIntValue(OHOS::Media::Tag::VIDEO_CROP_LEFT, 0);
641 format_.PutIntValue(OHOS::Media::Tag::VIDEO_CROP_TOP, 0);
642 }
643
644 format = format_;
645 AVCODEC_LOGI("Get outputFormat successful");
646 return AVCS_ERR_OK;
647 }
648
CalculateBufferSize()649 void HevcDecoder::CalculateBufferSize()
650 {
651 if ((static_cast<UINT32>(width_ * height_ * VIDEO_PIX_DEPTH_YUV) >> 1) <= VIDEO_MIN_BUFFER_SIZE) {
652 inputBufferSize_ = VIDEO_MIN_BUFFER_SIZE;
653 } else {
654 inputBufferSize_ = VIDEO_MAX_BUFFER_SIZE;
655 }
656 AVCODEC_LOGI("width = %{public}d, height = %{public}d, Input buffer size = %{public}d",
657 width_, height_, inputBufferSize_);
658 }
659
AllocateInputBuffer(int32_t bufferCnt,int32_t inBufferSize)660 int32_t HevcDecoder::AllocateInputBuffer(int32_t bufferCnt, int32_t inBufferSize)
661 {
662 int32_t valBufferCnt = 0;
663 for (int32_t i = 0; i < bufferCnt; i++) {
664 std::shared_ptr<HBuffer> buf = std::make_shared<HBuffer>();
665 std::shared_ptr<AVAllocator> allocator =
666 AVAllocatorFactory::CreateSharedAllocator(MemoryFlag::MEMORY_READ_WRITE);
667 CHECK_AND_CONTINUE_LOG(allocator != nullptr, "input buffer %{public}d allocator is nullptr", i);
668 buf->avBuffer = AVBuffer::CreateAVBuffer(allocator, inBufferSize);
669 CHECK_AND_CONTINUE_LOG(buf->avBuffer != nullptr, "Allocate input buffer failed, index=%{public}d", i);
670 AVCODEC_LOGI("Allocate input buffer success: index=%{public}d, size=%{public}d", i,
671 buf->avBuffer->memory_->GetCapacity());
672
673 buffers_[INDEX_INPUT].emplace_back(buf);
674 valBufferCnt++;
675 }
676 if (valBufferCnt < DEFAULT_MIN_BUFFER_CNT) {
677 AVCODEC_LOGE("Allocate input buffer failed: only %{public}d buffer is allocated, no memory", valBufferCnt);
678 buffers_[INDEX_INPUT].clear();
679 return AVCS_ERR_NO_MEMORY;
680 }
681 return AVCS_ERR_OK;
682 }
683
SetSurfaceCfg(int32_t bufferCnt)684 int32_t HevcDecoder::SetSurfaceCfg(int32_t bufferCnt)
685 {
686 if (outputPixelFmt_ == VideoPixelFormat::UNKNOWN) {
687 format_.PutIntValue(MediaDescriptionKey::MD_KEY_PIXEL_FORMAT, static_cast<int32_t>(VideoPixelFormat::NV12));
688 }
689 int32_t val32 = 0;
690 format_.GetIntValue(MediaDescriptionKey::MD_KEY_PIXEL_FORMAT, val32);
691 GraphicPixelFormat surfacePixelFmt = TranslateSurfaceFormat(static_cast<VideoPixelFormat>(val32));
692 CHECK_AND_RETURN_RET_LOG(surfacePixelFmt != GraphicPixelFormat::GRAPHIC_PIXEL_FMT_BUTT, AVCS_ERR_UNSUPPORT,
693 "Failed to allocate output buffer: unsupported surface format");
694 sInfo_.requestConfig.width = width_;
695 sInfo_.requestConfig.height = height_;
696 sInfo_.requestConfig.format = surfacePixelFmt;
697 if (sInfo_.surface != nullptr) {
698 CHECK_AND_RETURN_RET_LOG(sInfo_.surface->SetQueueSize(bufferCnt) == OHOS::SurfaceError::SURFACE_ERROR_OK,
699 AVCS_ERR_NO_MEMORY, "Surface set QueueSize=%{public}d failed", bufferCnt);
700
701 format_.GetIntValue(MediaDescriptionKey::MD_KEY_SCALE_TYPE, val32);
702 sInfo_.scalingMode = static_cast<ScalingMode>(val32);
703 format_.GetIntValue(MediaDescriptionKey::MD_KEY_ROTATION_ANGLE, val32);
704 sInfo_.surface->SetTransform(TranslateSurfaceRotation(static_cast<VideoRotation>(val32)));
705 }
706 return AVCS_ERR_OK;
707 }
708
AllocateOutputBuffer(int32_t bufferCnt)709 int32_t HevcDecoder::AllocateOutputBuffer(int32_t bufferCnt)
710 {
711 int32_t valBufferCnt = 0;
712 CHECK_AND_RETURN_RET_LOG(SetSurfaceCfg(bufferCnt) == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "SetSurfaceCfg failed");
713
714 if (sInfo_.surface != nullptr) {
715 sInfo_.surface->CleanCache();
716 }
717 for (int i = 0; i < bufferCnt; i++) {
718 std::shared_ptr<HBuffer> buf = std::make_shared<HBuffer>();
719 if (sInfo_.surface == nullptr) {
720 std::shared_ptr<AVAllocator> allocator =
721 AVAllocatorFactory::CreateSurfaceAllocator(sInfo_.requestConfig);
722 CHECK_AND_CONTINUE_LOG(allocator != nullptr, "output buffer %{public}d allocator is nullptr", i);
723 buf->avBuffer = AVBuffer::CreateAVBuffer(allocator, 0);
724 if (buf->avBuffer != nullptr) {
725 AVCODEC_LOGI("Allocate output share buffer success: index=%{public}d, size=%{public}d", i,
726 buf->avBuffer->memory_->GetCapacity());
727 }
728 } else {
729 buf->sMemory = std::make_shared<FSurfaceMemory>(&sInfo_);
730 CHECK_AND_CONTINUE_LOG(buf->sMemory->GetSurfaceBuffer() != nullptr,
731 "output surface memory %{public}d create fail", i);
732 outAVBuffer4Surface_.emplace_back(AVBuffer::CreateAVBuffer());
733 buf->avBuffer = AVBuffer::CreateAVBuffer(buf->sMemory->GetBase(), buf->sMemory->GetSize());
734 AVCODEC_LOGI("Allocate output surface buffer success: index=%{public}d, size=%{public}d, "
735 "stride=%{public}d",
736 i, buf->sMemory->GetSize(),
737 buf->sMemory->GetSurfaceBufferStride());
738 }
739 CHECK_AND_CONTINUE_LOG(buf->avBuffer != nullptr, "Allocate output buffer failed, index=%{public}d", i);
740
741 buf->width = width_;
742 buf->height = height_;
743 buffers_[INDEX_OUTPUT].emplace_back(buf);
744 valBufferCnt++;
745 }
746 if (valBufferCnt < DEFAULT_MIN_BUFFER_CNT) {
747 AVCODEC_LOGE("Allocate output buffer failed: only %{public}d buffer is allocated, no memory", valBufferCnt);
748 buffers_[INDEX_INPUT].clear();
749 buffers_[INDEX_OUTPUT].clear();
750 return AVCS_ERR_NO_MEMORY;
751 }
752 return AVCS_ERR_OK;
753 }
754
AllocateBuffers()755 int32_t HevcDecoder::AllocateBuffers()
756 {
757 AVCODEC_SYNC_TRACE;
758 CalculateBufferSize();
759 CHECK_AND_RETURN_RET_LOG(inputBufferSize_ > 0, AVCS_ERR_INVALID_VAL,
760 "Allocate buffer with input size=%{public}d failed", inputBufferSize_);
761 if (sInfo_.surface != nullptr && isOutBufSetted_ == false) {
762 format_.PutIntValue(MediaDescriptionKey::MD_KEY_MAX_OUTPUT_BUFFER_COUNT, DEFAULT_OUT_SURFACE_CNT);
763 }
764 int32_t inputBufferCnt = 0;
765 int32_t outputBufferCnt = 0;
766 format_.GetIntValue(MediaDescriptionKey::MD_KEY_MAX_INPUT_BUFFER_COUNT, inputBufferCnt);
767 format_.GetIntValue(MediaDescriptionKey::MD_KEY_MAX_OUTPUT_BUFFER_COUNT, outputBufferCnt);
768 inputAvailQue_ = std::make_shared<BlockQueue<uint32_t>>("inputAvailQue", inputBufferCnt);
769 codecAvailQue_ = std::make_shared<BlockQueue<uint32_t>>("codecAvailQue", outputBufferCnt);
770 if (sInfo_.surface != nullptr) {
771 renderAvailQue_ = std::make_shared<BlockQueue<uint32_t>>("renderAvailQue", outputBufferCnt);
772 }
773 if (AllocateInputBuffer(inputBufferCnt, inputBufferSize_) == AVCS_ERR_NO_MEMORY ||
774 AllocateOutputBuffer(outputBufferCnt) == AVCS_ERR_NO_MEMORY) {
775 return AVCS_ERR_NO_MEMORY;
776 }
777 AVCODEC_LOGI("Allocate buffers successful");
778 return AVCS_ERR_OK;
779 }
780
UpdateOutputBuffer(uint32_t index)781 int32_t HevcDecoder::UpdateOutputBuffer(uint32_t index)
782 {
783 std::shared_ptr<HBuffer> outputBuffer = buffers_[INDEX_OUTPUT][index];
784 if (width_ != outputBuffer->width || height_ != outputBuffer->height || bitDepth_ != outputBuffer->bitDepth) {
785 std::shared_ptr<AVAllocator> allocator =
786 AVAllocatorFactory::CreateSurfaceAllocator(sInfo_.requestConfig);
787 CHECK_AND_RETURN_RET_LOG(allocator != nullptr, AVCS_ERR_NO_MEMORY, "buffer %{public}d allocator is nullptr",
788 index);
789 outputBuffer->avBuffer = AVBuffer::CreateAVBuffer(allocator, 0);
790 CHECK_AND_RETURN_RET_LOG(outputBuffer->avBuffer != nullptr, AVCS_ERR_NO_MEMORY,
791 "Buffer allocate failed, index=%{public}d", index);
792 AVCODEC_LOGI("update output buffer success: index=%{public}d, size=%{public}d", index,
793 outputBuffer->avBuffer->memory_->GetCapacity());
794
795 outputBuffer->owner_ = HBuffer::Owner::OWNED_BY_CODEC;
796 outputBuffer->width = width_;
797 outputBuffer->height = height_;
798 outputBuffer->bitDepth = bitDepth_;
799 }
800 return AVCS_ERR_OK;
801 }
802
UpdateSurfaceMemory(uint32_t index)803 int32_t HevcDecoder::UpdateSurfaceMemory(uint32_t index)
804 {
805 AVCODEC_SYNC_TRACE;
806 std::unique_lock<std::mutex> oLock(outputMutex_);
807 std::shared_ptr<HBuffer> outputBuffer = buffers_[INDEX_OUTPUT][index];
808 oLock.unlock();
809 if (width_ != outputBuffer->width || height_ != outputBuffer->height || bitDepth_ != outputBuffer->bitDepth) {
810 std::shared_ptr<FSurfaceMemory> surfaceMemory = outputBuffer->sMemory;
811 surfaceMemory->SetNeedRender(false);
812 surfaceMemory->ReleaseSurfaceBuffer();
813 while (state_ == State::RUNNING) {
814 std::unique_lock<std::mutex> sLock(surfaceMutex_);
815 sptr<SurfaceBuffer> surfaceBuffer = surfaceMemory->GetSurfaceBuffer();
816 sLock.unlock();
817 if (surfaceBuffer != nullptr) {
818 break;
819 }
820 std::this_thread::sleep_for(std::chrono::milliseconds(DEFAULT_TRY_REQ_TIME));
821 }
822 outputBuffer->avBuffer =
823 AVBuffer::CreateAVBuffer(outputBuffer->sMemory->GetBase(), outputBuffer->sMemory->GetSize());
824 outputBuffer->width = width_;
825 outputBuffer->height = height_;
826 outputBuffer->bitDepth = bitDepth_;
827 }
828
829 return AVCS_ERR_OK;
830 }
831
CheckFormatChange(uint32_t index,int width,int height,int bitDepth)832 int32_t HevcDecoder::CheckFormatChange(uint32_t index, int width, int height, int bitDepth)
833 {
834 bool formatChanged = false;
835 if (width_ != width || height_ != height || bitDepth_ != bitDepth) {
836 AVCODEC_LOGI("format change, width: %{public}d->%{public}d, height: %{public}d->%{public}d, "
837 "bitDepth: %{public}d->%{public}d", width_, width, height_, height, bitDepth_, bitDepth);
838 width_ = width;
839 height_ = height;
840 bitDepth_ = bitDepth;
841 ResetData();
842 scale_ = nullptr;
843 std::unique_lock<std::mutex> sLock(surfaceMutex_);
844 sInfo_.requestConfig.width = width_;
845 sInfo_.requestConfig.height = height_;
846 if (bitDepth_ == BIT_DEPTH10BIT) {
847 if (outputPixelFmt_ == VideoPixelFormat::NV12 || outputPixelFmt_ == VideoPixelFormat::UNKNOWN) {
848 sInfo_.requestConfig.format = GraphicPixelFormat::GRAPHIC_PIXEL_FMT_YCBCR_P010;
849 } else {
850 sInfo_.requestConfig.format = GraphicPixelFormat::GRAPHIC_PIXEL_FMT_YCRCB_P010;
851 }
852 }
853 sLock.unlock();
854 formatChanged = true;
855 }
856 if (sInfo_.surface == nullptr) {
857 std::lock_guard<std::mutex> oLock(outputMutex_);
858 CHECK_AND_RETURN_RET_LOG((UpdateOutputBuffer(index) == AVCS_ERR_OK), AVCS_ERR_NO_MEMORY,
859 "Update output buffer failed, index=%{public}u", index);
860 } else {
861 CHECK_AND_RETURN_RET_LOG((UpdateSurfaceMemory(index) == AVCS_ERR_OK), AVCS_ERR_NO_MEMORY,
862 "Update buffer failed");
863 }
864 if (!format_.ContainKey(OHOS::Media::Tag::VIDEO_STRIDE) || formatChanged) {
865 int32_t stride = GetSurfaceBufferStride(buffers_[INDEX_OUTPUT][index]);
866 CHECK_AND_RETURN_RET_LOG(stride > 0, AVCS_ERR_NO_MEMORY, "get GetSurfaceBufferStride failed");
867 format_.PutIntValue(OHOS::Media::Tag::VIDEO_STRIDE, stride);
868 format_.PutIntValue(MediaDescriptionKey::MD_KEY_WIDTH, width_);
869 format_.PutIntValue(MediaDescriptionKey::MD_KEY_HEIGHT, height_);
870 format_.PutIntValue(OHOS::Media::Tag::VIDEO_SLICE_HEIGHT, height_);
871 format_.PutIntValue(OHOS::Media::Tag::VIDEO_PIC_WIDTH, width_);
872 format_.PutIntValue(OHOS::Media::Tag::VIDEO_PIC_HEIGHT, height_);
873 format_.PutIntValue(OHOS::Media::Tag::VIDEO_CROP_RIGHT, width_-1);
874 format_.PutIntValue(OHOS::Media::Tag::VIDEO_CROP_BOTTOM, height_-1);
875 format_.PutIntValue(OHOS::Media::Tag::VIDEO_CROP_LEFT, 0);
876 format_.PutIntValue(OHOS::Media::Tag::VIDEO_CROP_TOP, 0);
877 callback_->OnOutputFormatChanged(format_);
878 }
879 return AVCS_ERR_OK;
880 }
881
GetSurfaceBufferStride(const std::shared_ptr<HBuffer> & frameBuffer)882 int32_t HevcDecoder::GetSurfaceBufferStride(const std::shared_ptr<HBuffer> &frameBuffer)
883 {
884 int32_t surfaceBufferStride = 0;
885 if (sInfo_.surface == nullptr) {
886 auto surfaceBuffer = frameBuffer->avBuffer->memory_->GetSurfaceBuffer();
887 CHECK_AND_RETURN_RET_LOG(surfaceBuffer != nullptr, -1, "surfaceBuffer is nullptr");
888 auto bufferHandle = surfaceBuffer->GetBufferHandle();
889 CHECK_AND_RETURN_RET_LOG(bufferHandle != nullptr, -1, "fail to get bufferHandle");
890 surfaceBufferStride = bufferHandle->stride;
891 } else {
892 surfaceBufferStride = frameBuffer->sMemory->GetSurfaceBufferStride();
893 }
894 return surfaceBufferStride;
895 }
896
ReleaseBuffers()897 void HevcDecoder::ReleaseBuffers()
898 {
899 ResetData();
900 if (!isBufferAllocated_) {
901 return;
902 }
903
904 inputAvailQue_->Clear();
905 buffers_[INDEX_INPUT].clear();
906
907 std::unique_lock<std::mutex> oLock(outputMutex_);
908 codecAvailQue_->Clear();
909 if (sInfo_.surface != nullptr) {
910 renderAvailQue_->Clear();
911 renderSurfaceBufferMap_.clear();
912 for (uint32_t i = 0; i < buffers_[INDEX_OUTPUT].size(); i++) {
913 std::shared_ptr<HBuffer> outputBuffer = buffers_[INDEX_OUTPUT][i];
914 if (outputBuffer->owner_ == HBuffer::Owner::OWNED_BY_CODEC) {
915 std::shared_ptr<FSurfaceMemory> surfaceMemory = outputBuffer->sMemory;
916 surfaceMemory->SetNeedRender(false);
917 surfaceMemory->ReleaseSurfaceBuffer();
918 outputBuffer->owner_ = HBuffer::Owner::OWNED_BY_SURFACE;
919 }
920 }
921 sInfo_.surface->CleanCache();
922 AVCODEC_LOGI("surface cleancache success");
923 }
924 buffers_[INDEX_OUTPUT].clear();
925 oLock.unlock();
926 isBufferAllocated_ = false;
927 }
928
QueueInputBuffer(uint32_t index)929 int32_t HevcDecoder::QueueInputBuffer(uint32_t index)
930 {
931 AVCODEC_SYNC_TRACE;
932 CHECK_AND_RETURN_RET_LOG(state_ == State::RUNNING, AVCS_ERR_INVALID_STATE,
933 "Queue input buffer failed: not in Running state");
934 CHECK_AND_RETURN_RET_LOG(index < buffers_[INDEX_INPUT].size(), AVCS_ERR_INVALID_VAL,
935 "Queue input buffer failed with bad index, index=%{public}u, buffer_size=%{public}zu",
936 index, buffers_[INDEX_INPUT].size());
937 std::shared_ptr<HBuffer> inputBuffer = buffers_[INDEX_INPUT][index];
938 CHECK_AND_RETURN_RET_LOG(inputBuffer->owner_ == HBuffer::Owner::OWNED_BY_USER, AVCS_ERR_INVALID_OPERATION,
939 "Queue input buffer failed: buffer with index=%{public}u is not available", index);
940
941 inputBuffer->owner_ = HBuffer::Owner::OWNED_BY_CODEC;
942 inputAvailQue_->Push(index);
943 return AVCS_ERR_OK;
944 }
945
SendFrame()946 void HevcDecoder::SendFrame()
947 {
948 if (state_ == State::STOPPING || state_ == State::FLUSHING) {
949 return;
950 } else if (state_ != State::RUNNING || isSendEos_) {
951 std::this_thread::sleep_for(std::chrono::milliseconds(DEFAULT_TRY_DECODE_TIME));
952 return;
953 }
954 uint32_t index = inputAvailQue_->Front();
955 CHECK_AND_RETURN_LOG(state_ == State::RUNNING, "Not in running state");
956 std::shared_ptr<HBuffer> &inputBuffer = buffers_[INDEX_INPUT][index];
957 std::shared_ptr<AVBuffer> &inputAVBuffer = inputBuffer->avBuffer;
958 if (inputAVBuffer->flag_ & AVCODEC_BUFFER_FLAG_EOS) {
959 hevcDecoderInputArgs_.pStream = nullptr;
960 isSendEos_ = true;
961 AVCODEC_LOGI("Send eos end");
962 } else {
963 hevcDecoderInputArgs_.pStream = inputAVBuffer->memory_->GetAddr();
964 hevcDecoderInputArgs_.uiStreamLen = static_cast<UINT32>(inputAVBuffer->memory_->GetSize());
965 hevcDecoderInputArgs_.uiTimeStamp = static_cast<UINT64>(inputAVBuffer->pts_);
966 }
967
968 #ifdef BUILD_ENG_VERSION
969 if (dumpInFile_ && dumpInFile_->is_open() && !isSendEos_) {
970 dumpInFile_->write(reinterpret_cast<char*>(inputAVBuffer->memory_->GetAddr()),
971 static_cast<int32_t>(inputAVBuffer->memory_->GetSize()));
972 }
973 #endif
974
975 int32_t ret = 0;
976 std::unique_lock<std::mutex> runLock(decRunMutex_);
977 do {
978 ret = DecodeFrameOnce();
979 } while (ret == 0 && isSendEos_);
980 runLock.unlock();
981
982 if (isSendEos_) {
983 auto outIndex = codecAvailQue_->Front();
984 std::shared_ptr<HBuffer> frameBuffer = buffers_[INDEX_OUTPUT][outIndex];
985 frameBuffer->avBuffer->flag_ = AVCODEC_BUFFER_FLAG_EOS;
986 FramePostProcess(buffers_[INDEX_OUTPUT][outIndex], outIndex, AVCS_ERR_OK, AVCS_ERR_OK);
987 state_ = State::EOS;
988 } else if (ret < 0) {
989 AVCODEC_LOGE("decode frame error: ret = %{public}d", ret);
990 }
991
992 inputAvailQue_->Pop();
993 inputBuffer->owner_ = HBuffer::Owner::OWNED_BY_USER;
994 callback_->OnInputBufferAvailable(index, inputAVBuffer);
995 }
996
DecodeFrameOnce()997 int32_t HevcDecoder::DecodeFrameOnce()
998 {
999 int32_t ret = 0;
1000 if (hevcSDecoder_ != nullptr && hevcDecoderFlushFrameFunc_ != nullptr &&
1001 hevcDecoderDecodecFrameFunc_ != nullptr) {
1002 if (isSendEos_) {
1003 ret = hevcDecoderFlushFrameFunc_(hevcSDecoder_, &hevcDecoderOutpusArgs_);
1004 } else {
1005 ret = hevcDecoderDecodecFrameFunc_(hevcSDecoder_, &hevcDecoderInputArgs_, &hevcDecoderOutpusArgs_);
1006 }
1007 } else {
1008 AVCODEC_LOGW("hevcDecoderDecodecFrameFunc_ = nullptr || hevcSDecoder_ = nullptr || "
1009 "hevcDecoderFlushFrameFunc_ = nullptr, cannot call decoder");
1010 ret = -1;
1011 }
1012 int32_t bitDepth = static_cast<int32_t>(hevcDecoderOutpusArgs_.uiDecBitDepth);
1013 if (ret == 0) {
1014 CHECK_AND_RETURN_RET_LOG(bitDepth == BIT_DEPTH8BIT || bitDepth == BIT_DEPTH10BIT, -1,
1015 "Unsupported bitDepth %{public}d", bitDepth);
1016 ConvertDecOutToAVFrame(bitDepth);
1017 #ifdef BUILD_ENG_VERSION
1018 DumpOutputBuffer(bitDepth);
1019 #endif
1020 auto index = codecAvailQue_->Front();
1021 CHECK_AND_RETURN_RET_LOG(state_ == State::RUNNING, -1, "Not in running state");
1022 std::shared_ptr<HBuffer> frameBuffer = buffers_[INDEX_OUTPUT][index];
1023 int32_t status = AVCS_ERR_OK;
1024 if (CheckFormatChange(index, cachedFrame_->width, cachedFrame_->height, bitDepth) == AVCS_ERR_OK) {
1025 CHECK_AND_RETURN_RET_LOG(state_ == State::RUNNING, -1, "Not in running state");
1026 frameBuffer = buffers_[INDEX_OUTPUT][index];
1027 status = FillFrameBuffer(frameBuffer);
1028 } else {
1029 CHECK_AND_RETURN_RET_LOG(state_ == State::RUNNING, -1, "Not in running state");
1030 callback_->OnError(AVCODEC_ERROR_EXTEND_START, AVCS_ERR_NO_MEMORY);
1031 return -1;
1032 }
1033 frameBuffer->avBuffer->flag_ = AVCODEC_BUFFER_FLAG_NONE;
1034 FramePostProcess(frameBuffer, index, status, AVCS_ERR_OK);
1035 }
1036 return ret;
1037 }
1038
FillFrameBuffer(const std::shared_ptr<HBuffer> & frameBuffer)1039 int32_t HevcDecoder::FillFrameBuffer(const std::shared_ptr<HBuffer> &frameBuffer)
1040 {
1041 VideoPixelFormat targetPixelFmt = outputPixelFmt_;
1042 if (outputPixelFmt_ == VideoPixelFormat::UNKNOWN) {
1043 targetPixelFmt = VideoPixelFormat::NV12;
1044 }
1045 AVPixelFormat ffmpegFormat;
1046 if (bitDepth_ == BIT_DEPTH10BIT) {
1047 ffmpegFormat = AVPixelFormat::AV_PIX_FMT_P010LE;
1048 } else {
1049 ffmpegFormat = ConvertPixelFormatToFFmpeg(targetPixelFmt);
1050 }
1051 // yuv420 -> nv12 or nv21
1052 int32_t ret = ConvertVideoFrame(&scale_, cachedFrame_, scaleData_, scaleLineSize_, ffmpegFormat);
1053 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, ret, "Scale video frame failed: %{public}d", ret);
1054 isConverted_ = true;
1055
1056 format_.PutIntValue(MediaDescriptionKey::MD_KEY_PIXEL_FORMAT, static_cast<int32_t>(targetPixelFmt));
1057 std::shared_ptr<AVMemory> &bufferMemory = frameBuffer->avBuffer->memory_;
1058 CHECK_AND_RETURN_RET_LOG(bufferMemory != nullptr, AVCS_ERR_INVALID_VAL, "bufferMemory is nullptr");
1059 bufferMemory->SetSize(0);
1060 struct SurfaceInfo surfaceInfo;
1061 surfaceInfo.scaleData = scaleData_;
1062 surfaceInfo.scaleLineSize = scaleLineSize_;
1063 int32_t surfaceStride = GetSurfaceBufferStride(frameBuffer);
1064 CHECK_AND_RETURN_RET_LOG(surfaceStride > 0, AVCS_ERR_INVALID_VAL, "get GetSurfaceBufferStride failed");
1065 surfaceInfo.surfaceStride = static_cast<uint32_t>(surfaceStride);
1066 if (sInfo_.surface) {
1067 surfaceInfo.surfaceFence = frameBuffer->sMemory->GetFence();
1068 ret = WriteSurfaceData(bufferMemory, surfaceInfo, format_);
1069 } else {
1070 Format bufferFormat;
1071 bufferFormat.PutIntValue(MediaDescriptionKey::MD_KEY_HEIGHT, height_);
1072 bufferFormat.PutIntValue(MediaDescriptionKey::MD_KEY_WIDTH, surfaceStride);
1073 bufferFormat.PutIntValue(MediaDescriptionKey::MD_KEY_PIXEL_FORMAT, static_cast<int32_t>(targetPixelFmt));
1074 ret = WriteBufferData(bufferMemory, scaleData_, scaleLineSize_, bufferFormat);
1075 }
1076 #ifdef BUILD_ENG_VERSION
1077 DumpConvertOut(surfaceInfo);
1078 #endif
1079 frameBuffer->avBuffer->pts_ = cachedFrame_->pts;
1080 AVCODEC_LOGD("Fill frame buffer successful");
1081 return ret;
1082 }
1083
FramePostProcess(std::shared_ptr<HBuffer> & frameBuffer,uint32_t index,int32_t status,int ret)1084 void HevcDecoder::FramePostProcess(std::shared_ptr<HBuffer> &frameBuffer, uint32_t index, int32_t status, int ret)
1085 {
1086 if (status == AVCS_ERR_OK) {
1087 codecAvailQue_->Pop();
1088 frameBuffer->owner_ = HBuffer::Owner::OWNED_BY_USER;
1089 if (sInfo_.surface) {
1090 outAVBuffer4Surface_[index]->pts_ = frameBuffer->avBuffer->pts_;
1091 outAVBuffer4Surface_[index]->flag_ = frameBuffer->avBuffer->flag_;
1092 }
1093 callback_->OnOutputBufferAvailable(index, sInfo_.surface ?
1094 outAVBuffer4Surface_[index] : frameBuffer->avBuffer);
1095 } else if (status == AVCS_ERR_UNSUPPORT) {
1096 AVCODEC_LOGE("Recevie frame from codec failed: OnError");
1097 callback_->OnError(AVCodecErrorType::AVCODEC_ERROR_INTERNAL, AVCodecServiceErrCode::AVCS_ERR_UNSUPPORT);
1098 state_ = State::ERROR;
1099 } else {
1100 AVCODEC_LOGE("Recevie frame from codec failed");
1101 callback_->OnError(AVCodecErrorType::AVCODEC_ERROR_INTERNAL, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN);
1102 state_ = State::ERROR;
1103 }
1104 }
1105
ConvertDecOutToAVFrame(int32_t bitDepth)1106 void HevcDecoder::ConvertDecOutToAVFrame(int32_t bitDepth)
1107 {
1108 if (cachedFrame_ == nullptr) {
1109 cachedFrame_ = std::shared_ptr<AVFrame>(av_frame_alloc(), [](AVFrame *p) { av_frame_free(&p); });
1110 }
1111 cachedFrame_->data[0] = hevcDecoderOutpusArgs_.pucOutYUV[0];
1112 cachedFrame_->data[1] = hevcDecoderOutpusArgs_.pucOutYUV[1]; // 1 u channel
1113 cachedFrame_->data[2] = hevcDecoderOutpusArgs_.pucOutYUV[2]; // 2 v channel
1114 if (bitDepth == BIT_DEPTH8BIT) {
1115 cachedFrame_->format = static_cast<int>(AVPixelFormat::AV_PIX_FMT_YUV420P);
1116 cachedFrame_->linesize[0] = static_cast<int32_t>(hevcDecoderOutpusArgs_.uiDecStride);
1117 cachedFrame_->linesize[1] = static_cast<int32_t>(hevcDecoderOutpusArgs_.uiDecStride >> 1); // 1 u channel
1118 cachedFrame_->linesize[2] = static_cast<int32_t>(hevcDecoderOutpusArgs_.uiDecStride >> 1); // 2 v channel
1119 } else {
1120 cachedFrame_->format = static_cast<int>(AVPixelFormat::AV_PIX_FMT_YUV420P10LE);
1121 cachedFrame_->linesize[0] =
1122 static_cast<int32_t>(hevcDecoderOutpusArgs_.uiDecStride * 2); // 2 10bit per pixel 2bytes
1123 cachedFrame_->linesize[1] = static_cast<int32_t>(hevcDecoderOutpusArgs_.uiDecStride); // 1 u channel
1124 cachedFrame_->linesize[2] = static_cast<int32_t>(hevcDecoderOutpusArgs_.uiDecStride); // 2 v channel
1125 if (outputPixelFmt_ == VideoPixelFormat::NV21) { // exchange uv channel
1126 cachedFrame_->data[1] = hevcDecoderOutpusArgs_.pucOutYUV[2]; // 2 u -> v
1127 cachedFrame_->data[2] = hevcDecoderOutpusArgs_.pucOutYUV[1]; // 2 v -> u
1128 }
1129 }
1130 cachedFrame_->width = static_cast<int32_t>(hevcDecoderOutpusArgs_.uiDecWidth);
1131 cachedFrame_->height = static_cast<int32_t>(hevcDecoderOutpusArgs_.uiDecHeight);
1132 cachedFrame_->pts = static_cast<int64_t>(hevcDecoderOutpusArgs_.uiTimeStamp);
1133 }
1134
1135 #ifdef BUILD_ENG_VERSION
DumpOutputBuffer(int32_t bitDepth)1136 void HevcDecoder::DumpOutputBuffer(int32_t bitDepth)
1137 {
1138 if (!dumpOutFile_ || !dumpOutFile_->is_open()) {
1139 return;
1140 }
1141 int32_t pixelBytes = 1;
1142 if (bitDepth == BIT_DEPTH10BIT) {
1143 pixelBytes = 2; // 2
1144 }
1145 for (int32_t i = 0; i < cachedFrame_->height; i++) {
1146 dumpOutFile_->write(reinterpret_cast<char *>(cachedFrame_->data[0] + i * cachedFrame_->linesize[0]),
1147 static_cast<int32_t>(cachedFrame_->width * pixelBytes));
1148 }
1149 for (int32_t i = 0; i < cachedFrame_->height / 2; i++) { // 2
1150 dumpOutFile_->write(reinterpret_cast<char *>(cachedFrame_->data[1] + i * cachedFrame_->linesize[1]),
1151 static_cast<int32_t>(cachedFrame_->width * pixelBytes / 2)); // 2
1152 }
1153 for (int32_t i = 0; i < cachedFrame_->height / 2; i++) { // 2
1154 dumpOutFile_->write(reinterpret_cast<char *>(cachedFrame_->data[2] + i * cachedFrame_->linesize[2]),
1155 static_cast<int32_t>(cachedFrame_->width * pixelBytes / 2)); // 2
1156 }
1157 }
1158
DumpConvertOut(struct SurfaceInfo & surfaceInfo)1159 void HevcDecoder::DumpConvertOut(struct SurfaceInfo &surfaceInfo)
1160 {
1161 if (!dumpConvertFile_ || !dumpConvertFile_->is_open()) {
1162 return;
1163 }
1164 if (surfaceInfo.scaleData[0] != nullptr) {
1165 int32_t srcPos = 0;
1166 int32_t dataSize = surfaceInfo.scaleLineSize[0];
1167 int32_t writeSize = dataSize > static_cast<int32_t>(surfaceInfo.surfaceStride) ?
1168 static_cast<int32_t>(surfaceInfo.surfaceStride) : dataSize;
1169 for (int32_t i = 0; i < height_; i++) {
1170 dumpConvertFile_->write(reinterpret_cast<char *>(surfaceInfo.scaleData[0] + srcPos), writeSize);
1171 srcPos += dataSize;
1172 }
1173 srcPos = 0;
1174 dataSize = surfaceInfo.scaleLineSize[1];
1175 writeSize = dataSize > static_cast<int32_t>(surfaceInfo.surfaceStride) ?
1176 static_cast<int32_t>(surfaceInfo.surfaceStride) : dataSize;
1177 for (int32_t i = 0; i < height_ / 2; i++) { // 2
1178 dumpConvertFile_->write(reinterpret_cast<char *>(surfaceInfo.scaleData[1] + srcPos), writeSize);
1179 srcPos += dataSize;
1180 }
1181 }
1182 }
1183 #endif
1184
FindAvailIndex(uint32_t index)1185 void HevcDecoder::FindAvailIndex(uint32_t index)
1186 {
1187 uint32_t curQueSize = renderAvailQue_->Size();
1188 for (uint32_t i = 0u; i < curQueSize; i++) {
1189 uint32_t num = renderAvailQue_->Pop();
1190 if (num == index) {
1191 break;
1192 } else {
1193 renderAvailQue_->Push(num);
1194 }
1195 }
1196 }
1197
RequestBufferFromConsumer()1198 void HevcDecoder::RequestBufferFromConsumer()
1199 {
1200 auto index = renderAvailQue_->Front();
1201 std::shared_ptr<HBuffer> outputBuffer = buffers_[INDEX_OUTPUT][index];
1202 std::shared_ptr<FSurfaceMemory> surfaceMemory = outputBuffer->sMemory;
1203 sptr<SurfaceBuffer> surfaceBuffer = surfaceMemory->GetSurfaceBuffer();
1204 if (surfaceBuffer == nullptr) {
1205 AVCODEC_LOGE("get buffer failed.");
1206 return;
1207 }
1208 auto queSize = renderAvailQue_->Size();
1209 uint32_t curIndex = 0;
1210 uint32_t i = 0;
1211 for (i = 0; i < queSize; i++) {
1212 curIndex = renderAvailQue_->Pop();
1213 if (surfaceMemory->GetBase() == buffers_[INDEX_OUTPUT][curIndex]->avBuffer->memory_->GetAddr() &&
1214 surfaceMemory->GetSize() == buffers_[INDEX_OUTPUT][curIndex]->avBuffer->memory_->GetCapacity()) {
1215 buffers_[INDEX_OUTPUT][index]->sMemory = buffers_[INDEX_OUTPUT][curIndex]->sMemory;
1216 buffers_[INDEX_OUTPUT][curIndex]->sMemory = surfaceMemory;
1217 break;
1218 } else {
1219 renderAvailQue_->Push(curIndex);
1220 }
1221 }
1222 if (i == queSize) {
1223 curIndex = index;
1224 outputBuffer->avBuffer = AVBuffer::CreateAVBuffer(surfaceMemory->GetBase(), surfaceMemory->GetSize());
1225 outputBuffer->width = width_;
1226 outputBuffer->height = height_;
1227 FindAvailIndex(curIndex);
1228 }
1229 buffers_[INDEX_OUTPUT][curIndex]->owner_ = HBuffer::Owner::OWNED_BY_CODEC;
1230 codecAvailQue_->Push(curIndex);
1231 if (renderSurfaceBufferMap_.count(curIndex)) {
1232 renderSurfaceBufferMap_.erase(curIndex);
1233 }
1234 AVCODEC_LOGD("Request output buffer success, index = %{public}u, queSize=%{public}zu, i=%{public}d", curIndex,
1235 queSize, i);
1236 }
1237
BufferReleasedByConsumer(uint64_t surfaceId)1238 GSError HevcDecoder::BufferReleasedByConsumer(uint64_t surfaceId)
1239 {
1240 CHECK_AND_RETURN_RET_LOG(state_ == State::RUNNING || state_ == State::EOS, GSERROR_NO_PERMISSION,
1241 "In valid state");
1242 std::lock_guard<std::mutex> sLock(surfaceMutex_);
1243 CHECK_AND_RETURN_RET_LOG(renderAvailQue_->Size() > 0, GSERROR_NO_BUFFER, "No available buffer");
1244 CHECK_AND_RETURN_RET_LOG(surfaceId == sInfo_.surface->GetUniqueId(), GSERROR_INVALID_ARGUMENTS,
1245 "Ignore callback from old surface");
1246 RequestBufferFromConsumer();
1247 return GSERROR_OK;
1248 }
1249
UnRegisterListenerToSurface(const sptr<Surface> & surface)1250 int32_t HevcDecoder::UnRegisterListenerToSurface(const sptr<Surface> &surface)
1251 {
1252 GSError err = surface->UnRegisterReleaseListener();
1253 CHECK_AND_RETURN_RET_LOG(err == GSERROR_OK, AVCS_ERR_UNKNOWN,
1254 "surface %{public}" PRIu64 ", UnRegisterReleaseListener failed, GSError=%{public}d",
1255 surface->GetUniqueId(), err);
1256 return AVCS_ERR_OK;
1257 }
1258
RegisterListenerToSurface(const sptr<Surface> & surface)1259 GSError HevcDecoder::RegisterListenerToSurface(const sptr<Surface> &surface)
1260 {
1261 uint64_t surfaceId = surface->GetUniqueId();
1262 wptr<HevcDecoder> wp = this;
1263 GSError err = surface->RegisterReleaseListener([wp, surfaceId](sptr<SurfaceBuffer> &) {
1264 sptr<HevcDecoder> codec = wp.promote();
1265 if (!codec) {
1266 AVCODEC_LOGD("decoder is gone");
1267 return GSERROR_OK;
1268 }
1269 return codec->BufferReleasedByConsumer(surfaceId);
1270 });
1271 return err;
1272 }
1273
ReleaseOutputBuffer(uint32_t index)1274 int32_t HevcDecoder::ReleaseOutputBuffer(uint32_t index)
1275 {
1276 AVCODEC_SYNC_TRACE;
1277 std::unique_lock<std::mutex> oLock(outputMutex_);
1278 CHECK_AND_RETURN_RET_LOG(index < buffers_[INDEX_OUTPUT].size(), AVCS_ERR_INVALID_VAL,
1279 "Failed to release output buffer: invalid index");
1280 std::shared_ptr<HBuffer> frameBuffer = buffers_[INDEX_OUTPUT][index];
1281 oLock.unlock();
1282 if (frameBuffer->owner_ == HBuffer::Owner::OWNED_BY_USER) {
1283 frameBuffer->owner_ = HBuffer::Owner::OWNED_BY_CODEC;
1284 codecAvailQue_->Push(index);
1285 return AVCS_ERR_OK;
1286 } else {
1287 AVCODEC_LOGE("Release output buffer failed: check your index=%{public}u", index);
1288 return AVCS_ERR_INVALID_VAL;
1289 }
1290 }
1291
FlushSurfaceMemory(std::shared_ptr<FSurfaceMemory> & surfaceMemory,uint32_t index)1292 int32_t HevcDecoder::FlushSurfaceMemory(std::shared_ptr<FSurfaceMemory> &surfaceMemory, uint32_t index)
1293 {
1294 sptr<SurfaceBuffer> surfaceBuffer = surfaceMemory->GetSurfaceBuffer();
1295 CHECK_AND_RETURN_RET_LOG(surfaceBuffer != nullptr, AVCS_ERR_INVALID_VAL,
1296 "Failed to update surface memory: surface buffer is NULL");
1297 OHOS::BufferFlushConfig flushConfig = {{0, 0, surfaceBuffer->GetWidth(), surfaceBuffer->GetHeight()},
1298 outAVBuffer4Surface_[index]->pts_, -1};
1299 surfaceMemory->SetNeedRender(true);
1300 surfaceMemory->UpdateSurfaceBufferScaleMode();
1301 if (outAVBuffer4Surface_[index]->meta_->Find(OHOS::Media::Tag::VIDEO_DECODER_DESIRED_PRESENT_TIMESTAMP) !=
1302 outAVBuffer4Surface_[index]->meta_->end()) {
1303 outAVBuffer4Surface_[index]->meta_->Get<OHOS::Media::Tag::VIDEO_DECODER_DESIRED_PRESENT_TIMESTAMP>(
1304 flushConfig.desiredPresentTimestamp);
1305 outAVBuffer4Surface_[index]->meta_->Remove(OHOS::Media::Tag::VIDEO_DECODER_DESIRED_PRESENT_TIMESTAMP);
1306 }
1307 auto res = sInfo_.surface->FlushBuffer(surfaceBuffer, -1, flushConfig);
1308 if (res != OHOS::SurfaceError::SURFACE_ERROR_OK) {
1309 AVCODEC_LOGW("Failed to update surface memory: %{public}d", res);
1310 surfaceMemory->SetNeedRender(false);
1311 surfaceMemory->ReleaseSurfaceBuffer();
1312 return AVCS_ERR_UNKNOWN;
1313 }
1314 renderSurfaceBufferMap_[index] = std::make_pair(surfaceBuffer, flushConfig);
1315 surfaceMemory->ReleaseSurfaceBuffer();
1316 return AVCS_ERR_OK;
1317 }
1318
RenderOutputBuffer(uint32_t index)1319 int32_t HevcDecoder::RenderOutputBuffer(uint32_t index)
1320 {
1321 AVCODEC_SYNC_TRACE;
1322 CHECK_AND_RETURN_RET_LOG(sInfo_.surface != nullptr, AVCS_ERR_UNSUPPORT,
1323 "RenderOutputBuffer fail, surface is nullptr");
1324 std::unique_lock<std::mutex> oLock(outputMutex_);
1325 CHECK_AND_RETURN_RET_LOG(index < buffers_[INDEX_OUTPUT].size(), AVCS_ERR_INVALID_VAL,
1326 "Failed to render output buffer: invalid index");
1327 std::shared_ptr<HBuffer> frameBuffer = buffers_[INDEX_OUTPUT][index];
1328 oLock.unlock();
1329 std::lock_guard<std::mutex> sLock(surfaceMutex_);
1330 if (frameBuffer->owner_ == HBuffer::Owner::OWNED_BY_USER) {
1331 std::shared_ptr<FSurfaceMemory> surfaceMemory = frameBuffer->sMemory;
1332 int32_t ret = FlushSurfaceMemory(surfaceMemory, index);
1333 if (ret != AVCS_ERR_OK) {
1334 AVCODEC_LOGW("Update surface memory failed: %{public}d", static_cast<int32_t>(ret));
1335 } else {
1336 AVCODEC_LOGD("Update surface memory successful");
1337 }
1338 frameBuffer->owner_ = HBuffer::Owner::OWNED_BY_SURFACE;
1339 renderAvailQue_->Push(index);
1340 AVCODEC_LOGD("render output buffer with index, index=%{public}u", index);
1341 return AVCS_ERR_OK;
1342 } else {
1343 AVCODEC_LOGE("Failed to render output buffer with bad index, index=%{public}u", index);
1344 return AVCS_ERR_INVALID_VAL;
1345 }
1346 }
1347
ReplaceOutputSurfaceWhenRunning(sptr<Surface> newSurface)1348 int32_t HevcDecoder::ReplaceOutputSurfaceWhenRunning(sptr<Surface> newSurface)
1349 {
1350 CHECK_AND_RETURN_RET_LOG(sInfo_.surface != nullptr, AV_ERR_OPERATE_NOT_PERMIT,
1351 "Not support convert from AVBuffer Mode to Surface Mode");
1352 sptr<Surface> curSurface = sInfo_.surface;
1353 uint64_t oldId = curSurface->GetUniqueId();
1354 uint64_t newId = newSurface->GetUniqueId();
1355 AVCODEC_LOGI("surface %{public}" PRIu64 " -> %{public}" PRIu64 "", oldId, newId);
1356 if (oldId == newId) {
1357 return AVCS_ERR_OK;
1358 }
1359 GSError err = RegisterListenerToSurface(newSurface);
1360 CHECK_AND_RETURN_RET_LOG(err == GSERROR_OK, AVCS_ERR_UNKNOWN,
1361 "surface %{public}" PRIu64 ", RegisterListenerToSurface failed, GSError=%{public}d",
1362 newSurface->GetUniqueId(), err);
1363 int32_t outputBufferCnt = 0;
1364 format_.GetIntValue(MediaDescriptionKey::MD_KEY_MAX_OUTPUT_BUFFER_COUNT, outputBufferCnt);
1365 int32_t ret = SetQueueSize(newSurface, outputBufferCnt);
1366 if (ret != AVCS_ERR_OK) {
1367 UnRegisterListenerToSurface(newSurface);
1368 return ret;
1369 }
1370 std::unique_lock<std::mutex> sLock(surfaceMutex_);
1371 ret = SwitchBetweenSurface(newSurface);
1372 if (ret != AVCS_ERR_OK) {
1373 UnRegisterListenerToSurface(newSurface);
1374 sInfo_.surface = curSurface;
1375 return ret;
1376 }
1377 sLock.unlock();
1378 return AVCS_ERR_OK;
1379 }
1380
SetQueueSize(const sptr<Surface> & surface,uint32_t targetSize)1381 int32_t HevcDecoder::SetQueueSize(const sptr<Surface> &surface, uint32_t targetSize)
1382 {
1383 int32_t err = surface->SetQueueSize(targetSize);
1384 if (err != 0) {
1385 AVCODEC_LOGE("surface %{public}" PRIu64 ", SetQueueSize to %{public}u failed, GSError=%{public}d",
1386 surface->GetUniqueId(), targetSize, err);
1387 return AVCS_ERR_UNKNOWN;
1388 }
1389 AVCODEC_LOGI("surface %{public}" PRIu64 ", SetQueueSize to %{public}u succ", surface->GetUniqueId(), targetSize);
1390 return AVCS_ERR_OK;
1391 }
1392
SwitchBetweenSurface(const sptr<Surface> & newSurface)1393 int32_t HevcDecoder::SwitchBetweenSurface(const sptr<Surface> &newSurface)
1394 {
1395 sptr<Surface> curSurface = sInfo_.surface;
1396 newSurface->Connect(); // cleancache will work only if the surface is connected by us
1397 newSurface->CleanCache(); // make sure new surface is empty
1398 std::vector<uint32_t> ownedBySurfaceBufferIndex;
1399 uint64_t newId = newSurface->GetUniqueId();
1400 for (uint32_t index = 0; index < buffers_[INDEX_OUTPUT].size(); index++) {
1401 if (buffers_[INDEX_OUTPUT][index]->sMemory == nullptr) {
1402 continue;
1403 }
1404 sptr<SurfaceBuffer> surfaceBuffer = nullptr;
1405 if (buffers_[INDEX_OUTPUT][index]->owner_ == HBuffer::Owner::OWNED_BY_SURFACE) {
1406 if (renderSurfaceBufferMap_.count(index)) {
1407 surfaceBuffer = renderSurfaceBufferMap_[index].first;
1408 ownedBySurfaceBufferIndex.push_back(index);
1409 }
1410 } else {
1411 surfaceBuffer = buffers_[INDEX_OUTPUT][index]->sMemory->GetSurfaceBuffer();
1412 }
1413 if (surfaceBuffer == nullptr) {
1414 AVCODEC_LOGE("Get old surface buffer error!");
1415 return AVCS_ERR_UNKNOWN;
1416 }
1417 int32_t err = newSurface->AttachBufferToQueue(surfaceBuffer);
1418 if (err != 0) {
1419 AVCODEC_LOGE("surface %{public}" PRIu64 ", AttachBufferToQueue(seq=%{public}u) failed, GSError=%{public}d",
1420 newId, surfaceBuffer->GetSeqNum(), err);
1421 return AVCS_ERR_UNKNOWN;
1422 }
1423 }
1424 int32_t videoRotation = 0;
1425 format_.GetIntValue(MediaDescriptionKey::MD_KEY_ROTATION_ANGLE, videoRotation);
1426 newSurface->SetTransform(TranslateSurfaceRotation(static_cast<VideoRotation>(videoRotation)));
1427 sInfo_.surface = newSurface;
1428
1429 for (uint32_t index: ownedBySurfaceBufferIndex) {
1430 int32_t ret = RenderNewSurfaceWithOldBuffer(newSurface, index);
1431 if (ret != AVCS_ERR_OK) {
1432 return ret;
1433 }
1434 }
1435
1436 int32_t ret = UnRegisterListenerToSurface(curSurface);
1437 if (ret != AVCS_ERR_OK) {
1438 return ret;
1439 }
1440
1441 curSurface->CleanCache(true); // make sure old surface is empty and go black
1442 return AVCS_ERR_OK;
1443 }
1444
RenderNewSurfaceWithOldBuffer(const sptr<Surface> & newSurface,uint32_t index)1445 int32_t HevcDecoder::RenderNewSurfaceWithOldBuffer(const sptr<Surface> &newSurface, uint32_t index)
1446 {
1447 std::shared_ptr<FSurfaceMemory> surfaceMemory = buffers_[INDEX_OUTPUT][index]->sMemory;
1448 sptr<SurfaceBuffer> surfaceBuffer = renderSurfaceBufferMap_[index].first;
1449 OHOS::BufferFlushConfig flushConfig = renderSurfaceBufferMap_[index].second;
1450 surfaceMemory->SetNeedRender(true);
1451 newSurface->SetScalingMode(surfaceBuffer->GetSeqNum(), sInfo_.scalingMode);
1452 auto res = newSurface->FlushBuffer(surfaceBuffer, -1, flushConfig);
1453 if (res != OHOS::SurfaceError::SURFACE_ERROR_OK) {
1454 AVCODEC_LOGE("Failed to update surface memory: %{public}d", res);
1455 surfaceMemory->SetNeedRender(false);
1456 return AVCS_ERR_UNKNOWN;
1457 }
1458 return AVCS_ERR_OK;
1459 }
1460
SetOutputSurface(sptr<Surface> surface)1461 int32_t HevcDecoder::SetOutputSurface(sptr<Surface> surface)
1462 {
1463 AVCODEC_SYNC_TRACE;
1464 CHECK_AND_RETURN_RET_LOG(state_ != State::UNINITIALIZED, AV_ERR_INVALID_VAL,
1465 "set output surface fail: not initialized or configured");
1466 CHECK_AND_RETURN_RET_LOG((state_ == State::CONFIGURED || state_ == State::FLUSHED ||
1467 state_ == State::RUNNING || state_ == State::EOS), AVCS_ERR_INVALID_STATE,
1468 "set output surface fail: state %{public}d not support set output surface",
1469 static_cast<int32_t>(state_.load()));
1470 if (surface == nullptr || surface->IsConsumer()) {
1471 AVCODEC_LOGE("Set surface fail");
1472 return AVCS_ERR_INVALID_VAL;
1473 }
1474 if (state_ == State::FLUSHED || state_ == State::RUNNING || state_ == State::EOS) {
1475 return ReplaceOutputSurfaceWhenRunning(surface);
1476 }
1477 sInfo_.surface = surface;
1478 GSError err = RegisterListenerToSurface(sInfo_.surface);
1479 CHECK_AND_RETURN_RET_LOG(err == GSERROR_OK, AVCS_ERR_UNKNOWN,
1480 "surface %{public}" PRIu64 ", RegisterListenerToSurface failed, GSError=%{public}d",
1481 sInfo_.surface->GetUniqueId(), err);
1482 if (!format_.ContainKey(MediaDescriptionKey::MD_KEY_SCALE_TYPE)) {
1483 format_.PutIntValue(MediaDescriptionKey::MD_KEY_SCALE_TYPE,
1484 static_cast<int32_t>(ScalingMode::SCALING_MODE_SCALE_TO_WINDOW));
1485 }
1486 if (!format_.ContainKey(MediaDescriptionKey::MD_KEY_ROTATION_ANGLE)) {
1487 format_.PutIntValue(MediaDescriptionKey::MD_KEY_ROTATION_ANGLE,
1488 static_cast<int32_t>(VideoRotation::VIDEO_ROTATION_0));
1489 }
1490 AVCODEC_LOGI("Set surface success");
1491 return AVCS_ERR_OK;
1492 }
1493
SetCallback(const std::shared_ptr<MediaCodecCallback> & callback)1494 int32_t HevcDecoder::SetCallback(const std::shared_ptr<MediaCodecCallback> &callback)
1495 {
1496 AVCODEC_SYNC_TRACE;
1497 CHECK_AND_RETURN_RET_LOG(callback != nullptr, AVCS_ERR_INVALID_VAL, "Set callback failed: callback is NULL");
1498 callback_ = callback;
1499 return AVCS_ERR_OK;
1500 }
1501
CheckHevcDecLibStatus()1502 int32_t HevcDecoder::CheckHevcDecLibStatus()
1503 {
1504 void* handle = dlopen(HEVC_DEC_LIB_PATH, RTLD_LAZY);
1505 if (handle != nullptr) {
1506 auto hevcDecoderCreateFunc = reinterpret_cast<CreateHevcDecoderFuncType>(
1507 dlsym(handle, HEVC_DEC_CREATE_FUNC_NAME));
1508 auto hevcDecoderDecodecFrameFunc = reinterpret_cast<DecodeFuncType>(
1509 dlsym(handle, HEVC_DEC_DECODE_FRAME_FUNC_NAME));
1510 auto hevcDecoderFlushFrameFunc = reinterpret_cast<FlushFuncType>(dlsym(handle, HEVC_DEC_FLUSH_FRAME_FUNC_NAME));
1511 auto hevcDecoderDeleteFunc = reinterpret_cast<DeleteFuncType>(dlsym(handle, HEVC_DEC_DELETE_FUNC_NAME));
1512 if (hevcDecoderCreateFunc == nullptr || hevcDecoderDecodecFrameFunc == nullptr ||
1513 hevcDecoderDeleteFunc == nullptr || hevcDecoderFlushFrameFunc == nullptr) {
1514 AVCODEC_LOGE("HevcDecoder hevcFuncMatch_ failed!");
1515 hevcDecoderCreateFunc = nullptr;
1516 hevcDecoderDecodecFrameFunc = nullptr;
1517 hevcDecoderFlushFrameFunc = nullptr;
1518 hevcDecoderDeleteFunc = nullptr;
1519 dlclose(handle);
1520 handle = nullptr;
1521 }
1522 }
1523
1524 if (handle == nullptr) {
1525 return AVCS_ERR_UNSUPPORT;
1526 }
1527 dlclose(handle);
1528 handle = nullptr;
1529
1530 return AVCS_ERR_OK;
1531 }
1532
GetCodecCapability(std::vector<CapabilityData> & capaArray)1533 int32_t HevcDecoder::GetCodecCapability(std::vector<CapabilityData> &capaArray)
1534 {
1535 CHECK_AND_RETURN_RET_LOG(CheckHevcDecLibStatus() == AVCS_ERR_OK, AVCS_ERR_UNSUPPORT,
1536 "hevc decoder libs not available");
1537
1538 for (uint32_t i = 0; i < SUPPORT_HEVC_DECODER_NUM; ++i) {
1539 CapabilityData capsData;
1540 capsData.codecName = static_cast<std::string>(SUPPORT_HEVC_DECODER[i].codecName);
1541 capsData.mimeType = static_cast<std::string>(SUPPORT_HEVC_DECODER[i].mimeType);
1542 capsData.codecType = AVCODEC_TYPE_VIDEO_DECODER;
1543 capsData.isVendor = false;
1544 capsData.maxInstance = VIDEO_INSTANCE_SIZE;
1545 capsData.alignment.width = VIDEO_ALIGNMENT_SIZE;
1546 capsData.alignment.height = VIDEO_ALIGNMENT_SIZE;
1547 capsData.width.minVal = VIDEO_MIN_SIZE;
1548 capsData.width.maxVal = VIDEO_MAX_WIDTH_SIZE;
1549 capsData.height.minVal = VIDEO_MIN_SIZE;
1550 capsData.height.maxVal = VIDEO_MAX_HEIGHT_SIZE;
1551 capsData.blockPerFrame.minVal = 1;
1552 capsData.blockPerFrame.maxVal = VIDEO_BLOCKPERFRAME_SIZE;
1553 capsData.blockPerSecond.minVal = 1;
1554 capsData.blockPerSecond.maxVal = VIDEO_BLOCKPERSEC_SIZE;
1555 capsData.blockSize.width = VIDEO_ALIGN_SIZE;
1556 capsData.blockSize.height = VIDEO_ALIGN_SIZE;
1557 capsData.pixFormat = {static_cast<int32_t>(VideoPixelFormat::NV12),
1558 static_cast<int32_t>(VideoPixelFormat::NV21)};
1559 capsData.profiles = {static_cast<int32_t>(HEVC_PROFILE_MAIN), static_cast<int32_t>(HEVC_PROFILE_MAIN_10)};
1560
1561 std::vector<int32_t> levels;
1562 for (int32_t j = 0; j <= static_cast<int32_t>(HEVCLevel::HEVC_LEVEL_62); ++j) {
1563 levels.emplace_back(j);
1564 }
1565 capsData.profileLevelsMap.insert(std::make_pair(static_cast<int32_t>(HEVC_PROFILE_MAIN), levels));
1566 capsData.profileLevelsMap.insert(std::make_pair(static_cast<int32_t>(HEVC_PROFILE_MAIN_10), levels));
1567 capaArray.emplace_back(capsData);
1568 }
1569 return AVCS_ERR_OK;
1570 }
1571
HevcDecLog(UINT32 channelId,IHW265VIDEO_ALG_LOG_LEVEL eLevel,INT8 * pMsg,...)1572 void HevcDecLog(UINT32 channelId, IHW265VIDEO_ALG_LOG_LEVEL eLevel, INT8 *pMsg, ...)
1573 {
1574 va_list args;
1575 int32_t maxSize = 1024; // 1024 max size of one log
1576 std::vector<char> buf(maxSize);
1577 va_start(args, reinterpret_cast<const char*>(pMsg));
1578 int32_t size = vsnprintf_s(buf.data(), buf.size(), buf.size()-1, reinterpret_cast<const char*>(pMsg), args);
1579 va_end(args);
1580 if (size >= maxSize) {
1581 size = maxSize - 1;
1582 }
1583
1584 auto msg = std::string(buf.data(), size);
1585
1586 if (eLevel <= IHW265VIDEO_ALG_LOG_ERROR) {
1587 switch (eLevel) {
1588 case IHW265VIDEO_ALG_LOG_ERROR: {
1589 AVCODEC_LOGE("%{public}s", msg.c_str());
1590 break;
1591 }
1592 case IHW265VIDEO_ALG_LOG_WARNING: {
1593 AVCODEC_LOGW("%{public}s", msg.c_str());
1594 break;
1595 }
1596 case IHW265VIDEO_ALG_LOG_INFO: {
1597 AVCODEC_LOGI("%{public}s", msg.c_str());
1598 break;
1599 }
1600 case IHW265VIDEO_ALG_LOG_DEBUG: {
1601 AVCODEC_LOGD("%{public}s", msg.c_str());
1602 break;
1603 }
1604 default: {
1605 AVCODEC_LOGI("%{public}s", msg.c_str());
1606 break;
1607 }
1608 }
1609 }
1610
1611 return;
1612 }
1613
1614 std::mutex HevcDecoder::decoderCountMutex_;
1615 std::vector<uint32_t> HevcDecoder::decInstanceIDSet_;
1616 std::vector<uint32_t> HevcDecoder::freeIDSet_;
1617
1618 } // namespace Codec
1619 } // namespace MediaAVCodec
1620 } // namespace OHOS
1621