* Copyright (C) 2026 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 <arpa/inet.h>
#include <sys/time.h>
#include <sys/timeb.h>
#include <utility>
#include <vector>
#include <string>
#include <sstream>
#include <random>
#include "openssl/crypto.h"
#include "openssl/sha.h"
#include "videodec_api11_sample.h"
#include "video_types.h"
#include "nlohmann/json.hpp"
#include "meta_key.h"
using namespace OHOS;
using namespace OHOS::Media;
using namespace std;
using namespace nlohmann;
namespace {
constexpr int64_t NANOS_IN_SECOND = 1000000000L;
constexpr int64_t MICRO_IN_SECOND = 1000000L;
constexpr int64_t NANOS_IN_MICRO = 1000L;
constexpr int32_t ZERO = 0;
constexpr int32_t ONE = 1;
constexpr int32_t TWO = 2;
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 uint8_t H264_NALU_TYPE = 0x1f;
constexpr uint32_t START_CODE_SIZE = 4;
constexpr uint32_t MILLION = 1000000;
constexpr uint8_t START_CODE[START_CODE_SIZE] = {0, 0, 0, 1};
constexpr uint8_t SPS = 7;
constexpr uint8_t PPS = 8;
constexpr int32_t RES_CHANGE_TIME = 4;
constexpr int32_t CROP_INFO_SIZE = 2;
constexpr int32_t CROP_INFO[RES_CHANGE_TIME][CROP_INFO_SIZE] = {{621, 1103},
{1079, 1919}, {719, 1279}, {855, 1919}};
constexpr int32_t CROP_BOTTOM = 0;
constexpr int32_t CROP_RIGHT = 1;
constexpr int32_t DEFAULT_ANGLE = 90;
constexpr int32_t SYS_MAX_INPUT_SIZE = 1024 * 1024 * 24;
constexpr int32_t MIN_RANGE = 64;
constexpr int32_t MAX_RANGE = 1920;
constexpr int32_t MIN_FRANGE = 1;
constexpr int32_t MAX_FRANGE = 30;
constexpr int32_t EVEN_NUMBER = 2;
constexpr float DISPLAY_PRIMARY_REDX = 0.265;
constexpr float DISPLAY_PRIMARY_REDY = 0.69;
constexpr float DISPLAY_PRIMARY_GREENX = 0.15;
constexpr float DISPLAY_PRIMARY_GREENY = 0.06;
constexpr float DISPLAY_PRIMARY_BLUEX = 0.68;
constexpr float DISPLAY_PRIMARY_BLUEY = 0.32;
constexpr float WHITEPOINTX = 0.3127;
constexpr float WHITEPOINTY = 0.329;
constexpr float MINLUMINANCE = 0.0005;
constexpr float MAXLUMINANCE = 100;
SHA512_CTX g_c;
uint8_t g_md[SHA512_DIGEST_LENGTH];
VDecAPI11Sample *dec_sample = nullptr;
void clearIntqueue(std::queue<uint32_t> &q)
{
std::queue<uint32_t> empty;
swap(empty, q);
}
void clearBufferqueue(std::queue<OH_AVCodecBufferAttr> &q)
{
std::queue<OH_AVCodecBufferAttr> empty;
swap(empty, q);
}
}
class ConsumerListenerBuffer : public IBufferConsumerListener {
public:
ConsumerListenerBuffer(sptr<Surface> cs, std::string_view name) : cs(cs) {};
~ConsumerListenerBuffer() {}
void OnBufferAvailable() override
{
sptr<SurfaceBuffer> buffer;
int32_t flushFence;
cs->AcquireBuffer(buffer, flushFence, timestamp, damage);
cs->ReleaseBuffer(buffer, -1);
}
private:
int64_t timestamp = 0;
Rect damage = {};
sptr<Surface> cs {nullptr};
};
VDecAPI11Sample::~VDecAPI11Sample()
{
for (int i = 0; i < MAX_SURF_NUM; i++) {
if (nativeWindow[i]) {
OH_NativeWindow_DestroyNativeWindow(nativeWindow[i]);
nativeWindow[i] = nullptr;
}
}
Stop();
Release();
}
void VdecAPI11Error(OH_AVCodec *codec, int32_t errorCode, void *userData)
{
cout << "Error errorCode=" << errorCode << endl;
}
void VdecAPI11FormatChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData)
{
int32_t stride = 0;
int32_t sliceHeight = 0;
int32_t picWidth = 0;
int32_t picHeight = 0;
int currentFormat = 0;
OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_STRIDE, &stride);
OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_SLICE_HEIGHT, &sliceHeight);
OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_PIC_WIDTH, &picWidth);
OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_PIC_HEIGHT, &picHeight);
OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_NATIVE_BUFFER_FORMAT, ¤tFormat);
std::cout << "-----------------------------------------------------currentFormat:" << currentFormat << std::endl;
dec_sample->stride_ = stride;
dec_sample->sliceHeight_ = sliceHeight;
dec_sample->picWidth_ = picWidth;
dec_sample->picHeight_ = picHeight;
dec_sample->DEFAULT_WIDTH = picWidth;
dec_sample->DEFAULT_HEIGHT = picHeight;
dec_sample->onStreamChangedKey = currentFormat;
if (dec_sample->isResChangeStream) {
static int32_t resCount = 0;
int32_t cropBottom = 0;
int32_t cropRight = 0;
OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_CROP_BOTTOM, &cropBottom);
OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_CROP_RIGHT, &cropRight);
if (cropBottom != CROP_INFO[resCount][CROP_BOTTOM] || cropRight != CROP_INFO[resCount][CROP_RIGHT]) {
dec_sample->errCount++;
}
if (stride <= 0 || sliceHeight <= 0) {
dec_sample->errCount++;
}
resCount++;
}
}
void VdecAPI11InputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *data, void *userData)
{
if (dec_sample->isFlushing_) {
return;
}
if (dec_sample->inputCallbackFlush && dec_sample->outCount > 1) {
dec_sample->Flush();
cout << "OH_VideoDecoder_Flush end" << endl;
dec_sample->isRunning_.store(false);
dec_sample->signal_->inCond_.notify_all();
dec_sample->signal_->outCond_.notify_all();
return;
}
if (dec_sample->inputCallbackStop && dec_sample->outCount > 1) {
OH_VideoDecoder_Stop(codec);
cout << "OH_VideoDecoder_Stop end" << endl;
dec_sample->isRunning_.store(false);
dec_sample->signal_->inCond_.notify_all();
dec_sample->signal_->outCond_.notify_all();
return;
}
VDecAPI11Signal *signal = static_cast<VDecAPI11Signal *>(userData);
unique_lock<mutex> lock(signal->inMutex_);
signal->inIdxQueue_.push(index);
signal->inBufferQueue_.push(data);
signal->inCond_.notify_all();
}
void VdecAPI11OutputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *data, void *userData)
{
if (dec_sample->isFlushing_) {
return;
}
if (dec_sample->outputCallbackFlush && dec_sample->outCount > 1) {
dec_sample->Flush();
cout << "OH_VideoDecoder_Flush end" << endl;
dec_sample->isRunning_.store(false);
dec_sample->signal_->inCond_.notify_all();
dec_sample->signal_->outCond_.notify_all();
return;
}
if (dec_sample->outputCallbackStop && dec_sample->outCount > 1) {
OH_VideoDecoder_Stop(codec);
cout << "OH_VideoDecoder_Stop end" << endl;
dec_sample->isRunning_.store(false);
dec_sample->signal_->inCond_.notify_all();
dec_sample->signal_->outCond_.notify_all();
return;
}
VDecAPI11Signal *signal = static_cast<VDecAPI11Signal *>(userData);
unique_lock<mutex> lock(signal->outMutex_);
signal->outIdxQueue_.push(index);
signal->outBufferQueue_.push(data);
signal->outCond_.notify_all();
}
void VDecAPI11Sample::Flush_buffer()
{
unique_lock<mutex> inLock(signal_->inMutex_);
clearIntqueue(signal_->inIdxQueue_);
std::queue<OH_AVBuffer *> empty;
swap(empty, signal_->inBufferQueue_);
signal_->inCond_.notify_all();
inLock.unlock();
unique_lock<mutex> outLock(signal_->outMutex_);
clearIntqueue(signal_->outIdxQueue_);
clearBufferqueue(signal_->attrQueue_);
signal_->outCond_.notify_all();
outLock.unlock();
}
std::vector<uint8_t> VDecAPI11Sample::LoadHashFile()
{
std::ifstream f("/data/test/media/hash_val.json", ios::in);
std::vector<uint8_t> ret;
if (f) {
json data = json::parse(f);
filesystem::path filePath = INP_DIR;
std::string pixFmt = defualtPixelFormat == AV_PIXEL_FORMAT_NV12 ? "nv12" : "nv21";
std::string fileName = filePath.filename();
std::string hashValue = "";
if (enableDecodingOrder) {
hashValue = data[fileName.c_str()]["dec_order"];
} else {
hashValue = data[fileName.c_str()][pixFmt];
}
std::stringstream ss(hashValue);
std::string item;
while (getline(ss, item, ',')) {
if (!item.empty()) {
ret.push_back(stol(item, nullptr, SIXTEEN));
}
}
}
return ret;
}
static void DumpHashValue(std::vector<uint8_t> &srcHashVal, uint8_t outputHashVal[])
{
printf("--------------output hash value----------------\n");
for (int i = 1; i < SHA512_DIGEST_LENGTH + 1; i++) {
printf("%02x,", outputHashVal[i - 1]);
if (i % SIXTEEN == 0) {
printf("\n");
}
}
printf("--------------standard hash value----------------\n");
for (int i = 1; i < (SHA512_DIGEST_LENGTH + 1) && i <= srcHashVal.size(); i++) {
printf("%02x,", srcHashVal[i - 1]);
if (i % SIXTEEN == 0) {
printf("\n");
}
}
}
bool VDecAPI11Sample::MdCompare(uint8_t source[])
{
if (!NEED_MD5_COMPARE) {
return true;
}
std::vector<uint8_t> srcHashVal = LoadHashFile();
DumpHashValue(srcHashVal, source);
if (srcHashVal.size() != SHA512_DIGEST_LENGTH) {
cout << "get hash value failed, size" << srcHashVal.size() << endl;
return false;
}
for (int32_t i = 0; i < SHA512_DIGEST_LENGTH; i++) {
if (source[i] != srcHashVal[i]) {
cout << "decoded hash value mismatch" << endl;
return false;
}
}
return true;
}
int32_t HighRand()
{
std::mt19937 rng(std::random_device{}());
std::uniform_int_distribution<> dis(MIN_RANGE, MAX_RANGE);
int HRand = dis(rng);
if (HRand % EVEN_NUMBER != 0) {
HRand = HRand + 1;
}
cout << "HRand is = " << HRand << endl;
return HRand;
}
int32_t FrameRand()
{
std::mt19937 rng(std::random_device{}());
std::uniform_int_distribution<> dis(MIN_FRANGE, MAX_FRANGE);
int FRand = dis(rng);
if (FRand % EVEN_NUMBER != 0) {
FRand = FRand + 1;
}
cout << "FRand is = " << FRand << endl;
return FRand;
}
int32_t WidthRand()
{
std::mt19937 rng(std::random_device{}());
std::uniform_int_distribution<> dis(MIN_RANGE, MAX_RANGE);
int WRand = dis(rng);
if (WRand % EVEN_NUMBER != 0) {
WRand = WRand + 1;
}
cout << "WRand is = " << WRand << endl;
return WRand;
}
int64_t VDecAPI11Sample::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;
}
void VDecAPI11Sample::ConfigHDRMetadata(OH_AVFormat *format)
{
if (hdrColorInfo) {
OH_AVFormat_SetIntValue(format, OH_MD_KEY_RANGE_FLAG, 0);
OH_AVFormat_SetIntValue(format, OH_MD_KEY_COLOR_PRIMARIES, COLOR_PRIMARY_BT2020);
OH_AVFormat_SetIntValue(format, OH_MD_KEY_TRANSFER_CHARACTERISTICS, TRANSFER_CHARACTERISTIC_HLG);
OH_AVFormat_SetIntValue(format, OH_MD_KEY_MATRIX_COEFFICIENTS, MATRIX_COEFFICIENT_BT2020_NCL);
}
if (hdrStaticMetadata) {
Plugins::Smpte2086 smpte;
smpte.displayPrimaryRedX = DISPLAY_PRIMARY_REDX;
smpte.displayPrimaryRedY = DISPLAY_PRIMARY_REDY;
smpte.displayPrimaryGreenX = DISPLAY_PRIMARY_GREENX;
smpte.displayPrimaryGreenY = DISPLAY_PRIMARY_GREENY;
smpte.displayPrimaryBlueX = DISPLAY_PRIMARY_BLUEX;
smpte.displayPrimaryBlueY = DISPLAY_PRIMARY_BLUEY;
smpte.whitePointX = WHITEPOINTX;
smpte.whitePointY = WHITEPOINTY;
smpte.minLuminance = MINLUMINANCE;
smpte.maxLuminance = MAXLUMINANCE;
OH_AVFormat_SetBuffer(format, OHOS::Media::Tag::VIDEO_STATIC_METADATA_SMPT2086, (uint8_t*)&smpte,
sizeof(Plugins::Smpte2086));
Plugins::Cta861 cta;
cta.maxContentLightLevel = 0;
cta.maxFrameAverageLightLevel = 0;
OH_AVFormat_SetBuffer(format, OHOS::Media::Tag::VIDEO_STATIC_METADATA_CTA861, (uint8_t*)&cta,
sizeof(Plugins::Cta861));
}
}
void VDecAPI11Sample::ConfigureVideoDecoder2(OH_AVFormat *format)
{
if (useHDRSource) {
(void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, DEFAULT_PROFILE);
}
if (setTransform) {
(void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_TRANSFORM_TYPE, DEFAULT_TRANSFORM);
cout << "config transform: " << DEFAULT_TRANSFORM << endl;
}
if (retentionModeSet) {
(void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_DECODER_FRAME_RETENTION_MODE, defaultRetentionMode);
}
if (retentionRatioSet) {
(void)OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_VIDEO_DECODER_FRAME_RETENTION_RATIO,
defaultRetentionRatio);
}
if (decoderSpeedSet) {
(void)OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_VIDEO_DECODER_SPEED, defaultDecoderSpeed);
}
if (NV21_FLAG) {
(void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV21);
}
(void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_ENABLE_SYNC_MODE, enableSyncMode);
(void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_DECODER_BLANK_FRAME_ON_SHUTDOWN, enbleBlankFrame);
}
int32_t VDecAPI11Sample::ConfigureVideoDecoder()
{
if (autoSwitchSurface && needAutoSwitch) {
switchSurfaceFlag = (switchSurfaceFlag == 1) ? 0 : 1;
if (OH_VideoDecoder_SetSurface(vdec_, nativeWindow[switchSurfaceFlag]) != AV_ERR_INVALID_STATE) {
errCount++;
}
}
if (outputYuvFlag) {
outFile_ = make_unique<ofstream>();
outFile_->open(OUT_DIR, ios::binary);
}
OH_AVFormat *format = OH_AVFormat_Create();
if (format == nullptr) {
cout << "Fatal: Failed to create format" << endl;
return AV_ERR_UNKNOWN;
}
if (maxInputSize != 0) {
(void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, maxInputSize);
}
ConfigHDRMetadata(format);
(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, defualtPixelFormat);
(void)OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, DEFAULT_FRAME_RATE);
(void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_DECODER_OUTPUT_IN_DECODING_ORDER, enableDecodingOrder);
ConfigureVideoDecoder2(format);
int ret = OH_VideoDecoder_Configure(vdec_, format);
OH_AVFormat_Destroy(format);
return ret;
}
void VDecAPI11Sample::CreateSurface()
{
cs[0] = Surface::CreateSurfaceAsConsumer();
sptr<IBufferConsumerListener> listener = new ConsumerListenerBuffer(cs[0], OUT_DIR);
cs[0]->RegisterConsumerListener(listener);
auto p = cs[0]->GetProducer();
ps[0] = Surface::CreateSurfaceAsProducer(p);
nativeWindow[0] = CreateNativeWindowFromSurface(&ps[0]);
if (autoSwitchSurface || expireSurface) {
cs[1] = Surface::CreateSurfaceAsConsumer();
sptr<IBufferConsumerListener> listener2 = new ConsumerListenerBuffer(cs[1], OUT_DIR2);
cs[1]->RegisterConsumerListener(listener2);
auto p2 = cs[1]->GetProducer();
ps[1] = Surface::CreateSurfaceAsProducer(p2);
nativeWindow[1] = CreateNativeWindowFromSurface(&ps[1]);
}
}
int32_t VDecAPI11Sample::RunVideoDec_Surface(string codeName)
{
SF_OUTPUT = true;
int err = AV_ERR_OK;
CreateSurface();
if (!nativeWindow[0]) {
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;
}
if (setTransform && autoSwitchSurface) {
beforeSwitchTransform = GetSurfaceTransform(switchSurfaceFlag);
}
err = ConfigureVideoDecoder();
if (err != AV_ERR_OK) {
cout << "Failed to configure video decoder" << endl;
Release();
return err;
}
err = OH_VideoDecoder_SetSurface(vdec_, nativeWindow[0]);
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 VDecAPI11Sample::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 VDecAPI11Sample::SetVideoDecoderCallback()
{
signal_ = new VDecAPI11Signal();
if (signal_ == nullptr) {
cout << "Failed to new VDecAPI11Signal" << endl;
return AV_ERR_UNKNOWN;
}
cb_.onError = VdecAPI11Error;
cb_.onStreamChanged = VdecAPI11FormatChanged;
cb_.onNeedInputBuffer = VdecAPI11InputDataReady;
cb_.onNewOutputBuffer = VdecAPI11OutputDataReady;
return OH_VideoDecoder_RegisterCallback(vdec_, cb_, static_cast<void *>(signal_));
}
void VDecAPI11Sample::ReleaseInFile()
{
if (inFile_ != nullptr) {
if (inFile_->is_open()) {
inFile_->close();
}
inFile_.reset();
inFile_ = nullptr;
}
if (iptMultiStreamsBuf_ != nullptr) {
free(iptMultiStreamsBuf_);
iptMultiStreamsBuf_ = nullptr;
}
}
void VDecAPI11Sample::StopInloop()
{
if (inputLoop_ != nullptr && inputLoop_->joinable()) {
unique_lock<mutex> lock(signal_->inMutex_);
clearIntqueue(signal_->inIdxQueue_);
isRunning_.store(false);
signal_->inCond_.notify_all();
lock.unlock();
inputLoop_->join();
inputLoop_.reset();
}
}
int32_t VDecAPI11Sample::CreateVideoDecoder(string codeName)
{
vdec_ = OH_VideoDecoder_CreateByName(codeName.c_str());
dec_sample = this;
return vdec_ == nullptr ? AV_ERR_UNKNOWN : AV_ERR_OK;
}
int32_t VDecAPI11Sample::StartDecoder()
{
isRunning_.store(true);
inFile_ = make_unique<ifstream>();
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;
}
if (InitReadFileNals() != 0) {
return AV_ERR_UNKNOWN;
}
inputLoop_ = make_unique<thread>(&VDecAPI11Sample::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<thread>(&VDecAPI11Sample::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;
}
int32_t VDecAPI11Sample::StartSyncDecoder()
{
isRunning_.store(true);
inFile_ = make_unique<ifstream>();
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;
}
if (signal_ == nullptr) {
signal_ = new VDecAPI11Signal();
}
if (InitReadFileNals() != 0) {
return AV_ERR_UNKNOWN;
}
inputLoop_ = make_unique<thread>(&VDecAPI11Sample::SyncInputFunc, 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<thread>(&VDecAPI11Sample::SyncOutputFunc, 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;
}
int32_t VDecAPI11Sample::StartVideoDecoder()
{
isRunning_.store(true);
if (PREPARE_FLAG) {
int res = OH_VideoDecoder_Prepare(vdec_);
if (res != AV_ERR_OK) {
cout << "Failed to start codec, prepare failed! " << res << endl;
isRunning_.store(false);
ReleaseInFile();
Release();
return res;
}
}
int ret = OH_VideoDecoder_Start(vdec_);
if (ret != AV_ERR_OK) {
cout << "Failed to start codec" << endl;
isRunning_.store(false);
ReleaseInFile();
Release();
return ret;
}
if (enableSyncMode == 0) {
ret = StartDecoder();
} else {
ret = StartSyncDecoder();
}
return ret;
}
void VDecAPI11Sample::testAPI()
{
cs[0] = Surface::CreateSurfaceAsConsumer();
sptr<IBufferConsumerListener> listener = new ConsumerListenerBuffer(cs[0], OUT_DIR);
cs[0]->RegisterConsumerListener(listener);
auto p = cs[0]->GetProducer();
ps[0] = Surface::CreateSurfaceAsProducer(p);
nativeWindow[0] = CreateNativeWindowFromSurface(&ps[0]);
OH_VideoDecoder_SetSurface(vdec_, nativeWindow[0]);
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 VDecAPI11Sample::WaitForEOS()
{
if (!AFTER_EOS_DESTORY_CODEC && inputLoop_ && inputLoop_->joinable()) {
inputLoop_->join();
}
if (outputLoop_ && outputLoop_->joinable()) {
outputLoop_->join();
}
}
void VDecAPI11Sample::InFuncTest()
{
const int REFRESH_INTERVAL = 10;
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();
inFile_->clear();
inFile_->seekg(0, ios::beg);
OH_VideoDecoder_Start(vdec_);
}
if (INPUT_STREAM_TYPE == Input_Stream_Type_000001 && (outFrameCount % (INPUT_NAL_NUM * REFRESH_INTERVAL) == 0)) {
OH_VideoDecoder_Flush(vdec_);
Flush_buffer();
OH_VideoDecoder_Start(vdec_);
}
}
void VDecAPI11Sample::InputFuncTest()
{
bool flag = true;
static uint32_t inputCount = 0;
while (flag) {
inputCount++;
if (inputCount % DEFAULT_ANGLE == 0) {
ExpireSurface();
}
if (!isRunning_.load()) {
flag = false;
break;
}
InFuncTest();
uint32_t index;
unique_lock<mutex> lock(signal_->inMutex_);
signal_->inCond_.wait(lock, [this]() {
if (!isRunning_.load()) {
return true;
}
return signal_->inIdxQueue_.size() > 0 && !isFlushing_.load();
});
if (!isRunning_.load()) {
flag = false;
break;
}
index = signal_->inIdxQueue_.front();
auto buffer = signal_->inBufferQueue_.front();
signal_->inIdxQueue_.pop();
signal_->inBufferQueue_.pop();
if (!inFile_->eof()) {
int ret = PushData(index, buffer);
if (ret == 1) {
flag = false;
break;
}
}
lock.unlock();
if (sleepOnFPS) {
usleep(MICRO_IN_SECOND / (int32_t)DEFAULT_FRAME_RATE);
}
}
}
void VDecAPI11Sample::ExpireSurface()
{
if (!expireSurface) {
return;
}
static bool isSurfaceExpired = false;
static bool isSurfaceReset = false;
if (!isSurfaceExpired) {
OH_NativeWindow_DestroyNativeWindow(nativeWindow[0]);
nativeWindow[0] = nullptr;
cs[0] = nullptr;
ps[0] = nullptr;
isSurfaceExpired = true;
return;
}
if (isSurfaceExpired && !isSurfaceReset) {
if (OH_VideoDecoder_SetSurface(vdec_, nativeWindow[1]) != AV_ERR_OK) {
errCount++;
}
isSurfaceReset = true;
}
}
void VDecAPI11Sample::SyncInputFunc()
{
bool flag = true;
while (flag) {
if (!isRunning_.load()) {
flag = false;
break;
}
uint32_t index;
if (OH_VideoDecoder_QueryInputBuffer(vdec_, &index, syncInputWaitTime) != AV_ERR_OK) {
cout << "OH_VideoDecoder_QueryInputBuffer fail" << endl;
continue;
}
OH_AVBuffer *buffer = OH_VideoDecoder_GetInputBuffer(vdec_, index);
if (buffer == nullptr) {
cout << "OH_VideoDecoder_GetInputBuffer fail" << endl;
errCount = errCount + 1;
continue;
}
if (!inFile_->eof()) {
int ret = PushData(index, buffer);
if (ret == 1) {
flag = false;
break;
}
}
if (sleepOnFPS) {
usleep(MICRO_IN_SECOND / (int32_t)DEFAULT_FRAME_RATE);
}
}
}
int32_t VDecAPI11Sample::ReadFileAvccFrameLen(uint32_t index, uint32_t& bufferSize, OH_AVBuffer *buffer)
{
char ch[4] = {};
uint32_t zero = 0;
uint32_t one = 1;
uint32_t two = 2;
uint32_t three = 3;
(void)inFile_->read(ch, START_CODE_SIZE);
if (repeatRun && inFile_->eof()) {
static uint32_t repeat_count = 0;
inFile_->clear();
inFile_->seekg(0, ios::beg);
cout << "repeat run" << repeat_count << endl;
repeat_count++;
return 0;
}
if (inFile_->eof()) {
SetEOS(index, buffer);
return 1;
}
bufferSize = (uint32_t)(((ch[three] & 0xFF)) | ((ch[two] & 0xFF) << EIGHT) | ((ch[one] & 0xFF) << SIXTEEN) |
((ch[zero] & 0xFF) << TWENTY_FOUR));
if (useHDRSource) {
bufferSize = (uint32_t)(((ch[zero] & 0xFF)) | ((ch[one] & 0xFF) << EIGHT) | ((ch[two] & 0xFF) << SIXTEEN) |
((ch[three] & 0xFF) << TWENTY_FOUR));
}
return 0;
}
int32_t VDecAPI11Sample::ReadFileNalsFrame(uint32_t index, uint32_t& bufferSize, OH_AVBuffer *buffer)
{
const int READ_BAD_ERROR = -5;
const int READ_FAIL_ERROR = -4;
const int SEEK_FAILYRE = -3;
const int SIX_RE = -6;
if (iptMultiStreamsBuf_ == nullptr || inFile_ == nullptr) {
return -1;
}
if (inFile_->eof() || (startPos_ + START_CODE_SIZE) >= nFileSize_) {
SetEOS(index, buffer);
return 1;
}
char *inputBuffer = reinterpret_cast<char*>(iptMultiStreamsBuf_);
inFile_->seekg(startPos_, std::ios::beg);
uint64_t currentPos = inFile_->tellg();
if (currentPos != startPos_) {
std::cout << "telg fail " << strerror(errno) << ", file size" << nFileSize_
<< ", cur " << currentPos << std::endl;
return SEEK_FAILYRE;
}
uint64_t maxReadable = nFileSize_ - currentPos;
size_t bytesToRead = static_cast<size_t>(std::min(
static_cast<uint64_t>(DEFAULT_WIDTH * DEFAULT_HEIGHT * THREE >> 1), maxReadable));
inFile_->read(inputBuffer, bytesToRead);
uint64_t bytesRead = inFile_->gcount();
if (bytesRead == 0) {
if (inFile_->bad()) {
std::cout << "Read error: " << strerror(errno) << std::endl;
return READ_BAD_ERROR;
} else if (inFile_->fail()) {
std::cout << "Read error: " << strerror(errno) << std::endl;
return READ_FAIL_ERROR;
}
}
const uint32_t START_VALUE = 1;
const uint32_t START_SEPARATOR = 0xFF;
uint32_t startSeparator = ((inputBuffer[0] & START_SEPARATOR) << 24) | ((inputBuffer[1] & START_SEPARATOR) << 16)
| ((inputBuffer[2] & START_SEPARATOR) << 8) | (inputBuffer[3] & START_SEPARATOR);
if (startSeparator != START_VALUE) {
startSeparator = ((iptMultiStreamsBuf_[ZERO] & START_SEPARATOR) << SIXTEEN) |
((iptMultiStreamsBuf_[ONE] & START_SEPARATOR) << EIGHT) | (iptMultiStreamsBuf_[TWO] & START_SEPARATOR);
if (startSeparator != START_VALUE) {
return SIX_RE;
}
}
bufferSize = bytesRead;
return 0;
}
int32_t VDecAPI11Sample::PushData(uint32_t index, OH_AVBuffer *buffer)
{
OH_AVCodecBufferAttr attr;
if (BEFORE_EOS_INPUT && frameCount_ > TEN) {
SetEOS(index, buffer);
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;
}
uint32_t bufferSize = 0;
if ((INPUT_STREAM_TYPE != Input_Stream_Type_000001 ?
ReadFileAvccFrameLen(index, bufferSize, buffer) : ReadFileNalsFrame(index, bufferSize, buffer)) != 0) {
return 1;
}
if (bufferSize > DEFAULT_WIDTH * DEFAULT_HEIGHT * THREE >> 1) {
cout << "read bufferSize abnormal. buffersize = " << bufferSize << endl;
return 1;
}
return INPUT_STREAM_TYPE != Input_Stream_Type_000001 ?
SendData(bufferSize, index, buffer) : SendDataNals(bufferSize, index, buffer);
}
int32_t VDecAPI11Sample::CheckAndReturnBufferSize(OH_AVBuffer *buffer)
{
int32_t size = OH_AVBuffer_GetCapacity(buffer);
if ((maxInputSize < 0) && (size < 0)) {
errCount++;
} else if ((maxInputSize > 0) && (size > SYS_MAX_INPUT_SIZE)) {
errCount++;
}
return size;
}
uint32_t VDecAPI11Sample::SendData(uint32_t bufferSize, uint32_t index, OH_AVBuffer *buffer)
{
OH_AVCodecBufferAttr attr;
uint8_t *fileBuffer = new uint8_t[bufferSize + START_CODE_SIZE];
if (fileBuffer == nullptr) {
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(reinterpret_cast<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 = CheckAndReturnBufferSize(buffer);
if (size < bufferSize + START_CODE_SIZE) {
delete[] fileBuffer;
return 0;
}
uint8_t *avBuffer = OH_AVBuffer_GetAddr(buffer);
if (avBuffer == nullptr) {
inFile_->clear();
inFile_->seekg(0, ios::beg);
delete[] fileBuffer;
return 0;
}
if (memcpy_s(avBuffer, size, fileBuffer, bufferSize + START_CODE_SIZE) != EOK) {
delete[] fileBuffer;
return 0;
}
int64_t startPts = GetSystemTimeUs();
attr.pts = startPts;
attr.size = bufferSize + START_CODE_SIZE;
attr.offset = 0;
if (isRunning_.load()) {
OH_AVBuffer_SetBufferAttr(buffer, &attr);
OH_VideoDecoder_PushInputBuffer(vdec_, index) == AV_ERR_OK ? (0) : (errCount++);
frameCount_ = frameCount_ + 1;
if (enableSetParameter) {
SetParameterForSpeedPlayer();
}
SwitchSurfaceGetTransform();
}
delete[] fileBuffer;
return 0;
}
static int32_t H265DecLoadAU(uint8_t* pStream, uint32_t iStreamLen, uint32_t* pFrameLen)
{
uint32_t i;
uint32_t state = 0xffffffff;
bool bFrameStartFound = false;
const int frameHeaderSize = 4;
const int bitMask = 1 << 7;
const int eight = 8;
*pFrameLen = 0;
if (pStream == nullptr || iStreamLen <= frameHeaderSize) {
return -1;
}
for (i = 0; i < iStreamLen; i++) {
uint32_t maskedState = state & 0xFFFFFF7E;
if (maskedState >= 0x100 && maskedState <= 0x13E) {
if (!bFrameStartFound) {
bFrameStartFound = true;
state = (state << eight) | pStream[i];
continue;
}
if ((pStream[i + 1] & bitMask) == bitMask) {
*pFrameLen = i - frameHeaderSize;
return 0;
}
state = (state << eight) | pStream[i];
continue;
}
if (maskedState == 0x140 || maskedState == 0x142 ||
maskedState == 0x144 || maskedState == 0x14e ||
maskedState == 0x150) {
if (!bFrameStartFound) {
bFrameStartFound = true;
} else {
*pFrameLen = i - frameHeaderSize;
return 0;
}
}
state = (state << eight) | pStream[i];
}
*pFrameLen = i;
return (bFrameStartFound && iStreamLen == i) ? 0 : -1;
}
void VDecAPI11Sample::ReadNalsFromFixBuffer(uint32_t bufferSize, uint8_t nNALNum, uint32_t& consumeByteLen)
{
uint8_t* pDecStream = iptMultiStreamsBuf_;
uint32_t iInputLen = bufferSize;
bool bStreamEnd = false;
uint32_t uiStreamLen = 0;
uint32_t multiNumLen = 0;
uint8_t* multiNumFrmStart = pDecStream;
uint32_t numFrames = 0;
while (!bStreamEnd) {
bStreamEnd = H265DecLoadAU(pDecStream, iInputLen, &uiStreamLen);
if (bStreamEnd != 0) {
if (numFrames != 0) {
std::cout << "num frames " << iFrameIdxAU_ << ", frames len " << multiNumLen
<< ", remain len " << iInputLen << ",startPos_ " << startPos_ << std::endl;
}
break;
}
iFrameIdxAU_++;
if (numFrames == 0) {
multiNumFrmStart = pDecStream;
}
pDecStream += uiStreamLen;
iInputLen -= uiStreamLen;
multiNumLen += uiStreamLen;
numFrames++;
if (numFrames == nNALNum) {
break;
}
}
consumeByteLen = multiNumLen;
startPos_ += consumeByteLen;
return;
}
uint32_t VDecAPI11Sample::SendDataNals(uint32_t bufferSize, uint32_t index, OH_AVBuffer *buffer)
{
uint32_t consumeByteLen = 0;
ReadNalsFromFixBuffer(bufferSize, INPUT_NAL_NUM, consumeByteLen);
OH_AVCodecBufferAttr attr;
uint8_t *fileBuffer = iptMultiStreamsBuf_;
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 = CheckAndReturnBufferSize(buffer);
if (size < consumeByteLen) {
std::cout << "AVBuf size: " << size << "input size: " << bufferSize << std::endl;
return 1;
}
uint8_t *avBuffer = OH_AVBuffer_GetAddr(buffer);
if (avBuffer == nullptr) {
inFile_->clear();
inFile_->seekg(0, ios::beg);
std::cout << "AVBuf get addr error " << std::endl;
return 1;
}
int64_t startPts = GetSystemTimeUs();
attr.pts = startPts;
attr.size = consumeByteLen;
attr.offset = 0;
if (isRunning_.load()) {
OH_AVBuffer_SetBufferAttr(buffer, &attr);
OH_VideoDecoder_PushInputBuffer(vdec_, index) == AV_ERR_OK ? (0) : (errCount++);
frameCount_ = frameCount_ + 1;
SwitchSurfaceGetTransform();
}
return 0;
}
void VDecAPI11Sample::CheckOutputDescription()
{
OH_AVFormat *newFormat = OH_VideoDecoder_GetOutputDescription(vdec_);
if (newFormat != nullptr) {
int32_t cropTop = 0;
int32_t cropBottom = 0;
int32_t cropLeft = 0;
int32_t cropRight = 0;
int32_t stride = 0;
int32_t sliceHeight = 0;
int32_t picWidth = 0;
int32_t picHeight = 0;
OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_CROP_TOP, &cropTop);
OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_CROP_BOTTOM, &cropBottom);
OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_CROP_LEFT, &cropLeft);
OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_CROP_RIGHT, &cropRight);
OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_STRIDE, &stride);
OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_SLICE_HEIGHT, &sliceHeight);
OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_PIC_WIDTH, &picWidth);
OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_PIC_HEIGHT, &picHeight);
if (cropTop != expectCropTop || cropLeft != expectCropLeft) {
std::cout << "cropTop:" << cropTop << " cropBottom:" << cropBottom << " cropLeft:" << cropLeft <<std::endl;
errCount++;
}
if (stride <= 0 || sliceHeight <= 0) {
std::cout << "cropRight:" << cropRight << std::endl;
std::cout << "stride:" << stride << " sliceHeight:" << sliceHeight << std::endl;
errCount++;
}
if (picWidth != DEFAULT_WIDTH || picHeight != DEFAULT_HEIGHT) {
std::cout << "picWidth:" << picWidth << " picHeight:" << picHeight << std::endl;
errCount++;
}
} else {
errCount++;
}
OH_AVFormat_Destroy(newFormat);
}
void VDecAPI11Sample::AutoSwitchSurface()
{
if (autoSwitchSurface && needAutoSwitch) {
switchSurfaceFlag = (switchSurfaceFlag == 1) ? 0 : 1;
if (OH_VideoDecoder_SetSurface(vdec_, nativeWindow[switchSurfaceFlag]) != AV_ERR_OK) {
errCount++;
}
OH_AVFormat *format = OH_AVFormat_Create();
int32_t angle = DEFAULT_ANGLE * reinterpret_cast<int32_t>(switchSurfaceFlag);
(void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_ROTATION, angle);
OH_VideoDecoder_SetParameter(vdec_, format);
OH_AVFormat_Destroy(format);
}
}
int32_t VDecAPI11Sample::CheckAttrFlag(OH_AVCodecBufferAttr attr)
{
if (IS_FIRST_FRAME) {
GetStride();
IS_FIRST_FRAME = false;
}
if (needCheckOutputDesc) {
CheckOutputDescription();
needCheckOutputDesc = false;
}
if (attr.flags == AVCODEC_BUFFER_FLAGS_EOS) {
cout << "AVCODEC_BUFFER_FLAGS_EOS" << endl;
AutoSwitchSurface();
SHA512_Final(g_md, &g_c);
OPENSSL_cleanse(&g_c, sizeof(g_c));
if (!SF_OUTPUT && enableHash) {
if (!MdCompare(g_md)) {
errCount++;
}
}
return -1;
}
if (attr.flags == AVCODEC_BUFFER_FLAGS_CODEC_DATA) {
cout << "enc AVCODEC_BUFFER_FLAGS_CODEC_DATA" << attr.pts << endl;
return 0;
}
outFrameCount = outFrameCount + 1;
return 0;
}
void VDecAPI11Sample::GetStride()
{
OH_AVFormat *format = OH_VideoDecoder_GetOutputDescription(vdec_);
int32_t stride = 0;
int32_t sliceHeight = 0;
int32_t picWidth = 0;
int32_t picHeight = 0;
OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_STRIDE, &stride);
OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_SLICE_HEIGHT, &sliceHeight);
OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_PIC_WIDTH, &picWidth);
OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_PIC_HEIGHT, &picHeight);
dec_sample->stride_ = stride;
dec_sample->sliceHeight_ = sliceHeight;
dec_sample->picWidth_ = picWidth;
dec_sample->picHeight_ = picHeight;
OH_AVFormat_Destroy(format);
}
void VDecAPI11Sample::OutputFuncTest()
{
SHA512_Init(&g_c);
bool flag = true;
while (flag) {
if (!isRunning_.load()) {
flag = false;
break;
}
OH_AVCodecBufferAttr attr;
unique_lock<mutex> lock(signal_->outMutex_);
signal_->outCond_.wait(lock, [this]() {
if (!isRunning_.load()) {
return true;
}
return signal_->outIdxQueue_.size() > 0 && !isFlushing_.load();
});
if (!isRunning_.load()) {
flag = false;
break;
}
uint32_t index = signal_->outIdxQueue_.front();
OH_AVBuffer *buffer = signal_->outBufferQueue_.front();
signal_->outBufferQueue_.pop();
signal_->outIdxQueue_.pop();
if (OH_AVBuffer_GetBufferAttr(buffer, &attr) != AV_ERR_OK) {
errCount = errCount + 1;
}
if (CheckAttrFlag(attr) == -1) {
flag = false;
break;
}
ProcessOutputData(buffer, index);
lock.unlock();
if (errCount > 0) {
flag = false;
break;
}
}
}
void VDecAPI11Sample::SyncOutputFunc()
{
SHA512_Init(&g_c);
bool flag = true;
while (flag) {
if (!isRunning_.load()) {
flag = false;
break;
}
OH_AVCodecBufferAttr attr;
uint32_t index = 0;
if (OH_VideoDecoder_QueryOutputBuffer(vdec_, &index, syncOutputWaitTime) != AV_ERR_OK) {
cout << "OH_VideoDecoder_QueryOutputBuffer fail" << endl;
continue;
}
OH_AVBuffer *buffer = OH_VideoDecoder_GetOutputBuffer(vdec_, index);
if (buffer == nullptr) {
cout << "OH_VideoDecoder_GetOutputBuffer fail" << endl;
errCount = errCount + 1;
continue;
}
if (OH_AVBuffer_GetBufferAttr(buffer, &attr) != AV_ERR_OK) {
errCount = errCount + 1;
}
if (SyncOutputFuncEos(attr, index) != AV_ERR_OK) {
flag = false;
break;
}
ProcessOutputData(buffer, index);
if (errCount > 0) {
flag = false;
break;
}
}
}
int32_t VDecAPI11Sample::SyncOutputFuncEos(OH_AVCodecBufferAttr attr, uint32_t index)
{
if (CheckAttrFlag(attr) == -1) {
if (queryInputBufferEOS) {
OH_VideoDecoder_QueryInputBuffer(vdec_, &index, 0);
OH_VideoDecoder_QueryInputBuffer(vdec_, &index, MILLION);
OH_VideoDecoder_QueryInputBuffer(vdec_, &index, -1);
}
if (queryOutputBufferEOS) {
OH_VideoDecoder_QueryOutputBuffer(vdec_, &index, 0);
OH_VideoDecoder_QueryOutputBuffer(vdec_, &index, MILLION);
OH_VideoDecoder_QueryOutputBuffer(vdec_, &index, -1);
}
return AV_ERR_UNKNOWN;
}
return AV_ERR_OK;
}
void VDecAPI11Sample::ProcessOutputData(OH_AVBuffer *buffer, uint32_t index)
{
GetVideoSupportedPixelFormats();
GetFormatKey();
if (!SF_OUTPUT) {
uint8_t *bufferAddr = OH_AVBuffer_GetAddr(buffer);
uint32_t cropSize = (picWidth_ * picHeight_ * THREE) >> 1;
uint8_t *cropBuffer = new uint8_t[cropSize];
uint8_t *copyPos = cropBuffer;
for (int32_t i = 0; i < picHeight_; i++) {
memcpy_s(copyPos, picWidth_, bufferAddr, picWidth_);
bufferAddr += stride_;
copyPos += picWidth_;
}
bufferAddr += (sliceHeight_ - picHeight_) * stride_;
for (int32_t i = 0; i < picHeight_ >> 1; i++) {
memcpy_s(copyPos, picWidth_, bufferAddr, picWidth_);
bufferAddr += stride_;
copyPos += picWidth_;
}
SHA512_Update(&g_c, cropBuffer, cropSize);
delete[] cropBuffer;
if (outFile_ != nullptr) {
OH_AVCodecBufferAttr attr;
OH_AVBuffer_GetBufferAttr(buffer, &attr);
outFile_->write(reinterpret_cast<char*>(OH_AVBuffer_GetAddr(buffer)), attr.size);
}
if (OH_VideoDecoder_FreeOutputBuffer(vdec_, index) != AV_ERR_OK) {
cout << "Fatal: ReleaseOutputBuffer fail" << endl;
errCount = errCount + 1;
} else {
++outCount;
}
} else {
if (rsAtTime) {
RenderOutAtTime(index);
} else {
if (OH_VideoDecoder_RenderOutputBuffer(vdec_, index) != AV_ERR_OK) {
cout << "Fatal: RenderOutputBuffer fail" << endl;
errCount = errCount + 1;
} else {
++outCount;
}
}
}
}
void VDecAPI11Sample::RenderOutAtTime(uint32_t index)
{
if (isAPI) {
OH_AVErrCode code = OH_VideoDecoder_RenderOutputBufferAtTime(vdec_, index, -100000000);
if (code != AV_ERR_OK) {
cout << "Fatal: RenderOutputBufferAtTime fail" << endl;
errCount = code;
}
} else {
int32_t usTimeNum = 1000;
int32_t msTimeNum = 1000000;
if (renderTimestampNs == 0) {
renderTimestampNs = GetSystemTimeUs() / usTimeNum;
}
renderTimestampNs = renderTimestampNs + (usTimeNum / DEFAULT_FRAME_RATE * msTimeNum);
OH_AVErrCode code = OH_VideoDecoder_RenderOutputBufferAtTime(vdec_, index, renderTimestampNs);
if (code != AV_ERR_OK) {
cout << "Fatal: RenderOutputBufferAtTime fail" << endl;
errCount = code;
}
}
}
int32_t VDecAPI11Sample::state_EOS()
{
uint32_t index;
unique_lock<mutex> 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();
return OH_VideoDecoder_PushInputBuffer(vdec_, index);
}
void VDecAPI11Sample::SetEOS(uint32_t index, OH_AVBuffer *buffer)
{
OH_AVCodecBufferAttr attr;
attr.pts = 0;
attr.size = 0;
attr.offset = 0;
attr.flags = AVCODEC_BUFFER_FLAGS_EOS;
OH_AVBuffer_SetBufferAttr(buffer, &attr);
int32_t res = OH_VideoDecoder_PushInputBuffer(vdec_, index);
cout << "OH_VideoDecoder_PushInputBuffer EOS res: " << res << endl;
}
int32_t VDecAPI11Sample::Flush()
{
isFlushing_.store(true);
unique_lock<mutex> inLock(signal_->inMutex_);
clearIntqueue(signal_->inIdxQueue_);
signal_->inCond_.notify_all();
inLock.unlock();
unique_lock<mutex> outLock(signal_->outMutex_);
clearIntqueue(signal_->outIdxQueue_);
clearBufferqueue(signal_->attrQueue_);
signal_->outCond_.notify_all();
outLock.unlock();
isRunning_.store(false);
int32_t ret = OH_VideoDecoder_Flush(vdec_);
isFlushing_.store(false);
return ret;
}
int32_t VDecAPI11Sample::Reset()
{
isRunning_.store(false);
StopInloop();
StopOutloop();
ReleaseInFile();
return OH_VideoDecoder_Reset(vdec_);
}
int32_t VDecAPI11Sample::Release()
{
int ret = 0;
if (vdec_ != nullptr) {
ret = OH_VideoDecoder_Destroy(vdec_);
vdec_ = nullptr;
}
if (outFile_ != nullptr) {
if (outFile_->is_open()) {
outFile_->close();
}
outFile_.reset();
outFile_ = nullptr;
}
if (signal_ != nullptr) {
delete signal_;
signal_ = nullptr;
}
return ret;
}
int32_t VDecAPI11Sample::Stop()
{
StopInloop();
StopOutloop();
ReleaseInFile();
return OH_VideoDecoder_Stop(vdec_);
}
int32_t VDecAPI11Sample::Prepare()
{
return OH_VideoDecoder_Prepare(vdec_);
}
int32_t VDecAPI11Sample::Start()
{
isRunning_.store(true);
return OH_VideoDecoder_Start(vdec_);
}
void VDecAPI11Sample::StopOutloop()
{
if (outputLoop_ != nullptr && outputLoop_->joinable()) {
unique_lock<mutex> lock(signal_->outMutex_);
clearIntqueue(signal_->outIdxQueue_);
clearBufferqueue(signal_->attrQueue_);
isRunning_.store(false);
signal_->outCond_.notify_all();
lock.unlock();
outputLoop_->join();
outputLoop_.reset();
}
}
int32_t VDecAPI11Sample::SetParameter(OH_AVFormat *format)
{
return OH_VideoDecoder_SetParameter(vdec_, format);
}
int32_t VDecAPI11Sample::SwitchSurface()
{
int32_t ret = OH_VideoDecoder_SetSurface(vdec_, nativeWindow[switchSurfaceFlag]);
switchSurfaceFlag = (switchSurfaceFlag == 1) ? 0 : 1;
cout << "manual switch surf "<< switchSurfaceFlag << endl;
return ret;
}
int32_t VDecAPI11Sample::RepeatCallSetSurface()
{
for (int i = 0; i < REPEAT_CALL_TIME; i++) {
switchSurfaceFlag = (switchSurfaceFlag == 1) ? 0 : 1;
int32_t ret = OH_VideoDecoder_SetSurface(vdec_, nativeWindow[switchSurfaceFlag]);
if (ret != AV_ERR_OK && ret != AV_ERR_OPERATE_NOT_PERMIT && ret != AV_ERR_INVALID_STATE) {
return AV_ERR_OPERATE_NOT_PERMIT;
}
}
return AV_ERR_OK;
}
int32_t VDecAPI11Sample::DecodeSetSurface()
{
CreateSurface();
return OH_VideoDecoder_SetSurface(vdec_, nativeWindow[0]);
}
void VDecAPI11Sample::FlushStatus()
{
StopInloop();
StopOutloop();
Flush_buffer();
}
void VDecAPI11Sample::GetVideoSupportedPixelFormats()
{
if (!isGetVideoSupportedPixelFormats || isGetVideoSupportedPixelFormatsNum != 0) {
return;
}
OH_AVCapability *capability = OH_AVCodec_GetCapability(avcodecMimeType, isEncoder);
OH_AVCapability_GetVideoSupportedNativeBufferFormats(capability, &pixlFormats, &pixlFormatNum);
std::cout << "pixlFormats:" << *pixlFormats << "pixlFormatNum:" << pixlFormatNum << std::endl;
isGetVideoSupportedPixelFormatsNum++;
}
void VDecAPI11Sample::GetFormatKey()
{
if (!isGetFormatKey || isGetFormatKeyNum != 0) {
return;
}
OH_AVFormat *format = OH_AVFormat_Create();
OH_VideoDecoder_Configure(vdec_, format);
format = OH_VideoDecoder_GetOutputDescription(vdec_);
OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_NATIVE_BUFFER_FORMAT, &firstCallBackKey);
OH_AVFormat_Destroy(format);
std::cout << "firstCallBackKey:" << firstCallBackKey << std::endl;
isGetFormatKeyNum++;
}
int32_t VDecAPI11Sample::SetConfigTransform()
{
int32_t ret = ConfigureVideoDecoder();
if (ret != AV_ERR_OK) {
cout << "config failed" << endl;
return ret;
}
ret = OH_VideoDecoder_SetSurface(vdec_, nativeWindow[switchSurfaceFlag]);
if (ret != AV_ERR_OK) {
cout << "Failed to set surface" << endl;
return ret;
}
ret = OH_VideoDecoder_Prepare(vdec_);
if (ret != AV_ERR_OK) {
cout << "Failed to start codec, prepare failed! " << ret << endl;
Release();
return ret;
}
ret = OH_VideoDecoder_Start(vdec_);
if (ret != AV_ERR_OK) {
cout << "Failed to start codec" << endl;
Release();
return ret;
}
return AV_ERR_OK;
}
int32_t VDecAPI11Sample::SetParameterTransform()
{
int32_t ret = SetConfigTransform();
if (ret != AV_ERR_OK) {
return ret;
}
ret = SetParameter();
if (ret != AV_ERR_OK) {
cout << "set parameter failed" << endl;
return ret;
}
return ret;
}
int32_t VDecAPI11Sample::SetSurface()
{
return OH_VideoDecoder_SetSurface(vdec_, nativeWindow[switchSurfaceFlag]);
}
int32_t VDecAPI11Sample::SetParameter()
{
OH_AVFormat *format = OH_AVFormat_Create();
(void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_TRANSFORM_TYPE, DEFAULT_TRANSFORM);
cout << "set parameter transform: " << DEFAULT_TRANSFORM << endl;
int32_t ret = OH_VideoDecoder_SetParameter(vdec_, format);
OH_AVFormat_Destroy(format);
return ret;
}
int32_t VDecAPI11Sample::GetSurfaceTransform(int32_t surfaceFlag)
{
int32_t transform = -1;
int32_t ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow[surfaceFlag], GET_TRANSFORM, &transform);
if (ret != AV_ERR_OK) {
cout << "get transform failed, ret = " << ret << endl;
return ret;
}
cout << "get " << surfaceFlag << " surface transform: " << transform << endl;
return transform;
}
void VDecAPI11Sample::SwitchSurfaceGetTransform()
{
if (autoSwitchSurface && (frameCount_ % (int32_t)DEFAULT_FRAME_RATE == 0)) {
switchSurfaceFlag = (switchSurfaceFlag == 1) ? 0 : 1;
OH_VideoDecoder_SetSurface(vdec_, nativeWindow[switchSurfaceFlag]) == AV_ERR_OK ? (0) : (errCount++);
if (setTransform) {
afterSwitchTransform = GetSurfaceTransform(0);
autoSwitchSurface = false;
cout << "switchSurfaceFlag: " << switchSurfaceFlag << endl;
GetSurfaceTransform(switchSurfaceFlag) == DEFAULT_TRANSFORM ? (0) : (errCount++);
DEFAULT_TRANSFORM = NATIVEBUFFER_FLIP_V_ROT270;
SetParameter() == AV_ERR_OK ? (0) : (errCount++);
GetSurfaceTransform(switchSurfaceFlag) == DEFAULT_TRANSFORM ? (0) : (errCount++);
}
}
}
int32_t VDecAPI11Sample::InitReadFileNals()
{
const int MAIN_NAL_NUM = 0;
const int MAX_NAL_NUM = 4;
if (INPUT_STREAM_TYPE != Input_Stream_Type_000001) {
return 0;
}
if (INPUT_NAL_NUM == MAIN_NAL_NUM || INPUT_NAL_NUM >= MAX_NAL_NUM) {
std::cout << "input NAL num error" << std::endl;
return 1;
}
if (!inFile_->is_open()) {
std::cout << "file not open" << std::endl;
return 1;
}
inFile_->seekg(0, ios::end);
nFileSize_ = inFile_->tellg();
inFile_->seekg(0, std::ios::beg);
startPos_ = 0;
iptMultiStreamsBuf_ = (uint8_t*)malloc(DEFAULT_WIDTH * DEFAULT_HEIGHT * THREE >> 1);
if (iptMultiStreamsBuf_ == NULL) {
std::cout << "malloc fail: " << strerror(errno) << std::endl;
return 1;
}
return 0;
}
void VDecAPI11Sample::SetParameterForSpeedPlayer()
{
if (setSpeedPlayerFlag == true) {
OH_AVFormat *format = OH_AVFormat_Create();
if (format == nullptr) {
return;
}
int32_t oneHundred = 100;
int32_t twoHundred = 200;
int32_t threeHundred = 300;
int32_t fourHundred = 400;
double paremeter1 = 0.5;
double paremeter2 = 0.75;
double paremeter3 = 1.25;
double paremeter4 = 1.5;
double paremeter5 = 2.0;
double paremeter6 = 1.0;
if (this->frameCount_ == oneHundred) {
(void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_DECODER_FRAME_RETENTION_MODE,
OH_FRAME_RETENTION_MODE_ADAPTIVE);
(void)OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_VIDEO_DECODER_SPEED, paremeter3);
} else if (this->frameCount_ == twoHundred) {
(void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_DECODER_FRAME_RETENTION_MODE,
OH_FRAME_RETENTION_MODE_UNIFORM);
(void)OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_VIDEO_DECODER_SPEED, paremeter1);
(void)OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_VIDEO_DECODER_FRAME_RETENTION_RATIO,
paremeter4);
} else if (this->frameCount_ == threeHundred) {
(void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_DECODER_FRAME_RETENTION_MODE,
OH_FRAME_RETENTION_MODE_UNIFORM);
(void)OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_VIDEO_DECODER_SPEED, paremeter2);
(void)OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_VIDEO_DECODER_FRAME_RETENTION_RATIO,
paremeter5);
} else if (this->frameCount_ == fourHundred) {
(void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_DECODER_FRAME_RETENTION_MODE,
OH_FRAME_RETENTION_MODE_FULL);
(void)OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_VIDEO_DECODER_SPEED, paremeter6);
} else {
cout << "Unknown frame count: " << this->frameCount_ << endl;
}
OH_VideoDecoder_SetParameter(vdec_, format);
OH_AVFormat_Destroy(format);
}
}