/* * Copyright (C) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include "iconsumer_surface.h" #include "openssl/crypto.h" #include "openssl/sha.h" #include "videodec_ndk_sample.h" using namespace OHOS; using namespace OHOS::Media; using namespace std; namespace { const string MIME_TYPE = "video/avc"; constexpr int64_t NANOS_IN_SECOND = 1000000000L; constexpr int64_t NANOS_IN_MICRO = 1000L; constexpr int32_t THREE = 3; constexpr int32_t EIGHT = 8; constexpr int32_t TEN = 10; constexpr int32_t SIXTEEN = 16; constexpr int32_t TWENTY_FOUR = 24; constexpr uint32_t FRAME_INTERVAL = 1; // 16666 constexpr uint8_t H264_NALU_TYPE = 0x1f; constexpr uint32_t START_CODE_SIZE = 4; constexpr uint8_t START_CODE[START_CODE_SIZE] = {0, 0, 0, 1}; constexpr uint8_t SPS = 7; constexpr uint8_t PPS = 8; SHA512_CTX c; sptr cs = nullptr; sptr ps = nullptr; unsigned char md[SHA512_DIGEST_LENGTH]; VDecNdkSample *dec_sample = nullptr; void clearIntqueue(std::queue &q) { std::queue empty; swap(empty, q); } void clearBufferqueue(std::queue &q) { std::queue empty; swap(empty, q); } } // namespace class TestConsumerListener : public IBufferConsumerListener { public: TestConsumerListener(sptr cs, std::string_view name) : cs(cs) {}; ~TestConsumerListener() {} void OnBufferAvailable() override { sptr buffer; int32_t flushFence; cs->AcquireBuffer(buffer, flushFence, timestamp, damage); cs->ReleaseBuffer(buffer, -1); } private: int64_t timestamp = 0; Rect damage = {}; sptr cs {nullptr}; }; VDecNdkSample::~VDecNdkSample() { Release(); } void VdecError(OH_AVCodec *codec, int32_t errorCode, void *userData) { cout << "Error errorCode=" << errorCode << endl; dec_sample->StopInloop(); dec_sample->StopOutloop(); dec_sample->ReleaseInFile(); dec_sample->Release(); } void VdecFormatChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData) { cout << "Format Changed" << endl; int32_t current_width = 0; int32_t current_height = 0; OH_AVFormat_GetIntValue(format, OH_MD_KEY_WIDTH, ¤t_width); OH_AVFormat_GetIntValue(format, OH_MD_KEY_HEIGHT, ¤t_height); dec_sample->DEFAULT_WIDTH = current_width; dec_sample->DEFAULT_HEIGHT = current_height; } void VdecInputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, void *userData) { VDecSignal *signal = static_cast(userData); unique_lock lock(signal->inMutex_); signal->inIdxQueue_.push(index); signal->inBufferQueue_.push(data); signal->inCond_.notify_all(); } void VdecOutputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, OH_AVCodecBufferAttr *attr, void *userData) { VDecSignal *signal = static_cast(userData); unique_lock lock(signal->outMutex_); signal->outIdxQueue_.push(index); signal->attrQueue_.push(*attr); signal->outBufferQueue_.push(data); signal->outCond_.notify_all(); } void VDecNdkSample::Flush_buffer() { unique_lock inLock(signal_->inMutex_); clearIntqueue(signal_->inIdxQueue_); std::queue empty; swap(empty, signal_->inBufferQueue_); signal_->inCond_.notify_all(); inLock.unlock(); unique_lock outLock(signal_->outMutex_); clearIntqueue(signal_->outIdxQueue_); clearBufferqueue(signal_->attrQueue_); signal_->outCond_.notify_all(); outLock.unlock(); } bool VDecNdkSample::MdCompare(unsigned char buffer[], int len, const char *source[]) { bool result = true; for (int i = 0; i < len; i++) { } return result; } int64_t VDecNdkSample::GetSystemTimeUs() { struct timespec now; (void)clock_gettime(CLOCK_BOOTTIME, &now); int64_t nanoTime = (int64_t)now.tv_sec * NANOS_IN_SECOND + now.tv_nsec; return nanoTime / NANOS_IN_MICRO; } int32_t VDecNdkSample::ConfigureVideoDecoder() { OH_AVFormat *format = OH_AVFormat_Create(); if (format == nullptr) { cout << "Fatal: Failed to create format" << endl; return AV_ERR_UNKNOWN; } (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH); (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT); (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12); (void)OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, DEFAULT_FRAME_RATE); int ret = OH_VideoDecoder_Configure(vdec_, format); OH_AVFormat_Destroy(format); return ret; } int32_t VDecNdkSample::RunVideoDec_Surface(string codeName) { SF_OUTPUT = true; int err = AV_ERR_OK; cs = Surface::CreateSurfaceAsConsumer(); sptr listener = new TestConsumerListener(cs, OUT_DIR); cs->RegisterConsumerListener(listener); auto p = cs->GetProducer(); ps = Surface::CreateSurfaceAsProducer(p); OHNativeWindow *nativeWindow = CreateNativeWindowFromSurface(&ps); if (!nativeWindow) { cout << "Failed to create surface" << endl; return AV_ERR_UNKNOWN; } err = CreateVideoDecoder(codeName); if (err != AV_ERR_OK) { cout << "Failed to create video decoder" << endl; return err; } err = SetVideoDecoderCallback(); if (err != AV_ERR_OK) { cout << "Failed to setCallback" << endl; Release(); return err; } err = ConfigureVideoDecoder(); if (err != AV_ERR_OK) { cout << "Failed to configure video decoder" << endl; Release(); return err; } err = OH_VideoDecoder_SetSurface(vdec_, nativeWindow); if (err != AV_ERR_OK) { cout << "Failed to set surface" << endl; return err; } err = StartVideoDecoder(); if (err != AV_ERR_OK) { cout << "Failed to start video decoder" << endl; Release(); return err; } return err; } int32_t VDecNdkSample::RunVideoDec(string codeName) { SF_OUTPUT = false; int err = CreateVideoDecoder(codeName); if (err != AV_ERR_OK) { cout << "Failed to create video decoder" << endl; return err; } err = ConfigureVideoDecoder(); if (err != AV_ERR_OK) { cout << "Failed to configure video decoder" << endl; Release(); return err; } err = SetVideoDecoderCallback(); if (err != AV_ERR_OK) { cout << "Failed to setCallback" << endl; Release(); return err; } err = StartVideoDecoder(); if (err != AV_ERR_OK) { cout << "Failed to start video decoder" << endl; Release(); return err; } return err; } int32_t VDecNdkSample::SetVideoDecoderCallback() { signal_ = new VDecSignal(); if (signal_ == nullptr) { cout << "Failed to new VDecSignal" << endl; return AV_ERR_UNKNOWN; } cb_.onError = VdecError; cb_.onStreamChanged = VdecFormatChanged; cb_.onNeedInputData = VdecInputDataReady; cb_.onNeedOutputData = VdecOutputDataReady; return OH_VideoDecoder_SetCallback(vdec_, cb_, static_cast(signal_)); } void VDecNdkSample::ReleaseInFile() { if (inFile_ != nullptr) { if (inFile_->is_open()) { inFile_->close(); } inFile_.reset(); inFile_ = nullptr; } } void VDecNdkSample::StopInloop() { if (inputLoop_ != nullptr && inputLoop_->joinable()) { unique_lock lock(signal_->inMutex_); clearIntqueue(signal_->inIdxQueue_); signal_->inCond_.notify_all(); lock.unlock(); inputLoop_->join(); inputLoop_.reset(); } } int32_t VDecNdkSample::CreateVideoDecoder(string codeName) { vdec_ = OH_VideoDecoder_CreateByName(codeName.c_str()); dec_sample = this; return vdec_ == nullptr ? AV_ERR_UNKNOWN : AV_ERR_OK; } int32_t VDecNdkSample::StartVideoDecoder() { isRunning_.store(true); int ret = OH_VideoDecoder_Start(vdec_); if (ret != AV_ERR_OK) { cout << "Failed to start codec" << endl; isRunning_.store(false); ReleaseInFile(); Release(); return ret; } inFile_ = make_unique(); if (inFile_ == nullptr) { isRunning_.store(false); (void)OH_VideoDecoder_Stop(vdec_); return AV_ERR_UNKNOWN; } inFile_->open(INP_DIR, ios::in | ios::binary); if (!inFile_->is_open()) { cout << "failed open file " << INP_DIR << endl; isRunning_.store(false); (void)OH_VideoDecoder_Stop(vdec_); inFile_->close(); inFile_.reset(); inFile_ = nullptr; return AV_ERR_UNKNOWN; } inputLoop_ = make_unique(&VDecNdkSample::InputFuncTest, this); if (inputLoop_ == nullptr) { cout << "Failed to create input loop" << endl; isRunning_.store(false); (void)OH_VideoDecoder_Stop(vdec_); ReleaseInFile(); return AV_ERR_UNKNOWN; } outputLoop_ = make_unique(&VDecNdkSample::OutputFuncTest, this); if (outputLoop_ == nullptr) { cout << "Failed to create output loop" << endl; isRunning_.store(false); (void)OH_VideoDecoder_Stop(vdec_); ReleaseInFile(); StopInloop(); Release(); return AV_ERR_UNKNOWN; } return AV_ERR_OK; } void VDecNdkSample::testAPI() { cs = Surface::CreateSurfaceAsConsumer(); sptr listener = new TestConsumerListener(cs, OUT_DIR); cs->RegisterConsumerListener(listener); auto p = cs->GetProducer(); ps = Surface::CreateSurfaceAsProducer(p); OHNativeWindow *nativeWindow = CreateNativeWindowFromSurface(&ps); OH_VideoDecoder_SetSurface(vdec_, nativeWindow); OH_VideoDecoder_Prepare(vdec_); OH_VideoDecoder_Start(vdec_); OH_AVFormat *format = OH_AVFormat_Create(); (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH); (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT); (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12); (void)OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, DEFAULT_FRAME_RATE); OH_VideoDecoder_SetParameter(vdec_, format); OH_AVFormat_Destroy(format); OH_VideoDecoder_GetOutputDescription(vdec_); OH_VideoDecoder_Flush(vdec_); OH_VideoDecoder_Stop(vdec_); OH_VideoDecoder_Reset(vdec_); bool isvalid = false; OH_VideoDecoder_IsValid(vdec_, &isvalid); } void VDecNdkSample::WaitForEOS() { if (inputLoop_ && inputLoop_->joinable()) { inputLoop_->join(); } if (outputLoop_ && outputLoop_->joinable()) { outputLoop_->join(); } } void VDecNdkSample::InputFuncTest() { while (true) { if (!isRunning_.load()) { break; } if (REPEAT_START_FLUSH_BEFORE_EOS > 0) { REPEAT_START_FLUSH_BEFORE_EOS--; OH_VideoDecoder_Flush(vdec_); Flush_buffer(); OH_VideoDecoder_Start(vdec_); } if (REPEAT_START_STOP_BEFORE_EOS > 0) { REPEAT_START_STOP_BEFORE_EOS--; OH_VideoDecoder_Stop(vdec_); Flush_buffer(); OH_VideoDecoder_Start(vdec_); } uint32_t index; unique_lock lock(signal_->inMutex_); signal_->inCond_.wait(lock, [this]() { return signal_->inIdxQueue_.size() > 0; }); if (!isRunning_.load()) { break; } index = signal_->inIdxQueue_.front(); auto buffer = signal_->inBufferQueue_.front(); signal_->inIdxQueue_.pop(); signal_->inBufferQueue_.pop(); lock.unlock(); if (!inFile_->eof()) { int ret = PushData(index, buffer); if (ret == 1) { break; } } if (sleepOnFPS) { usleep(FRAME_INTERVAL); } } } int32_t VDecNdkSample::PushData(uint32_t index, OH_AVMemory *buffer) { static uint32_t repeat_count = 0; OH_AVCodecBufferAttr attr; if (BEFORE_EOS_INPUT && frameCount_ > TEN) { SetEOS(index); return 1; } if (BEFORE_EOS_INPUT_INPUT && frameCount_ > TEN) { memset_s(&attr, sizeof(OH_AVCodecBufferAttr), 0, sizeof(OH_AVCodecBufferAttr)); attr.flags = AVCODEC_BUFFER_FLAGS_EOS; BEFORE_EOS_INPUT_INPUT = false; } char ch[4] = {}; (void)inFile_->read(ch, START_CODE_SIZE); if (repeatRun && inFile_->eof()) { inFile_->clear(); inFile_->seekg(0, ios::beg); cout << "repeat run " << repeat_count << endl; repeat_count++; return 0; } if (inFile_->eof()) { SetEOS(index); return 1; } uint32_t bufferSize = (uint32_t)(((ch[3] & 0xFF)) | ((ch[2] & 0xFF) << EIGHT) | ((ch[1] & 0xFF) << SIXTEEN) | ((ch[0] & 0xFF) << TWENTY_FOUR)); if (bufferSize >= DEFAULT_WIDTH * DEFAULT_HEIGHT * THREE >> 1) { cout << "read bufferSize abnormal. buffersize = " << bufferSize << endl; return 1; } return SendData(bufferSize, index, buffer); } uint32_t VDecNdkSample::SendData(uint32_t bufferSize, uint32_t index, OH_AVMemory *buffer) { OH_AVCodecBufferAttr attr; uint8_t *fileBuffer = new uint8_t[bufferSize + START_CODE_SIZE]; if (fileBuffer == nullptr) { cout << "Fatal: no memory" << endl; delete[] fileBuffer; return 0; } if (memcpy_s(fileBuffer, bufferSize + START_CODE_SIZE, START_CODE, START_CODE_SIZE) != EOK) { cout << "Fatal: memory copy failed" << endl; } (void)inFile_->read((char *)fileBuffer + START_CODE_SIZE, bufferSize); if ((fileBuffer[START_CODE_SIZE] & H264_NALU_TYPE) == SPS || (fileBuffer[START_CODE_SIZE] & H264_NALU_TYPE) == PPS) { attr.flags = AVCODEC_BUFFER_FLAGS_CODEC_DATA; } else { attr.flags = AVCODEC_BUFFER_FLAGS_NONE; } int32_t size = OH_AVMemory_GetSize(buffer); if (size < bufferSize + START_CODE_SIZE) { delete[] fileBuffer; cout << "buffer size not enough." << endl; return 0; } uint8_t *avBuffer = OH_AVMemory_GetAddr(buffer); if (avBuffer == nullptr) { cout << "avBuffer == nullptr" << endl; inFile_->clear(); inFile_->seekg(0, ios::beg); delete[] fileBuffer; return 0; } if (memcpy_s(avBuffer, size, fileBuffer, bufferSize + START_CODE_SIZE) != EOK) { delete[] fileBuffer; cout << "Fatal: memcpy fail" << endl; return 0; } int64_t startPts = GetSystemTimeUs(); attr.pts = startPts; attr.size = bufferSize + START_CODE_SIZE; attr.offset = 0; int32_t result = OH_VideoDecoder_PushInputData(vdec_, index, attr); if (result != AV_ERR_OK) { errCount = errCount + 1; cout << "push input data failed,error:" << result << endl; } delete[] fileBuffer; frameCount_ = frameCount_ + 1; return 0; } void VDecNdkSample::OutputFuncTest() { SHA512_Init(&c); while (true) { if (!isRunning_.load()) { break; } OH_AVCodecBufferAttr attr; uint32_t index; unique_lock lock(signal_->outMutex_); signal_->outCond_.wait(lock, [this]() { return signal_->outIdxQueue_.size() > 0; }); if (!isRunning_.load()) { break; } index = signal_->outIdxQueue_.front(); attr = signal_->attrQueue_.front(); OH_AVMemory *buffer = signal_->outBufferQueue_.front(); signal_->outBufferQueue_.pop(); signal_->outIdxQueue_.pop(); signal_->attrQueue_.pop(); lock.unlock(); if (attr.flags == AVCODEC_BUFFER_FLAGS_EOS) { SHA512_Final(md, &c); OPENSSL_cleanse(&c, sizeof(c)); MdCompare(md, SHA512_DIGEST_LENGTH, fileSourcesha256); if (AFTER_EOS_DESTORY_CODEC) { (void)Stop(); Release(); } break; } ProcessOutputData(buffer, index); if (errCount > 0) { break; } } } void VDecNdkSample::ProcessOutputData(OH_AVMemory *buffer, uint32_t index) { if (!SF_OUTPUT) { uint32_t size = OH_AVMemory_GetSize(buffer); if (size >= DEFAULT_WIDTH * DEFAULT_HEIGHT * THREE >> 1) { uint8_t *cropBuffer = new uint8_t[size]; if (memcpy_s(cropBuffer, DEFAULT_WIDTH * DEFAULT_HEIGHT, OH_AVMemory_GetAddr(buffer), DEFAULT_WIDTH * DEFAULT_HEIGHT) != EOK) { cout << "Fatal: memory copy failed Y" << endl; } // copy UV uint32_t uvSize = size - DEFAULT_WIDTH * DEFAULT_HEIGHT; if (memcpy_s(cropBuffer + DEFAULT_WIDTH * DEFAULT_HEIGHT, uvSize, OH_AVMemory_GetAddr(buffer) + DEFAULT_WIDTH * DEFAULT_HEIGHT, uvSize) != EOK) { cout << "Fatal: memory copy failed UV" << endl; } SHA512_Update(&c, cropBuffer, size); delete[] cropBuffer; } if (OH_VideoDecoder_FreeOutputData(vdec_, index) != AV_ERR_OK) { cout << "Fatal: ReleaseOutputBuffer fail" << endl; errCount = errCount + 1; } } else { if (OH_VideoDecoder_RenderOutputData(vdec_, index) != AV_ERR_OK) { cout << "Fatal: RenderOutputBuffer fail" << endl; errCount = errCount + 1; } } } int32_t VDecNdkSample::state_EOS() { uint32_t index; unique_lock lock(signal_->inMutex_); signal_->inCond_.wait(lock, [this]() { return signal_->inIdxQueue_.size() > 0; }); index = signal_->inIdxQueue_.front(); signal_->inIdxQueue_.pop(); signal_->inBufferQueue_.pop(); lock.unlock(); OH_AVCodecBufferAttr attr; attr.pts = 0; attr.size = 0; attr.offset = 0; attr.flags = AVCODEC_BUFFER_FLAGS_EOS; return OH_VideoDecoder_PushInputData(vdec_, index, attr); } void VDecNdkSample::SetEOS(uint32_t index) { OH_AVCodecBufferAttr attr; attr.pts = 0; attr.size = 0; attr.offset = 0; attr.flags = AVCODEC_BUFFER_FLAGS_EOS; int32_t res = OH_VideoDecoder_PushInputData(vdec_, index, attr); cout << "OH_VideoDecoder_PushInputData EOS res: " << res << endl; } int32_t VDecNdkSample::Flush() { unique_lock inLock(signal_->inMutex_); clearIntqueue(signal_->inIdxQueue_); signal_->inCond_.notify_all(); inLock.unlock(); unique_lock outLock(signal_->outMutex_); clearIntqueue(signal_->outIdxQueue_); clearBufferqueue(signal_->attrQueue_); signal_->outCond_.notify_all(); outLock.unlock(); return OH_VideoDecoder_Flush(vdec_); } int32_t VDecNdkSample::Reset() { isRunning_.store(false); StopInloop(); StopOutloop(); ReleaseInFile(); return OH_VideoDecoder_Reset(vdec_); } int32_t VDecNdkSample::Release() { int ret = 0; if (vdec_ != nullptr) { ret = OH_VideoDecoder_Destroy(vdec_); vdec_ = nullptr; } if (signal_ != nullptr) { delete signal_; signal_ = nullptr; } return ret; } int32_t VDecNdkSample::Stop() { StopInloop(); clearIntqueue(signal_->outIdxQueue_); clearBufferqueue(signal_->attrQueue_); ReleaseInFile(); return OH_VideoDecoder_Stop(vdec_); } int32_t VDecNdkSample::Start() { return OH_VideoDecoder_Start(vdec_); } void VDecNdkSample::StopOutloop() { if (outputLoop_ != nullptr && outputLoop_->joinable()) { unique_lock lock(signal_->outMutex_); clearIntqueue(signal_->outIdxQueue_); clearBufferqueue(signal_->attrQueue_); signal_->outCond_.notify_all(); lock.unlock(); } } int32_t VDecNdkSample::SetParameter(OH_AVFormat *format) { return OH_VideoDecoder_SetParameter(vdec_, format); }