1 /*
2 * Copyright (C) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <arpa/inet.h>
16 #include <sys/time.h>
17 #include <utility>
18 #include "openssl/crypto.h"
19 #include "openssl/sha.h"
20 #include "videodec_api11_sample.h"
21 using namespace OHOS;
22 using namespace OHOS::Media;
23 using namespace std;
24 namespace {
25 constexpr int64_t NANOS_IN_SECOND = 1000000000L;
26 constexpr int64_t MICRO_IN_SECOND = 1000000L;
27 constexpr int64_t NANOS_IN_MICRO = 1000L;
28 constexpr int32_t THREE = 3;
29 constexpr int32_t EIGHT = 8;
30 constexpr int32_t TEN = 10;
31 constexpr int32_t SIXTEEN = 16;
32 constexpr int32_t TWENTY_FOUR = 24;
33 constexpr uint32_t START_CODE_SIZE = 4;
34 constexpr int32_t DEFAULT_ANGLE = 90;
35 constexpr uint32_t MILLION = 1000000;
36 SHA512_CTX g_c;
37 unsigned char g_md[SHA512_DIGEST_LENGTH];
38 VDecAPI11Sample *dec_sample = nullptr;
39 int32_t g_strideSurface = 0;
40 int32_t g_sliceSurface = 0;
41 bool g_yuvSurface = false;
42
clearIntqueue(std::queue<uint32_t> & q)43 void clearIntqueue(std::queue<uint32_t> &q)
44 {
45 std::queue<uint32_t> empty;
46 swap(empty, q);
47 }
48
clearBufferqueue(std::queue<OH_AVCodecBufferAttr> & q)49 void clearBufferqueue(std::queue<OH_AVCodecBufferAttr> &q)
50 {
51 std::queue<OH_AVCodecBufferAttr> empty;
52 swap(empty, q);
53 }
54 } // namespace
55
56 class ConsumerListenerBuffer : public IBufferConsumerListener {
57 public:
ConsumerListenerBuffer(sptr<Surface> cs,std::string_view name)58 ConsumerListenerBuffer(sptr<Surface> cs, std::string_view name) : cs(cs)
59 {
60 outFile_ = std::make_unique<std::ofstream>();
61 outFile_->open(name.data(), std::ios::out | std::ios::binary);
62 };
~ConsumerListenerBuffer()63 ~ConsumerListenerBuffer()
64 {
65 if (outFile_ != nullptr) {
66 outFile_->close();
67 }
68 }
OnBufferAvailable()69 void OnBufferAvailable() override
70 {
71 sptr<SurfaceBuffer> buffer;
72 int32_t flushFence;
73 cs->AcquireBuffer(buffer, flushFence, timestamp, damage);
74 if (buffer == nullptr) {
75 cout << "surface is nullptr" << endl;
76 return;
77 } else {
78 int32_t frameSize = (g_strideSurface * g_sliceSurface * THREE) >> 1;
79 if (g_yuvSurface && outFile_ != nullptr && frameSize <= buffer->GetSize()) {
80 outFile_->write(reinterpret_cast<char *>(buffer->GetVirAddr()), frameSize);
81 }
82 }
83 cs->ReleaseBuffer(buffer, -1);
84 }
85
86 private:
87 int64_t timestamp = 0;
88 Rect damage = {};
89 sptr<Surface> cs {nullptr};
90 std::unique_ptr<std::ofstream> outFile_;
91 };
92
~VDecAPI11Sample()93 VDecAPI11Sample::~VDecAPI11Sample()
94 {
95 g_yuvSurface = false;
96 for (int i = 0; i < MAX_SURF_NUM; i++) {
97 if (nativeWindow[i]) {
98 OH_NativeWindow_DestroyNativeWindow(nativeWindow[i]);
99 nativeWindow[i] = nullptr;
100 }
101 }
102 Stop();
103 Release();
104 }
105
VdecAPI11Error(OH_AVCodec * codec,int32_t errorCode,void * userData)106 void VdecAPI11Error(OH_AVCodec *codec, int32_t errorCode, void *userData)
107 {
108 if (errorCode == AV_ERR_UNKNOWN) {
109 dec_sample->isRunning_.store(false);
110 dec_sample->signal_->inCond_.notify_all();
111 dec_sample->signal_->outCond_.notify_all();
112 dec_sample->isonError = true;
113 }
114 cout << "Error errorCode=" << errorCode << endl;
115 }
116
VdecAPI11FormatChanged(OH_AVCodec * codec,OH_AVFormat * format,void * userData)117 void VdecAPI11FormatChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData)
118 {
119 int32_t currentWidth = 0;
120 int32_t currentHeight = 0;
121 int32_t stride = 0;
122 int32_t sliceHeight = 0;
123 OH_AVFormat_GetIntValue(format, OH_MD_KEY_WIDTH, ¤tWidth);
124 OH_AVFormat_GetIntValue(format, OH_MD_KEY_HEIGHT, ¤tHeight);
125 OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_STRIDE, &stride);
126 OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_SLICE_HEIGHT, &sliceHeight);
127 dec_sample->DEFAULT_WIDTH = currentWidth;
128 dec_sample->DEFAULT_HEIGHT = currentHeight;
129 g_strideSurface = stride;
130 g_sliceSurface = sliceHeight;
131 if (stride <= 0 || sliceHeight <= 0) {
132 dec_sample->errCount++;
133 }
134 }
135
VdecAPI11InputDataReady(OH_AVCodec * codec,uint32_t index,OH_AVBuffer * data,void * userData)136 void VdecAPI11InputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *data, void *userData)
137 {
138 if (dec_sample->inputCallbackFlush && dec_sample->outCount > 1) {
139 OH_VideoDecoder_Flush(codec);
140 cout << "OH_VideoDecoder_Flush end" << endl;
141 dec_sample->isRunning_.store(false);
142 dec_sample->signal_->inCond_.notify_all();
143 dec_sample->signal_->outCond_.notify_all();
144 return;
145 }
146 if (dec_sample->inputCallbackStop && dec_sample->outCount > 1) {
147 OH_VideoDecoder_Stop(codec);
148 cout << "OH_VideoDecoder_Stop end" << endl;
149 dec_sample->isRunning_.store(false);
150 dec_sample->signal_->inCond_.notify_all();
151 dec_sample->signal_->outCond_.notify_all();
152 return;
153 }
154 VDecAPI11Signal *signal = static_cast<VDecAPI11Signal *>(userData);
155 unique_lock<mutex> lock(signal->inMutex_);
156 signal->inIdxQueue_.push(index);
157 signal->inBufferQueue_.push(data);
158 signal->inCond_.notify_all();
159 }
160
VdecAPI11OutputDataReady(OH_AVCodec * codec,uint32_t index,OH_AVBuffer * data,void * userData)161 void VdecAPI11OutputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *data, void *userData)
162 {
163 if (dec_sample->outputCallbackFlush && dec_sample->outCount > 1) {
164 OH_VideoDecoder_Flush(codec);
165 cout << "OH_VideoDecoder_Flush end" << endl;
166 dec_sample->isRunning_.store(false);
167 dec_sample->signal_->inCond_.notify_all();
168 dec_sample->signal_->outCond_.notify_all();
169 return;
170 }
171 if (dec_sample->outputCallbackStop && dec_sample->outCount > 1) {
172 OH_VideoDecoder_Stop(codec);
173 cout << "OH_VideoDecoder_Stop end" << endl;
174 dec_sample->isRunning_.store(false);
175 dec_sample->signal_->inCond_.notify_all();
176 dec_sample->signal_->outCond_.notify_all();
177 return;
178 }
179 VDecAPI11Signal *signal = static_cast<VDecAPI11Signal *>(userData);
180 unique_lock<mutex> lock(signal->outMutex_);
181 signal->outIdxQueue_.push(index);
182 signal->outBufferQueue_.push(data);
183 signal->outCond_.notify_all();
184 }
185
Flush_buffer()186 void VDecAPI11Sample::Flush_buffer()
187 {
188 unique_lock<mutex> inLock(signal_->inMutex_);
189 clearIntqueue(signal_->inIdxQueue_);
190 std::queue<OH_AVBuffer *> empty;
191 swap(empty, signal_->inBufferQueue_);
192 signal_->inCond_.notify_all();
193 inLock.unlock();
194 unique_lock<mutex> outLock(signal_->outMutex_);
195 clearIntqueue(signal_->outIdxQueue_);
196 clearBufferqueue(signal_->attrQueue_);
197 signal_->outCond_.notify_all();
198 outLock.unlock();
199 }
200
MdCompare(unsigned char buffer[],int len,const char * source[])201 bool VDecAPI11Sample::MdCompare(unsigned char buffer[], int len, const char *source[])
202 {
203 bool result = true;
204 for (int i = 0; i < len; i++) {
205 }
206 return result;
207 }
208
GetSystemTimeUs()209 int64_t VDecAPI11Sample::GetSystemTimeUs()
210 {
211 struct timespec now;
212 (void)clock_gettime(CLOCK_BOOTTIME, &now);
213 int64_t nanoTime = (int64_t)now.tv_sec * NANOS_IN_SECOND + now.tv_nsec;
214 return nanoTime / NANOS_IN_MICRO;
215 }
216
ConfigureVideoDecoder()217 int32_t VDecAPI11Sample::ConfigureVideoDecoder()
218 {
219 if (autoSwitchSurface) {
220 switchSurfaceFlag = (switchSurfaceFlag == 1) ? 0 : 1;
221 if (OH_VideoDecoder_SetSurface(vdec_, nativeWindow[switchSurfaceFlag]) != AV_ERR_INVALID_STATE) {
222 errCount++;
223 }
224 }
225 OH_AVFormat *format = OH_AVFormat_Create();
226 if (format == nullptr) {
227 cout << "Fatal: Failed to create format" << endl;
228 return AV_ERR_UNKNOWN;
229 }
230 if (maxInputSize > 0) {
231 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, maxInputSize);
232 }
233 originalWidth = DEFAULT_WIDTH;
234 originalHeight = DEFAULT_HEIGHT;
235 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
236 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
237 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, defualtPixelFormat);
238 (void)OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, DEFAULT_FRAME_RATE);
239 if (useHDRSource) {
240 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, DEFAULT_PROFILE);
241 }
242 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_ENABLE_SYNC_MODE, enbleSyncMode);
243 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_DECODER_BLANK_FRAME_ON_SHUTDOWN, enbleBlankFrame);
244 int ret = OH_VideoDecoder_Configure(vdec_, format);
245 OH_AVFormat_Destroy(format);
246 return ret;
247 }
248
CreateSurface()249 void VDecAPI11Sample::CreateSurface()
250 {
251 cs[0] = Surface::CreateSurfaceAsConsumer();
252 if (cs[0] == nullptr) {
253 cout << "Create the surface consummer fail" << endl;
254 return;
255 }
256 GSError err = cs[0]->SetDefaultUsage(BUFFER_USAGE_MEM_DMA | BUFFER_USAGE_VIDEO_DECODER | BUFFER_USAGE_CPU_READ);
257 if (err == GSERROR_OK) {
258 cout << "set consumer usage succ" << endl;
259 } else {
260 cout << "set consumer usage failed" << endl;
261 }
262 sptr<IBufferConsumerListener> listener = new ConsumerListenerBuffer(cs[0], OUT_DIR);
263 cs[0]->RegisterConsumerListener(listener);
264 auto p = cs[0]->GetProducer();
265 ps[0] = Surface::CreateSurfaceAsProducer(p);
266 nativeWindow[0] = CreateNativeWindowFromSurface(&ps[0]);
267 if (autoSwitchSurface) {
268 cs[1] = Surface::CreateSurfaceAsConsumer();
269 sptr<IBufferConsumerListener> listener2 = new ConsumerListenerBuffer(cs[1], OUT_DIR2);
270 cs[1]->RegisterConsumerListener(listener2);
271 auto p2 = cs[1]->GetProducer();
272 ps[1] = Surface::CreateSurfaceAsProducer(p2);
273 nativeWindow[1] = CreateNativeWindowFromSurface(&ps[1]);
274 }
275 }
276
RunVideoDec_Surface(string codeName)277 int32_t VDecAPI11Sample::RunVideoDec_Surface(string codeName)
278 {
279 SF_OUTPUT = true;
280 int err = AV_ERR_OK;
281 CreateSurface();
282 if (!nativeWindow[0]) {
283 cout << "Failed to create surface" << endl;
284 return AV_ERR_UNKNOWN;
285 }
286 err = CreateVideoDecoder(codeName);
287 if (err != AV_ERR_OK) {
288 cout << "Failed to create video decoder" << endl;
289 return err;
290 }
291 err = SetVideoDecoderCallback();
292 if (err != AV_ERR_OK) {
293 cout << "Failed to setCallback" << endl;
294 Release();
295 return err;
296 }
297 err = ConfigureVideoDecoder();
298 if (err != AV_ERR_OK) {
299 cout << "Failed to configure video decoder" << endl;
300 Release();
301 return err;
302 }
303 err = OH_VideoDecoder_SetSurface(vdec_, nativeWindow[0]);
304 if (err != AV_ERR_OK) {
305 cout << "Failed to set surface" << endl;
306 return err;
307 }
308 err = StartVideoDecoder();
309 if (err != AV_ERR_OK) {
310 cout << "Failed to start video decoder" << endl;
311 Release();
312 return err;
313 }
314 return err;
315 }
316
RunVideoDec(string codeName)317 int32_t VDecAPI11Sample::RunVideoDec(string codeName)
318 {
319 SF_OUTPUT = false;
320 int err = CreateVideoDecoder(codeName);
321 if (err != AV_ERR_OK) {
322 cout << "Failed to create video decoder" << endl;
323 return err;
324 }
325
326 err = ConfigureVideoDecoder();
327 if (err != AV_ERR_OK) {
328 cout << "Failed to configure video decoder" << endl;
329 Release();
330 return err;
331 }
332
333 err = SetVideoDecoderCallback();
334 if (err != AV_ERR_OK) {
335 cout << "Failed to setCallback" << endl;
336 Release();
337 return err;
338 }
339
340 err = StartVideoDecoder();
341 if (err != AV_ERR_OK) {
342 cout << "Failed to start video decoder" << endl;
343 Release();
344 return err;
345 }
346 return err;
347 }
348
SetVideoDecoderCallback()349 int32_t VDecAPI11Sample::SetVideoDecoderCallback()
350 {
351 signal_ = new VDecAPI11Signal();
352 if (signal_ == nullptr) {
353 cout << "Failed to new VDecAPI11Signal" << endl;
354 return AV_ERR_UNKNOWN;
355 }
356
357 cb_.onError = VdecAPI11Error;
358 cb_.onStreamChanged = VdecAPI11FormatChanged;
359 cb_.onNeedInputBuffer = VdecAPI11InputDataReady;
360 cb_.onNewOutputBuffer = VdecAPI11OutputDataReady;
361 return OH_VideoDecoder_RegisterCallback(vdec_, cb_, static_cast<void *>(signal_));
362 }
363
ReleaseInFile()364 void VDecAPI11Sample::ReleaseInFile()
365 {
366 if (inFile_ != nullptr) {
367 if (inFile_->is_open()) {
368 inFile_->close();
369 }
370 inFile_.reset();
371 inFile_ = nullptr;
372 }
373 }
374
StopInloop()375 void VDecAPI11Sample::StopInloop()
376 {
377 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
378 unique_lock<mutex> lock(signal_->inMutex_);
379 clearIntqueue(signal_->inIdxQueue_);
380 isRunning_.store(false);
381 signal_->inCond_.notify_all();
382 lock.unlock();
383
384 inputLoop_->join();
385 inputLoop_.reset();
386 }
387 }
388
CreateVideoDecoder(string codeName)389 int32_t VDecAPI11Sample::CreateVideoDecoder(string codeName)
390 {
391 vdec_ = OH_VideoDecoder_CreateByName(codeName.c_str());
392 dec_sample = this;
393 return vdec_ == nullptr ? AV_ERR_UNKNOWN : AV_ERR_OK;
394 }
395
StartVideoDecoder()396 int32_t VDecAPI11Sample::StartVideoDecoder()
397 {
398 isRunning_.store(true);
399 int ret = OH_VideoDecoder_Start(vdec_);
400 if (ret != AV_ERR_OK) {
401 cout << "Failed to start codec" << endl;
402 isRunning_.store(false);
403 ReleaseInFile();
404 Release();
405 return ret;
406 }
407 inFile_ = make_unique<ifstream>();
408 if (inFile_ == nullptr) {
409 isRunning_.store(false);
410 (void)OH_VideoDecoder_Stop(vdec_);
411 return AV_ERR_UNKNOWN;
412 }
413 inFile_->open(INP_DIR, ios::in | ios::binary);
414 if (!inFile_->is_open()) {
415 cout << "failed open file " << INP_DIR << endl;
416 isRunning_.store(false);
417 (void)OH_VideoDecoder_Stop(vdec_);
418 inFile_->close();
419 inFile_.reset();
420 inFile_ = nullptr;
421 return AV_ERR_UNKNOWN;
422 }
423 inputLoop_ = make_unique<thread>(&VDecAPI11Sample::InputFuncTest, this);
424 if (inputLoop_ == nullptr) {
425 cout << "Failed to create input loop" << endl;
426 isRunning_.store(false);
427 (void)OH_VideoDecoder_Stop(vdec_);
428 ReleaseInFile();
429 return AV_ERR_UNKNOWN;
430 }
431 outputLoop_ = make_unique<thread>(&VDecAPI11Sample::OutputFuncTest, this);
432 if (outputLoop_ == nullptr) {
433 cout << "Failed to create output loop" << endl;
434 isRunning_.store(false);
435 (void)OH_VideoDecoder_Stop(vdec_);
436 ReleaseInFile();
437 StopInloop();
438 Release();
439 return AV_ERR_UNKNOWN;
440 }
441
442 return AV_ERR_OK;
443 }
444
StartSyncVideoDecoder()445 int32_t VDecAPI11Sample::StartSyncVideoDecoder()
446 {
447 isRunning_.store(true);
448 int ret = OH_VideoDecoder_Start(vdec_);
449 if (ret != AV_ERR_OK) {
450 cout << "Failed to start codec" << endl;
451 isRunning_.store(false);
452 ReleaseInFile();
453 Release();
454 return ret;
455 }
456 inFile_ = make_unique<ifstream>();
457 if (inFile_ == nullptr) {
458 isRunning_.store(false);
459 (void)OH_VideoDecoder_Stop(vdec_);
460 return AV_ERR_UNKNOWN;
461 }
462 inFile_->open(INP_DIR, ios::in | ios::binary);
463 if (!inFile_->is_open()) {
464 cout << "failed open file " << INP_DIR << endl;
465 isRunning_.store(false);
466 (void)OH_VideoDecoder_Stop(vdec_);
467 inFile_->close();
468 inFile_.reset();
469 inFile_ = nullptr;
470 return AV_ERR_UNKNOWN;
471 }
472 signal_ = new VDecAPI11Signal();
473 inputLoop_ = make_unique<thread>(&VDecAPI11Sample::SyncInputFunc, this);
474 if (inputLoop_ == nullptr) {
475 cout << "Failed to create input loop" << endl;
476 isRunning_.store(false);
477 (void)OH_VideoDecoder_Stop(vdec_);
478 ReleaseInFile();
479 return AV_ERR_UNKNOWN;
480 }
481 outputLoop_ = make_unique<thread>(&VDecAPI11Sample::SyncOutputFunc, this);
482 if (outputLoop_ == nullptr) {
483 cout << "Failed to create output loop" << endl;
484 isRunning_.store(false);
485 (void)OH_VideoDecoder_Stop(vdec_);
486 ReleaseInFile();
487 StopInloop();
488 Release();
489 return AV_ERR_UNKNOWN;
490 }
491
492 return AV_ERR_OK;
493 }
494
testAPI()495 void VDecAPI11Sample::testAPI()
496 {
497 cs[0] = Surface::CreateSurfaceAsConsumer();
498 sptr<IBufferConsumerListener> listener = new ConsumerListenerBuffer(cs[0], OUT_DIR);
499 cs[0]->RegisterConsumerListener(listener);
500 auto p = cs[0]->GetProducer();
501 ps[0] = Surface::CreateSurfaceAsProducer(p);
502 nativeWindow[0] = CreateNativeWindowFromSurface(&ps[0]);
503 OH_VideoDecoder_SetSurface(vdec_, nativeWindow[0]);
504
505 OH_VideoDecoder_Prepare(vdec_);
506 OH_VideoDecoder_Start(vdec_);
507
508 OH_AVFormat *format = OH_AVFormat_Create();
509 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
510 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
511 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
512 (void)OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, DEFAULT_FRAME_RATE);
513 OH_VideoDecoder_SetParameter(vdec_, format);
514 OH_AVFormat_Destroy(format);
515 OH_VideoDecoder_GetOutputDescription(vdec_);
516 OH_VideoDecoder_Flush(vdec_);
517 OH_VideoDecoder_Stop(vdec_);
518 OH_VideoDecoder_Reset(vdec_);
519 bool isvalid = false;
520 OH_VideoDecoder_IsValid(vdec_, &isvalid);
521 }
522
WaitForEOS()523 void VDecAPI11Sample::WaitForEOS()
524 {
525 if (!AFTER_EOS_DESTORY_CODEC && inputLoop_ && inputLoop_->joinable()) {
526 inputLoop_->join();
527 }
528
529 if (outputLoop_ && outputLoop_->joinable()) {
530 outputLoop_->join();
531 }
532 }
533
InputFuncTest()534 void VDecAPI11Sample::InputFuncTest()
535 {
536 if (outputYuvSurface) {
537 g_yuvSurface = true;
538 }
539 bool flag = true;
540 while (flag) {
541 if (!isRunning_.load()) {
542 flag = false;
543 break;
544 }
545 uint32_t index;
546 unique_lock<mutex> lock(signal_->inMutex_);
547 signal_->inCond_.wait(lock, [this]() {
548 if (!isRunning_.load()) {
549 return true;
550 }
551 return signal_->inIdxQueue_.size() > 0;
552 });
553 if (!isRunning_.load()) {
554 flag = false;
555 break;
556 }
557 index = signal_->inIdxQueue_.front();
558 auto buffer = signal_->inBufferQueue_.front();
559 signal_->inIdxQueue_.pop();
560 signal_->inBufferQueue_.pop();
561 lock.unlock();
562 if (!inFile_->eof()) {
563 int ret = PushData(index, buffer);
564 if (ret == 1) {
565 flag = false;
566 break;
567 }
568 }
569 if (sleepOnFPS) {
570 usleep(MICRO_IN_SECOND / (int32_t)DEFAULT_FRAME_RATE);
571 }
572 }
573 }
574
SyncInputFunc()575 void VDecAPI11Sample::SyncInputFunc()
576 {
577 if (outputYuvSurface) {
578 g_yuvSurface = true;
579 }
580 bool flag = true;
581 while (flag) {
582 if (!isRunning_.load()) {
583 flag = false;
584 break;
585 }
586 uint32_t index;
587 if (OH_VideoDecoder_QueryInputBuffer(vdec_, &index, syncInputWaitTime) != AV_ERR_OK) {
588 continue;
589 }
590 OH_AVBuffer *buffer = OH_VideoDecoder_GetInputBuffer(vdec_, index);
591 if (buffer == nullptr) {
592 cout << "OH_VideoDecoder_GetInputBuffer fail" << endl;
593 errCount = errCount + 1;
594 continue;
595 }
596 if (!inFile_->eof()) {
597 int ret = PushData(index, buffer);
598 if (ret == 1) {
599 flag = false;
600 break;
601 }
602 }
603 if (sleepOnFPS) {
604 usleep(MICRO_IN_SECOND / (int32_t)DEFAULT_FRAME_RATE);
605 }
606 }
607 }
608
PushData(uint32_t index,OH_AVBuffer * buffer)609 int32_t VDecAPI11Sample::PushData(uint32_t index, OH_AVBuffer *buffer)
610 {
611 OH_AVCodecBufferAttr attr;
612 if (BEFORE_EOS_INPUT && frameCount_ > TEN) {
613 SetEOS(index, buffer);
614 return 1;
615 }
616 if (BEFORE_EOS_INPUT_INPUT && frameCount_ > TEN) {
617 memset_s(&attr, sizeof(OH_AVCodecBufferAttr), 0, sizeof(OH_AVCodecBufferAttr));
618 attr.flags = AVCODEC_BUFFER_FLAGS_EOS;
619 BEFORE_EOS_INPUT_INPUT = false;
620 }
621 char ch[4] = {};
622 (void)inFile_->read(ch, START_CODE_SIZE);
623 if (repeatRun && inFile_->eof()) {
624 static uint32_t repeat_count = 0;
625 inFile_->clear();
626 inFile_->seekg(0, ios::beg);
627 cout << "repeat run " << repeat_count << endl;
628 repeat_count++;
629 return 0;
630 }
631 if (inFile_->eof()) {
632 SetEOS(index, buffer);
633 return 1;
634 }
635 uint32_t bufferSize = (uint32_t)(((ch[3] & 0xFF)) | ((ch[2] & 0xFF) << EIGHT) | ((ch[1] & 0xFF) << SIXTEEN) |
636 ((ch[0] & 0xFF) << TWENTY_FOUR));
637 if (useHDRSource) {
638 uint32_t zero = 0;
639 uint32_t one = 1;
640 uint32_t two = 2;
641 uint32_t three = 3;
642 bufferSize = (uint32_t)(((ch[zero] & 0xFF)) | ((ch[one] & 0xFF) << EIGHT) | ((ch[two] & 0xFF) << SIXTEEN) |
643 ((ch[three] & 0xFF) << TWENTY_FOUR));
644 }
645 if (bufferSize >= DEFAULT_WIDTH * DEFAULT_HEIGHT * THREE >> 1) {
646 cout << "read bufferSize abnormal. buffersize = " << bufferSize << endl;
647 }
648 return SendData(bufferSize, index, buffer);
649 }
650
CheckAndReturnBufferSize(OH_AVBuffer * buffer)651 int32_t VDecAPI11Sample::CheckAndReturnBufferSize(OH_AVBuffer *buffer)
652 {
653 int32_t size = OH_AVBuffer_GetCapacity(buffer);
654 if (maxInputSize > 0 && (size > maxInputSize)) {
655 errCount++;
656 }
657 return size;
658 }
659
SetAttr(OH_AVCodecBufferAttr & attr,uint32_t bufferSize)660 void VDecAPI11Sample::SetAttr(OH_AVCodecBufferAttr &attr, uint32_t bufferSize)
661 {
662 int64_t startPts = GetSystemTimeUs();
663 attr.pts = startPts;
664 attr.size = bufferSize;
665 attr.offset = 0;
666 }
SendData(uint32_t bufferSize,uint32_t index,OH_AVBuffer * buffer)667 uint32_t VDecAPI11Sample::SendData(uint32_t bufferSize, uint32_t index, OH_AVBuffer *buffer)
668 {
669 OH_AVCodecBufferAttr attr;
670 uint8_t *fileBuffer = nullptr;
671 if (bufferSize > 0) {
672 fileBuffer = new uint8_t[bufferSize];
673 } else {
674 delete[] fileBuffer;
675 return 0;
676 }
677 (void)inFile_->read(reinterpret_cast<char *>(fileBuffer), bufferSize);
678 if (bufferSize != 0) {
679 attr.flags = AVCODEC_BUFFER_FLAGS_SYNC_FRAME + AVCODEC_BUFFER_FLAGS_CODEC_DATA;
680 } else {
681 if (!isH263Change) {
682 SetEOS(index, buffer);
683 return 1;
684 } else {
685 return 0;
686 }
687 }
688 int32_t size = CheckAndReturnBufferSize(buffer);
689 if (size < bufferSize) {
690 delete[] fileBuffer;
691 return 0;
692 }
693 uint8_t *avBuffer = OH_AVBuffer_GetAddr(buffer);
694 if (avBuffer == nullptr) {
695 inFile_->clear();
696 inFile_->seekg(0, ios::beg);
697 delete[] fileBuffer;
698 return 0;
699 }
700 if (memcpy_s(avBuffer, size, fileBuffer, bufferSize) != EOK) {
701 delete[] fileBuffer;
702 return 0;
703 }
704 SetAttr(attr, bufferSize);
705 if (isRunning_.load()) {
706 OH_AVBuffer_SetBufferAttr(buffer, &attr);
707 OH_VideoDecoder_PushInputBuffer(vdec_, index) == AV_ERR_OK ? (0) : (errCount++);
708 frameCount_ = frameCount_ + 1;
709 outCount = outCount + 1;
710 if (autoSwitchSurface && (frameCount_ % (int32_t)DEFAULT_FRAME_RATE == 0)) {
711 switchSurfaceFlag = (switchSurfaceFlag == 1) ? 0 : 1;
712 OH_VideoDecoder_SetSurface(vdec_, nativeWindow[switchSurfaceFlag]) == AV_ERR_OK ? (0) : (errCount++);
713 }
714 }
715 delete[] fileBuffer;
716 return 0;
717 }
718
CheckOutputDescription()719 void VDecAPI11Sample::CheckOutputDescription()
720 {
721 OH_AVFormat *newFormat = OH_VideoDecoder_GetOutputDescription(vdec_);
722 if (newFormat != nullptr) {
723 int32_t cropTop = 0;
724 int32_t cropBottom = 0;
725 int32_t cropLeft = 0;
726 int32_t cropRight = 0;
727 int32_t stride = 0;
728 int32_t sliceHeight = 0;
729 int32_t picWidth = 0;
730 int32_t picHeight = 0;
731 OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_CROP_TOP, &cropTop);
732 OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_CROP_BOTTOM, &cropBottom);
733 OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_CROP_LEFT, &cropLeft);
734 OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_CROP_RIGHT, &cropRight);
735 OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_STRIDE, &stride);
736 OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_SLICE_HEIGHT, &sliceHeight);
737 OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_PIC_WIDTH, &picWidth);
738 OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_PIC_HEIGHT, &picHeight);
739 if (cropTop != expectCropTop || cropBottom != expectCropBottom || cropLeft != expectCropLeft) {
740 std::cout << "cropTop:" << cropTop << " cropBottom:" << cropBottom << " cropLeft:" << cropLeft <<std::endl;
741 errCount++;
742 }
743 if (cropRight != expectCropRight || stride <= 0 || sliceHeight <= 0) {
744 std::cout << "cropRight:" << cropRight << std::endl;
745 std::cout << "stride:" << stride << " sliceHeight:" << sliceHeight << std::endl;
746 errCount++;
747 }
748 if (picWidth != originalWidth || picHeight != originalHeight) {
749 std::cout << "picWidth:" << picWidth << " picHeight:" << picHeight << std::endl;
750 errCount++;
751 }
752 } else {
753 errCount++;
754 }
755 OH_AVFormat_Destroy(newFormat);
756 }
757
AutoSwitchSurface()758 void VDecAPI11Sample::AutoSwitchSurface()
759 {
760 if (autoSwitchSurface) {
761 switchSurfaceFlag = (switchSurfaceFlag == 1) ? 0 : 1;
762 if (OH_VideoDecoder_SetSurface(vdec_, nativeWindow[switchSurfaceFlag]) != AV_ERR_OK) {
763 errCount++;
764 }
765 OH_AVFormat *format = OH_AVFormat_Create();
766 int32_t angle = DEFAULT_ANGLE * reinterpret_cast<int32_t>(switchSurfaceFlag);
767 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_ROTATION, angle);
768 OH_VideoDecoder_SetParameter(vdec_, format);
769 OH_AVFormat_Destroy(format);
770 }
771 }
CheckAttrFlag(OH_AVCodecBufferAttr attr)772 int32_t VDecAPI11Sample::CheckAttrFlag(OH_AVCodecBufferAttr attr)
773 {
774 if (needCheckOutputDesc) {
775 CheckOutputDescription();
776 needCheckOutputDesc = false;
777 }
778 if (attr.flags & AVCODEC_BUFFER_FLAGS_EOS) {
779 cout << "AVCODEC_BUFFER_FLAGS_EOS" << endl;
780 AutoSwitchSurface();
781 SHA512_Final(g_md, &g_c);
782 OPENSSL_cleanse(&g_c, sizeof(g_c));
783 MdCompare(g_md, SHA512_DIGEST_LENGTH, fileSourcesha256);
784 return -1;
785 }
786 outFrameCount = outFrameCount + 1;
787 return 0;
788 }
789
OutputFuncTest()790 void VDecAPI11Sample::OutputFuncTest()
791 {
792 FILE *outFile = nullptr;
793 if (outputYuvFlag) {
794 outFile = fopen(OUT_DIR, "wb");
795 }
796 SHA512_Init(&g_c);
797 bool flag = true;
798 while (flag) {
799 if (!isRunning_.load()) {
800 flag = false;
801 break;
802 }
803 OH_AVCodecBufferAttr attr;
804 unique_lock<mutex> lock(signal_->outMutex_);
805 signal_->outCond_.wait(lock, [this]() {
806 if (!isRunning_.load()) {
807 return true;
808 }
809 return signal_->outIdxQueue_.size() > 0;
810 });
811 if (!isRunning_.load()) {
812 flag = false;
813 break;
814 }
815 uint32_t index = signal_->outIdxQueue_.front();
816 OH_AVBuffer *buffer = signal_->outBufferQueue_.front();
817 signal_->outBufferQueue_.pop();
818 signal_->outIdxQueue_.pop();
819 if (OH_AVBuffer_GetBufferAttr(buffer, &attr) != AV_ERR_OK) {
820 errCount = errCount + 1;
821 }
822 lock.unlock();
823 if (CheckAttrFlag(attr) == -1) {
824 flag = false;
825 break;
826 }
827 if (outFile != nullptr) {
828 fwrite(OH_AVBuffer_GetAddr(buffer), 1, attr.size, outFile);
829 }
830 ProcessOutputData(buffer, index, attr.size);
831 if (errCount > 0) {
832 flag = false;
833 break;
834 }
835 }
836 if (outFile) {
837 (void)fclose(outFile);
838 }
839 }
840
SyncOutputFunc()841 void VDecAPI11Sample::SyncOutputFunc()
842 {
843 FILE *outFile = nullptr;
844 if (outputYuvFlag) {
845 outFile = fopen(OUT_DIR, "wb");
846 }
847 SHA512_Init(&g_c);
848 bool flag = true;
849 while (flag) {
850 if (!isRunning_.load()) {
851 flag = false;
852 break;
853 }
854 OH_AVCodecBufferAttr attr;
855 uint32_t index = 0;
856 if (OH_VideoDecoder_QueryOutputBuffer(vdec_, &index, syncOutputWaitTime) != AV_ERR_OK) {
857 continue;
858 }
859 OH_AVBuffer *buffer = OH_VideoDecoder_GetOutputBuffer(vdec_, index);
860 if (buffer == nullptr) {
861 cout << "OH_VideoDecoder_GetOutputBuffer fail" << endl;
862 errCount = errCount + 1;
863 continue;
864 }
865 if (OH_AVBuffer_GetBufferAttr(buffer, &attr) != AV_ERR_OK) {
866 errCount = errCount + 1;
867 }
868 if (SyncOutputFuncEos(attr, index) != AV_ERR_OK) {
869 flag = false;
870 break;
871 }
872 if (outFile != nullptr) {
873 fwrite(OH_AVBuffer_GetAddr(buffer), 1, attr.size, outFile);
874 }
875 ProcessOutputData(buffer, index, attr.size);
876 if (errCount > 0) {
877 flag = false;
878 break;
879 }
880 }
881 if (outFile) {
882 (void)fclose(outFile);
883 }
884 }
885
SyncOutputFuncEos(OH_AVCodecBufferAttr attr,uint32_t index)886 int32_t VDecAPI11Sample::SyncOutputFuncEos(OH_AVCodecBufferAttr attr, uint32_t index)
887 {
888 if (CheckAttrFlag(attr) == -1) {
889 if (queryInputBufferEOS) {
890 OH_VideoDecoder_QueryInputBuffer(vdec_, &index, 0);
891 OH_VideoDecoder_QueryInputBuffer(vdec_, &index, MILLION);
892 OH_VideoDecoder_QueryInputBuffer(vdec_, &index, -1);
893 }
894 if (queryOutputBufferEOS) {
895 OH_VideoDecoder_QueryOutputBuffer(vdec_, &index, 0);
896 OH_VideoDecoder_QueryOutputBuffer(vdec_, &index, MILLION);
897 OH_VideoDecoder_QueryOutputBuffer(vdec_, &index, -1);
898 }
899 return AV_ERR_UNKNOWN;
900 }
901 return AV_ERR_OK;
902 }
903
ProcessOutputData(OH_AVBuffer * buffer,uint32_t index,int32_t size)904 void VDecAPI11Sample::ProcessOutputData(OH_AVBuffer *buffer, uint32_t index, int32_t size)
905 {
906 if (!SF_OUTPUT) {
907 if (size >= DEFAULT_WIDTH * DEFAULT_HEIGHT * THREE >> 1) {
908 uint8_t *cropBuffer = new uint8_t[size];
909 if (memcpy_s(cropBuffer, size, OH_AVBuffer_GetAddr(buffer),
910 DEFAULT_WIDTH * DEFAULT_HEIGHT) != EOK) {
911 cout << "Fatal: memory copy failed Y" << endl;
912 }
913 // copy UV
914 uint32_t uvSize = size - DEFAULT_WIDTH * DEFAULT_HEIGHT;
915 if (memcpy_s(cropBuffer + DEFAULT_WIDTH * DEFAULT_HEIGHT, uvSize,
916 OH_AVBuffer_GetAddr(buffer) + DEFAULT_WIDTH * DEFAULT_HEIGHT, uvSize) != EOK) {
917 cout << "Fatal: memory copy failed UV" << endl;
918 }
919 SHA512_Update(&g_c, cropBuffer, size);
920 delete[] cropBuffer;
921 }
922 if (OH_VideoDecoder_FreeOutputBuffer(vdec_, index) != AV_ERR_OK) {
923 cout << "Fatal: ReleaseOutputBuffer fail" << endl;
924 errCount = errCount + 1;
925 }
926 } else {
927 if (rsAtTime) {
928 int32_t usTimeNum = 1000;
929 int32_t msTimeNum = 1000000;
930 if (renderTimestampNs == 0) {
931 renderTimestampNs = GetSystemTimeUs() * usTimeNum;
932 }
933 renderTimestampNs = renderTimestampNs + (usTimeNum / DEFAULT_FRAME_RATE * msTimeNum);
934 if (OH_VideoDecoder_RenderOutputBufferAtTime(vdec_, index, renderTimestampNs) != AV_ERR_OK) {
935 cout << "Fatal: RenderOutputBufferAtTime fail" << endl;
936 errCount = errCount + 1;
937 }
938 } else {
939 if (OH_VideoDecoder_RenderOutputBuffer(vdec_, index) != AV_ERR_OK) {
940 cout << "Fatal: RenderOutputBuffer fail" << endl;
941 errCount = errCount + 1;
942 }
943 }
944 }
945 }
946
state_EOS()947 int32_t VDecAPI11Sample::state_EOS()
948 {
949 uint32_t index;
950 unique_lock<mutex> lock(signal_->inMutex_);
951 signal_->inCond_.wait(lock, [this]() { return signal_->inIdxQueue_.size() > 0; });
952 index = signal_->inIdxQueue_.front();
953 signal_->inIdxQueue_.pop();
954 signal_->inBufferQueue_.pop();
955 lock.unlock();
956 return OH_VideoDecoder_PushInputBuffer(vdec_, index);
957 }
958
SetEOS(uint32_t index,OH_AVBuffer * buffer)959 void VDecAPI11Sample::SetEOS(uint32_t index, OH_AVBuffer *buffer)
960 {
961 OH_AVCodecBufferAttr attr;
962 attr.pts = 0;
963 attr.size = 0;
964 attr.offset = 0;
965 attr.flags = AVCODEC_BUFFER_FLAGS_EOS;
966 OH_AVBuffer_SetBufferAttr(buffer, &attr);
967 int32_t res = OH_VideoDecoder_PushInputBuffer(vdec_, index);
968 cout << "OH_VideoDecoder_PushInputBuffer EOS res: " << res << endl;
969 }
970
Flush()971 int32_t VDecAPI11Sample::Flush()
972 {
973 unique_lock<mutex> inLock(signal_->inMutex_);
974 clearIntqueue(signal_->inIdxQueue_);
975 signal_->inCond_.notify_all();
976 inLock.unlock();
977 unique_lock<mutex> outLock(signal_->outMutex_);
978 clearIntqueue(signal_->outIdxQueue_);
979 clearBufferqueue(signal_->attrQueue_);
980 signal_->outCond_.notify_all();
981 outLock.unlock();
982 isRunning_.store(false);
983 return OH_VideoDecoder_Flush(vdec_);
984 }
985
Reset()986 int32_t VDecAPI11Sample::Reset()
987 {
988 isRunning_.store(false);
989 StopInloop();
990 StopOutloop();
991 ReleaseInFile();
992 return OH_VideoDecoder_Reset(vdec_);
993 }
994
Release()995 int32_t VDecAPI11Sample::Release()
996 {
997 int ret = 0;
998 if (vdec_ != nullptr) {
999 ret = OH_VideoDecoder_Destroy(vdec_);
1000 vdec_ = nullptr;
1001 }
1002
1003 if (signal_ != nullptr) {
1004 delete signal_;
1005 signal_ = nullptr;
1006 }
1007 return ret;
1008 }
1009
Stop()1010 int32_t VDecAPI11Sample::Stop()
1011 {
1012 StopInloop();
1013 StopOutloop();
1014 ReleaseInFile();
1015 return OH_VideoDecoder_Stop(vdec_);
1016 }
1017
Start()1018 int32_t VDecAPI11Sample::Start()
1019 {
1020 isRunning_.store(true);
1021 return OH_VideoDecoder_Start(vdec_);
1022 }
1023
StopOutloop()1024 void VDecAPI11Sample::StopOutloop()
1025 {
1026 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1027 unique_lock<mutex> lock(signal_->outMutex_);
1028 clearIntqueue(signal_->outIdxQueue_);
1029 clearBufferqueue(signal_->attrQueue_);
1030 isRunning_.store(false);
1031 signal_->outCond_.notify_all();
1032 lock.unlock();
1033 outputLoop_->join();
1034 outputLoop_.reset();
1035 }
1036 }
1037
SetParameter(OH_AVFormat * format)1038 int32_t VDecAPI11Sample::SetParameter(OH_AVFormat *format)
1039 {
1040 return OH_VideoDecoder_SetParameter(vdec_, format);
1041 }
1042
SwitchSurface()1043 int32_t VDecAPI11Sample::SwitchSurface()
1044 {
1045 int32_t ret = OH_VideoDecoder_SetSurface(vdec_, nativeWindow[switchSurfaceFlag]);
1046 switchSurfaceFlag = (switchSurfaceFlag == 1) ? 0 : 1;
1047 cout << "manual switch surf "<< switchSurfaceFlag << endl;
1048 return ret;
1049 }
1050
RepeatCallSetSurface()1051 int32_t VDecAPI11Sample::RepeatCallSetSurface()
1052 {
1053 for (int i = 0; i < REPEAT_CALL_TIME; i++) {
1054 switchSurfaceFlag = (switchSurfaceFlag == 1) ? 0 : 1;
1055 int32_t ret = OH_VideoDecoder_SetSurface(vdec_, nativeWindow[switchSurfaceFlag]);
1056 if (ret != AV_ERR_OK && ret != AV_ERR_OPERATE_NOT_PERMIT && ret != AV_ERR_INVALID_STATE) {
1057 return AV_ERR_OPERATE_NOT_PERMIT;
1058 }
1059 }
1060 return AV_ERR_OK;
1061 }
1062
DecodeSetSurface()1063 int32_t VDecAPI11Sample::DecodeSetSurface()
1064 {
1065 CreateSurface();
1066 return OH_VideoDecoder_SetSurface(vdec_, nativeWindow[0]);
1067 }