* Copyright (C) 2025 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 "videodec_api11_sample.h"
#include <arpa/inet.h>
#include <sys/time.h>
#include <unistd.h>
#include <unordered_map>
#include <utility>
#include "openssl/crypto.h"
#include "openssl/sha.h"
using namespace OHOS;
using namespace OHOS::Media;
using namespace std;
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 THREE = 3;
constexpr int32_t TEN = 10;
constexpr int32_t DEFAULT_ANGLE = 90;
constexpr uint32_t MILLION = 1000000;
constexpr uint32_t MAX_WIDTH = 4000;
constexpr uint32_t MAX_HEIGHT = 3000;
constexpr uint32_t MAX_NALU_SIZE = MAX_WIDTH * MAX_HEIGHT << 1;
static const unordered_map<std::string, WMV3_StreamData> WMV3_STREAM_DATA = {
{"/data/test/media/profile1_1280_720_24.wmv3",
{.extradata = {0x4b, 0xf1, 0x0a, 0x01},
.extradata_size = 4,
.frameBtyes = {96567, 28373, 28624, 29924, 28891, 29772, 30359, 29766, 31042, 34215, 30812, 33766, 32173,
23693, 29794, 29307, 31815, 31160, 32350, 32360, 23243, 31439, 32176, 31647, 31064, 31641,
33174, 21929, 29088, 31025, 29285, 29210, 30145, 30843, 31814, 30112, 29904, 31500, 32499,
30658, 29975, 29324, 28694, 30066, 29851, 29234, 50921, 29758, 32406, 29906, 28567, 29755,
31127, 29116, 51264, 31911, 31241, 28658, 30101, 30472, 30589},
.frameCount = 61}},
{"/data/test/media/profile0_128x96_10.wmv3",
{.extradata = {0x04, 0x01, 0x80, 0x01},
.extradata_size = 4,
.frameBtyes = {67, 808, 49, 127, 235, 330, 244, 278, 740, 1261, 904, 537, 142, 83, 514, 556, 287, 314,
358, 63, 269, 94, 141, 1280, 192, 113, 125, 212, 427, 275, 202, 561, 508, 148, 807, 579,
346, 386, 660, 697, 580, 635, 1111, 957, 433, 441, 448, 890, 1157, 813, 132, 82, 251, 119,
100, 145, 72, 85, 88, 271, 281, 178, 169, 499, 679, 508, 366, 299, 456, 225, 294, 311,
349, 447, 548, 550, 888, 769, 1041, 299, 875, 683, 236, 173, 180, 402, 204, 262, 173, 134},
.frameCount = 90}},
{"/data/test/media/profile1_352_288_10.wmv3",
{.extradata = {0x45, 0xf9, 0x18, 0x00},
.extradata_size = 4,
.frameBtyes = {9673, 2294, 3049, 2229, 2859, 2735, 2901, 2881, 3056, 3570, 2583, 2636, 2708, 2426, 2226, 2892,
3104, 3224, 2466, 2590, 3371, 2963, 2768, 3152, 3377, 4311, 4512, 3991, 3692, 2715, 3297, 3482,
3461, 3672, 4766, 3194, 3750, 3731, 4622, 3228, 4155, 5918, 5879, 6822, 5330, 6465, 5614, 5274,
5416, 3847, 5176, 3806, 4520, 4829, 3375, 3546, 3483, 4206, 4693, 3826, 3812},
.frameCount = 61}},
{"/data/test/media/profile1_1440x1080_24.wmv3",
{.extradata = {0x4d, 0xf9, 0x0a, 0x01},
.extradata_size = 4,
.frameBtyes = {65582, 7057, 120221, 577, 127679, 31752, 42451, 66667, 30585, 29191, 39954, 65322,
66826, 20434, 40513, 40825, 64701, 42312, 64875, 42258, 41468, 41228, 41135, 40994,
63625, 38455, 37903, 37613, 40575, 40667, 40411, 40441, 40602, 40486, 40711, 40614,
40590, 40496, 40501, 40424, 40576, 40751, 40476, 64338, 38556, 40860, 41010, 40937,
40831, 40260, 40484, 40537, 40722, 40662, 40619, 40302, 40557, 40318, 40541, 40647,
40587, 64092, 38465, 37616, 40558, 40925, 40631, 40230, 40831, 40593, 40879, 40561,
40523, 40182, 40306, 40150, 40598, 40713, 40581, 40390, 65003, 38512, 38027, 40944,
40884, 40494, 40214, 40338, 40548, 40920, 40502, 40248, 40544, 40391, 40713, 40635,
145170, 53296, 43135, 29301, 28105, 38483, 27309, 26431, 36338, 26852, 37376, 26729,
36600, 41649, 41487, 41824, 42009, 41891, 41345, 41311, 41471, 41313, 41130, 41224,
40857, 40803, 7658, 10903, 14563, 15347, 16336, 18456, 20355, 31049, 59725, 21555,
31585, 44506, 87634, 54601, 96223, 99086, 99184, 103780, 117354, 125664, 129393, 125546,
129961, 134391, 140546, 85278, 120767, 38128, 41034, 30511, 30375, 30050, 30328, 29784,
22384, 31651, 33073, 26302, 25814, 35989, 26494, 25860, 31508, 22262, 32880, 30242,
21867, 25481, 28151, 29220, 30106, 31070, 29548, 31949, 32385, 21226, 31420, 30259},
.frameCount = 180}},
{"/data/test/media/profile1_1920x1080_24.wmv3",
{.extradata = {0x4b, 0xf1, 0x08, 0x01, 0x00},
.extradata_size = 5,
.frameBtyes = {10208, 12, 12, 12, 12, 12, 175454, 81703, 96222, 12688, 83134, 13098, 44182, 81929,
18789, 20231, 82946, 12123, 41382, 81978, 18979, 21833, 81136, 12944, 44249, 36321, 88734, 11426,
44240, 41766, 89005, 9176, 41447, 39203, 86638, 10266, 44534, 39951, 45092, 81478, 19600, 19401,
40328, 80393, 17385, 21404, 43975, 83305, 18359, 22548, 43608, 40756, 88598, 11548, 43345, 36901,
44883, 79154, 19072, 19639, 42296, 78537, 20970, 19374, 42398, 40741, 84877, 9779, 44521, 39010,
44917, 79201, 19746, 19975, 41422, 36541, 83425, 11430, 39764, 40228, 42586, 83541, 19905, 20472,
43109, 37243, 83414, 12147, 41709, 37397, 44183, 77316, 18084, 20972, 45261, 39740, 90813, 10196,
22812, 84013, 19300, 20965, 45377, 39246, 81887, 11717, 23037, 81804, 20600, 18743, 43264, 79605,
20972, 18986, 42406, 82242, 19739, 21847, 42328, 40513, 85273, 9011, 39884, 39863, 45346, 97926,
10210, 94, 473, 3526, 3931, 4506, 4461, 4710, 4116, 4611, 4530, 4548, 4340, 4874,
4508, 4654, 4700, 4783, 4469, 4568, 4448, 4871, 4670, 4635, 4537, 4831, 4753, 4614,
4418, 4778, 4524, 4420, 4214, 4945, 4473, 6881, 75196, 24838, 31081, 25945, 35081, 30603,
45473, 40686, 54285, 44748, 61293, 51958, 65359, 59281, 82268, 76347, 33327, 33240},
.frameCount = 180}}};
SHA512_CTX g_c;
unsigned char g_md[SHA512_DIGEST_LENGTH];
VDecAPI11Sample *dec_sample = nullptr;
int32_t g_strideSurface = 0;
int32_t g_sliceSurface = 0;
bool g_yuvSurface = false;
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)
{
outFile_ = std::make_unique<std::ofstream>();
outFile_->open(name.data(), std::ios::out | std::ios::binary);
cout << "at surface open " << name.data() << " output file" << endl;
};
~ConsumerListenerBuffer()
{
if (outFile_ != nullptr) {
outFile_->close();
}
}
void OnBufferAvailable() override
{
sptr<SurfaceBuffer> buffer;
int32_t flushFence;
cs->AcquireBuffer(buffer, flushFence, timestamp, damage);
if (buffer == nullptr) {
cout << "surface is nullptr" << endl;
return;
} else {
int32_t frameSize = (g_strideSurface * g_sliceSurface * THREE) >> 1;
if (g_yuvSurface && outFile_ != nullptr && frameSize <= buffer->GetSize()) {
outFile_->write(reinterpret_cast<char *>(buffer->GetVirAddr()), frameSize);
}
}
cs->ReleaseBuffer(buffer, -1);
}
private:
int64_t timestamp = 0;
Rect damage = {};
sptr<Surface> cs{nullptr};
std::unique_ptr<std::ofstream> outFile_;
};
VDecAPI11Sample::~VDecAPI11Sample()
{
g_yuvSurface = false;
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)
{
if (errorCode == AV_ERR_UNKNOWN) {
dec_sample->isRunning_.store(false);
dec_sample->signal_->inCond_.notify_all();
dec_sample->signal_->outCond_.notify_all();
dec_sample->isonError = true;
}
cout << "Error errorCode=" << errorCode << endl;
}
void VdecAPI11FormatChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData)
{
int32_t currentWidth = 0;
int32_t currentHeight = 0;
int32_t stride = 0;
int32_t sliceHeight = 0;
OH_AVFormat_GetIntValue(format, OH_MD_KEY_WIDTH, ¤tWidth);
OH_AVFormat_GetIntValue(format, OH_MD_KEY_HEIGHT, ¤tHeight);
OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_STRIDE, &stride);
OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_SLICE_HEIGHT, &sliceHeight);
dec_sample->DEFAULT_WIDTH = currentWidth;
dec_sample->DEFAULT_HEIGHT = currentHeight;
g_strideSurface = stride;
g_sliceSurface = sliceHeight;
if (stride <= 0 || sliceHeight <= 0) {
dec_sample->errCount++;
}
cout << "Format Changed, g_strideSurface: " <<
g_strideSurface << ", g_sliceSurface: " << g_sliceSurface << endl;
}
void VdecAPI11InputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *data, void *userData)
{
if (dec_sample->inputCallbackFlush && dec_sample->outCount > 1) {
OH_VideoDecoder_Flush(codec);
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->outputCallbackFlush && dec_sample->outCount > 1) {
OH_VideoDecoder_Flush(codec);
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();
}
bool VDecAPI11Sample::MdCompare(unsigned char buffer[], int len, const char *source[])
{
bool result = true;
for (int i = 0; i < len; i++) {
}
return result;
}
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;
}
int32_t VDecAPI11Sample::ConfigureVideoDecoder()
{
if (autoSwitchSurface) {
switchSurfaceFlag = (switchSurfaceFlag == 1) ? 0 : 1;
if (OH_VideoDecoder_SetSurface(vdec_, nativeWindow[switchSurfaceFlag]) != AV_ERR_INVALID_STATE) {
errCount++;
}
}
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);
}
originalWidth = DEFAULT_WIDTH;
originalHeight = DEFAULT_HEIGHT;
(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_ENABLE_SYNC_MODE, enbleSyncMode);
(void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_DECODER_BLANK_FRAME_ON_SHUTDOWN, enbleBlankFrame);
if ((WMV3_STREAM_DATA.find(INP_DIR)) != WMV3_STREAM_DATA.end()) {
streamData_ = WMV3_STREAM_DATA.at(INP_DIR);
OH_AVFormat_SetBuffer(format, OH_MD_KEY_CODEC_CONFIG,
streamData_.extradata.data(), streamData_.extradata_size);
cout << "find wmv3 stream data and frame count " << streamData_.frameCount << endl;
}
int ret = OH_VideoDecoder_Configure(vdec_, format);
OH_AVFormat_Destroy(format);
return ret;
}
void VDecAPI11Sample::CreateSurface()
{
cs[0] = Surface::CreateSurfaceAsConsumer();
if (cs[0] == nullptr) {
cout << "Create the surface consummer fail" << endl;
return;
}
GSError err = cs[0]->SetDefaultUsage(BUFFER_USAGE_MEM_DMA | BUFFER_USAGE_VIDEO_DECODER | BUFFER_USAGE_CPU_READ);
if (err == GSERROR_OK) {
cout << "set consumer usage succ" << endl;
} else {
cout << "set consumer usage failed" << endl;
}
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) {
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;
}
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;
wmv3Unit_.reset();
wmv3Unit_ = nullptr;
frameIndex_ = 0;
}
}
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::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;
}
wmv3Unit_ = std::make_unique<std::vector<uint8_t>>(MAX_NALU_SIZE);
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;
}
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::StartSyncVideoDecoder()
{
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;
}
wmv3Unit_ = std::make_unique<std::vector<uint8_t>>(MAX_NALU_SIZE);
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;
}
signal_ = new VDecAPI11Signal();
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;
}
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::InputFuncTest()
{
if (outputYuvSurface) {
g_yuvSurface = true;
}
bool flag = true;
while (flag) {
if (!isRunning_.load()) {
flag = false;
break;
}
uint32_t index;
unique_lock<mutex> lock(signal_->inMutex_);
signal_->inCond_.wait(lock, [this]() {
if (!isRunning_.load()) {
return true;
}
return signal_->inIdxQueue_.size() > 0;
});
if (!isRunning_.load()) {
flag = false;
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) {
flag = false;
break;
}
}
if (sleepOnFPS) {
usleep(MICRO_IN_SECOND / (int32_t)DEFAULT_FRAME_RATE);
}
}
}
void VDecAPI11Sample::SyncInputFunc()
{
if (outputYuvSurface) {
g_yuvSurface = true;
}
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::PushData(uint32_t index, OH_AVBuffer *buffer)
{
uint32_t bufferSize = 0;
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;
}
if (frameIndex_ < streamData_.frameCount) {
uint32_t frameSize = streamData_.frameBtyes[frameIndex_];
wmv3Unit_->resize(frameSize);
auto pBuffer = wmv3Unit_->data();
inFile_->read(reinterpret_cast<char *>(pBuffer), frameSize);
frameIndex_++;
} else {
SetEOS(index, buffer);
wmv3Unit_->resize(0);
return 1;
}
bufferSize = wmv3Unit_->size();
if (bufferSize >= DEFAULT_WIDTH * DEFAULT_HEIGHT * THREE >> 1) {
cout << "read bufferSize abnormal. buffersize = " << bufferSize << endl;
return 1;
}
return SendData(bufferSize, index, buffer);
}
int32_t VDecAPI11Sample::CheckAndReturnBufferSize(OH_AVBuffer *buffer)
{
int32_t size = OH_AVBuffer_GetCapacity(buffer);
if (maxInputSize > 0 && (size > maxInputSize)) {
errCount++;
}
return size;
}
void VDecAPI11Sample::SetAttr(OH_AVCodecBufferAttr &attr, uint32_t bufferSize)
{
int64_t startPts = GetSystemTimeUs();
attr.pts = startPts;
attr.size = bufferSize;
attr.offset = 0;
}
void VDecAPI11Sample::EnforceAutoSwitchSurface()
{
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++);
}
}
uint32_t VDecAPI11Sample::SendData(uint32_t bufferSize, uint32_t index, OH_AVBuffer *buffer)
{
OH_AVCodecBufferAttr attr;
uint8_t *fileBuffer = nullptr;
if (bufferSize > 0) {
fileBuffer = new uint8_t[bufferSize];
} else {
delete[] fileBuffer;
return 0;
}
if (memcpy_s(fileBuffer, bufferSize, wmv3Unit_->data(), bufferSize) != EOK) {
cout << "Fatal: memory copy failed" << endl;
}
if (bufferSize != 0) {
attr.flags = AVCODEC_BUFFER_FLAGS_SYNC_FRAME + AVCODEC_BUFFER_FLAGS_CODEC_DATA;
} else {
if (!isWmv3Change) {
SetEOS(index, buffer);
return 1;
} else {
return 0;
}
}
int32_t size = CheckAndReturnBufferSize(buffer);
if (size < bufferSize) {
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) != EOK) {
delete[] fileBuffer;
return 0;
}
SetAttr(attr, bufferSize);
if (isRunning_.load()) {
OH_AVBuffer_SetBufferAttr(buffer, &attr);
OH_VideoDecoder_PushInputBuffer(vdec_, index) == AV_ERR_OK ? (0) : (errCount++);
frameCount_ = frameCount_ + 1;
outCount = outCount + 1;
EnforceAutoSwitchSurface();
}
delete[] fileBuffer;
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 || cropBottom != expectCropBottom || cropLeft != expectCropLeft) {
std::cout << "cropTop:" << cropTop << " cropBottom:" << cropBottom << " cropLeft:" << cropLeft << std::endl;
errCount++;
}
if (cropRight != expectCropRight || stride <= 0 || sliceHeight <= 0) {
std::cout << "cropRight:" << cropRight << std::endl;
std::cout << "stride:" << stride << " sliceHeight:" << sliceHeight << std::endl;
errCount++;
}
if (picWidth != originalWidth || picHeight != originalHeight) {
std::cout << "picWidth:" << picWidth << " picHeight:" << picHeight << std::endl;
errCount++;
}
} else {
errCount++;
}
OH_AVFormat_Destroy(newFormat);
}
void VDecAPI11Sample::AutoSwitchSurface()
{
if (autoSwitchSurface) {
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 (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));
MdCompare(g_md, SHA512_DIGEST_LENGTH, fileSourcesha256);
return -1;
}
outFrameCount = outFrameCount + 1;
return 0;
}
void VDecAPI11Sample::OutputFuncTest()
{
FILE *outFile = nullptr;
if (outputYuvFlag) {
outFile = fopen(OUT_DIR, "wb");
}
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;
});
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;
}
lock.unlock();
if (CheckAttrFlag(attr) == -1) {
flag = false;
break;
}
if (outFile != nullptr) {
fwrite(OH_AVBuffer_GetAddr(buffer), 1, attr.size, outFile);
}
ProcessOutputData(buffer, index, attr.size);
if (errCount > 0) {
flag = false;
break;
}
}
if (outFile) {
(void)fclose(outFile);
}
}
void VDecAPI11Sample::SyncOutputFunc()
{
FILE *outFile = nullptr;
if (outputYuvFlag) {
outFile = fopen(OUT_DIR, "wb");
}
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;
}
if (outFile != nullptr) {
fwrite(OH_AVBuffer_GetAddr(buffer), 1, attr.size, outFile);
}
ProcessOutputData(buffer, index, attr.size);
if (errCount > 0) {
flag = false;
break;
}
}
if (outFile) {
(void)fclose(outFile);
}
}
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, int32_t size)
{
GetVideoSupportedPixelFormats();
GetFormatKey();
if (!SF_OUTPUT) {
if (size >= DEFAULT_WIDTH * DEFAULT_HEIGHT * THREE >> 1) {
uint8_t *cropBuffer = new uint8_t[size];
if (memcpy_s(cropBuffer, size, OH_AVBuffer_GetAddr(buffer), DEFAULT_WIDTH * DEFAULT_HEIGHT) != EOK) {
cout << "Fatal: memory copy failed Y" << endl;
}
uint32_t uvSize = size - DEFAULT_WIDTH * DEFAULT_HEIGHT;
if (memcpy_s(cropBuffer + DEFAULT_WIDTH * DEFAULT_HEIGHT, uvSize,
OH_AVBuffer_GetAddr(buffer) + DEFAULT_WIDTH * DEFAULT_HEIGHT, uvSize) != EOK) {
cout << "Fatal: memory copy failed UV" << endl;
}
SHA512_Update(&g_c, cropBuffer, size);
delete[] cropBuffer;
}
if (OH_VideoDecoder_FreeOutputBuffer(vdec_, index) != AV_ERR_OK) {
cout << "Fatal: ReleaseOutputBuffer fail" << endl;
errCount = errCount + 1;
}
} else {
if (rsAtTime) {
int32_t usTimeNum = 1000;
int32_t msTimeNum = 1000000;
if (renderTimestampNs == 0) {
renderTimestampNs = GetSystemTimeUs() * usTimeNum;
}
renderTimestampNs = renderTimestampNs + (usTimeNum / DEFAULT_FRAME_RATE * msTimeNum);
if (OH_VideoDecoder_RenderOutputBufferAtTime(vdec_, index, renderTimestampNs) != AV_ERR_OK) {
cout << "Fatal: RenderOutputBufferAtTime fail" << endl;
errCount = errCount + 1;
}
} else {
if (OH_VideoDecoder_RenderOutputBuffer(vdec_, index) != AV_ERR_OK) {
cout << "Fatal: RenderOutputBuffer fail" << endl;
errCount = errCount + 1;
}
}
}
}
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()
{
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);
return OH_VideoDecoder_Flush(vdec_);
}
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 (signal_ != nullptr) {
delete signal_;
signal_ = nullptr;
}
return ret;
}
int32_t VDecAPI11Sample::Stop()
{
StopInloop();
StopOutloop();
ReleaseInFile();
return OH_VideoDecoder_Stop(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;
for (int i = 0; i < pixlFormatNum; i++) {
std::cout << "pixFormats[" << i << "]: " << *(pixlFormats + i) << 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++;
}