• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, &currentWidth);
124     OH_AVFormat_GetIntValue(format, OH_MD_KEY_HEIGHT, &currentHeight);
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             cout << "OH_VideoDecoder_QueryInputBuffer fail" << endl;
589             continue;
590         }
591         OH_AVBuffer *buffer = OH_VideoDecoder_GetInputBuffer(vdec_, index);
592         if (buffer == nullptr) {
593             cout << "OH_VideoDecoder_GetInputBuffer fail" << endl;
594             errCount = errCount + 1;
595             continue;
596         }
597         if (!inFile_->eof()) {
598             int ret = PushData(index, buffer);
599             if (ret == 1) {
600                 flag = false;
601                 break;
602             }
603         }
604         if (sleepOnFPS) {
605             usleep(MICRO_IN_SECOND / (int32_t)DEFAULT_FRAME_RATE);
606         }
607     }
608 }
609 
PushData(uint32_t index,OH_AVBuffer * buffer)610 int32_t VDecAPI11Sample::PushData(uint32_t index, OH_AVBuffer *buffer)
611 {
612     OH_AVCodecBufferAttr attr;
613     if (BEFORE_EOS_INPUT && frameCount_ > TEN) {
614         SetEOS(index, buffer);
615         return 1;
616     }
617     if (BEFORE_EOS_INPUT_INPUT && frameCount_ > TEN) {
618         memset_s(&attr, sizeof(OH_AVCodecBufferAttr), 0, sizeof(OH_AVCodecBufferAttr));
619         attr.flags = AVCODEC_BUFFER_FLAGS_EOS;
620         BEFORE_EOS_INPUT_INPUT = false;
621     }
622     char ch[4] = {};
623     (void)inFile_->read(ch, START_CODE_SIZE);
624     if (repeatRun && inFile_->eof()) {
625         static uint32_t repeat_count = 0;
626         inFile_->clear();
627         inFile_->seekg(0, ios::beg);
628         cout << "repeat run " << repeat_count << endl;
629         repeat_count++;
630         return 0;
631     }
632     if (inFile_->eof()) {
633         SetEOS(index, buffer);
634         return 1;
635     }
636     uint32_t bufferSize = (uint32_t)(((ch[3] & 0xFF)) | ((ch[2] & 0xFF) << EIGHT) | ((ch[1] & 0xFF) << SIXTEEN) |
637                                      ((ch[0] & 0xFF) << TWENTY_FOUR));
638     if (useHDRSource) {
639         uint32_t zero = 0;
640         uint32_t one = 1;
641         uint32_t two = 2;
642         uint32_t three = 3;
643         bufferSize = (uint32_t)(((ch[zero] & 0xFF)) | ((ch[one] & 0xFF) << EIGHT) | ((ch[two] & 0xFF) << SIXTEEN) |
644                                      ((ch[three] & 0xFF) << TWENTY_FOUR));
645     }
646     if (bufferSize >= DEFAULT_WIDTH * DEFAULT_HEIGHT * THREE >> 1) {
647         cout << "read bufferSize abnormal. buffersize = " << bufferSize << endl;
648     }
649     return SendData(bufferSize, index, buffer);
650 }
651 
CheckAndReturnBufferSize(OH_AVBuffer * buffer)652 int32_t VDecAPI11Sample::CheckAndReturnBufferSize(OH_AVBuffer *buffer)
653 {
654     int32_t size = OH_AVBuffer_GetCapacity(buffer);
655     if (maxInputSize > 0 && (size > maxInputSize)) {
656         errCount++;
657     }
658     return size;
659 }
660 
SetAttr(OH_AVCodecBufferAttr & attr,uint32_t bufferSize)661 void VDecAPI11Sample::SetAttr(OH_AVCodecBufferAttr &attr, uint32_t bufferSize)
662 {
663     int64_t startPts = GetSystemTimeUs();
664     attr.pts = startPts;
665     attr.size = bufferSize;
666     attr.offset = 0;
667 }
SendData(uint32_t bufferSize,uint32_t index,OH_AVBuffer * buffer)668 uint32_t VDecAPI11Sample::SendData(uint32_t bufferSize, uint32_t index, OH_AVBuffer *buffer)
669 {
670     OH_AVCodecBufferAttr attr;
671     uint8_t *fileBuffer = nullptr;
672     if (bufferSize > 0) {
673         fileBuffer = new uint8_t[bufferSize];
674     } else {
675         delete[] fileBuffer;
676         return 0;
677     }
678     (void)inFile_->read(reinterpret_cast<char *>(fileBuffer), bufferSize);
679     if (bufferSize != 0) {
680         attr.flags = AVCODEC_BUFFER_FLAGS_SYNC_FRAME + AVCODEC_BUFFER_FLAGS_CODEC_DATA;
681     } else {
682         if (!isH263Change) {
683             SetEOS(index, buffer);
684             return 1;
685         } else {
686             return 0;
687         }
688     }
689     int32_t size = CheckAndReturnBufferSize(buffer);
690     if (size < bufferSize) {
691         delete[] fileBuffer;
692         return 0;
693     }
694     uint8_t *avBuffer = OH_AVBuffer_GetAddr(buffer);
695     if (avBuffer == nullptr) {
696         inFile_->clear();
697         inFile_->seekg(0, ios::beg);
698         delete[] fileBuffer;
699         return 0;
700     }
701     if (memcpy_s(avBuffer, size, fileBuffer, bufferSize) != EOK) {
702         delete[] fileBuffer;
703         return 0;
704     }
705     SetAttr(attr, bufferSize);
706     if (isRunning_.load()) {
707         OH_AVBuffer_SetBufferAttr(buffer, &attr);
708         OH_VideoDecoder_PushInputBuffer(vdec_, index) == AV_ERR_OK ? (0) : (errCount++);
709         frameCount_ = frameCount_ + 1;
710         outCount = outCount + 1;
711         if (autoSwitchSurface && (frameCount_ % (int32_t)DEFAULT_FRAME_RATE == 0)) {
712             switchSurfaceFlag = (switchSurfaceFlag == 1) ? 0 : 1;
713             OH_VideoDecoder_SetSurface(vdec_, nativeWindow[switchSurfaceFlag]) == AV_ERR_OK ? (0) : (errCount++);
714         }
715     }
716     delete[] fileBuffer;
717     return 0;
718 }
719 
CheckOutputDescription()720 void VDecAPI11Sample::CheckOutputDescription()
721 {
722     OH_AVFormat *newFormat = OH_VideoDecoder_GetOutputDescription(vdec_);
723     if (newFormat != nullptr) {
724         int32_t cropTop = 0;
725         int32_t cropBottom = 0;
726         int32_t cropLeft = 0;
727         int32_t cropRight = 0;
728         int32_t stride = 0;
729         int32_t sliceHeight = 0;
730         int32_t picWidth = 0;
731         int32_t picHeight = 0;
732         OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_CROP_TOP, &cropTop);
733         OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_CROP_BOTTOM, &cropBottom);
734         OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_CROP_LEFT, &cropLeft);
735         OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_CROP_RIGHT, &cropRight);
736         OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_STRIDE, &stride);
737         OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_SLICE_HEIGHT, &sliceHeight);
738         OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_PIC_WIDTH, &picWidth);
739         OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_PIC_HEIGHT, &picHeight);
740         if (cropTop != expectCropTop || cropBottom != expectCropBottom || cropLeft != expectCropLeft) {
741             std::cout << "cropTop:" << cropTop << " cropBottom:" << cropBottom << " cropLeft:" << cropLeft <<std::endl;
742             errCount++;
743         }
744         if (cropRight != expectCropRight || stride <= 0 || sliceHeight <= 0) {
745             std::cout << "cropRight:" << cropRight << std::endl;
746             std::cout << "stride:" << stride << " sliceHeight:" << sliceHeight << std::endl;
747             errCount++;
748         }
749         if (picWidth != originalWidth || picHeight != originalHeight) {
750             std::cout << "picWidth:" << picWidth << " picHeight:" << picHeight << std::endl;
751             errCount++;
752         }
753     } else {
754         errCount++;
755     }
756     OH_AVFormat_Destroy(newFormat);
757 }
758 
AutoSwitchSurface()759 void VDecAPI11Sample::AutoSwitchSurface()
760 {
761     if (autoSwitchSurface) {
762         switchSurfaceFlag = (switchSurfaceFlag == 1) ? 0 : 1;
763         if (OH_VideoDecoder_SetSurface(vdec_, nativeWindow[switchSurfaceFlag]) != AV_ERR_OK) {
764             errCount++;
765         }
766         OH_AVFormat *format = OH_AVFormat_Create();
767         int32_t angle = DEFAULT_ANGLE * reinterpret_cast<int32_t>(switchSurfaceFlag);
768         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_ROTATION, angle);
769         OH_VideoDecoder_SetParameter(vdec_, format);
770         OH_AVFormat_Destroy(format);
771     }
772 }
CheckAttrFlag(OH_AVCodecBufferAttr attr)773 int32_t VDecAPI11Sample::CheckAttrFlag(OH_AVCodecBufferAttr attr)
774 {
775     if (needCheckOutputDesc) {
776         CheckOutputDescription();
777         needCheckOutputDesc = false;
778     }
779     if (attr.flags & AVCODEC_BUFFER_FLAGS_EOS) {
780         cout << "AVCODEC_BUFFER_FLAGS_EOS" << endl;
781         AutoSwitchSurface();
782         SHA512_Final(g_md, &g_c);
783         OPENSSL_cleanse(&g_c, sizeof(g_c));
784         MdCompare(g_md, SHA512_DIGEST_LENGTH, fileSourcesha256);
785         return -1;
786     }
787     outFrameCount = outFrameCount + 1;
788     return 0;
789 }
790 
OutputFuncTest()791 void VDecAPI11Sample::OutputFuncTest()
792 {
793     FILE *outFile = nullptr;
794     if (outputYuvFlag) {
795         outFile = fopen(OUT_DIR, "wb");
796     }
797     SHA512_Init(&g_c);
798     bool flag = true;
799     while (flag) {
800         if (!isRunning_.load()) {
801             flag = false;
802             break;
803         }
804         OH_AVCodecBufferAttr attr;
805         unique_lock<mutex> lock(signal_->outMutex_);
806         signal_->outCond_.wait(lock, [this]() {
807             if (!isRunning_.load()) {
808                 return true;
809             }
810             return signal_->outIdxQueue_.size() > 0;
811         });
812         if (!isRunning_.load()) {
813             flag = false;
814             break;
815         }
816         uint32_t index = signal_->outIdxQueue_.front();
817         OH_AVBuffer *buffer = signal_->outBufferQueue_.front();
818         signal_->outBufferQueue_.pop();
819         signal_->outIdxQueue_.pop();
820         if (OH_AVBuffer_GetBufferAttr(buffer, &attr) != AV_ERR_OK) {
821             errCount = errCount + 1;
822         }
823         lock.unlock();
824         if (CheckAttrFlag(attr) == -1) {
825             flag = false;
826             break;
827         }
828         if (outFile != nullptr) {
829             fwrite(OH_AVBuffer_GetAddr(buffer), 1, attr.size, outFile);
830         }
831         ProcessOutputData(buffer, index, attr.size);
832         if (errCount > 0) {
833             flag = false;
834             break;
835         }
836     }
837     if (outFile) {
838         (void)fclose(outFile);
839     }
840 }
841 
SyncOutputFunc()842 void VDecAPI11Sample::SyncOutputFunc()
843 {
844     FILE *outFile = nullptr;
845     if (outputYuvFlag) {
846         outFile = fopen(OUT_DIR, "wb");
847     }
848     SHA512_Init(&g_c);
849     bool flag = true;
850     while (flag) {
851         if (!isRunning_.load()) {
852             flag = false;
853             break;
854         }
855         OH_AVCodecBufferAttr attr;
856         uint32_t index = 0;
857         if (OH_VideoDecoder_QueryOutputBuffer(vdec_, &index, syncOutputWaitTime) != AV_ERR_OK) {
858             cout << "OH_VideoDecoder_QueryOutputBuffer fail" << endl;
859             continue;
860         }
861         OH_AVBuffer *buffer = OH_VideoDecoder_GetOutputBuffer(vdec_, index);
862         if (buffer == nullptr) {
863             cout << "OH_VideoDecoder_GetOutputBuffer fail" << endl;
864             errCount = errCount + 1;
865             continue;
866         }
867         if (OH_AVBuffer_GetBufferAttr(buffer, &attr) != AV_ERR_OK) {
868             errCount = errCount + 1;
869         }
870         if (SyncOutputFuncEos(attr, index) != AV_ERR_OK) {
871             flag = false;
872             break;
873         }
874         if (outFile != nullptr) {
875             fwrite(OH_AVBuffer_GetAddr(buffer), 1, attr.size, outFile);
876         }
877         ProcessOutputData(buffer, index, attr.size);
878         if (errCount > 0) {
879             flag = false;
880             break;
881         }
882     }
883     if (outFile) {
884         (void)fclose(outFile);
885     }
886 }
887 
SyncOutputFuncEos(OH_AVCodecBufferAttr attr,uint32_t index)888 int32_t VDecAPI11Sample::SyncOutputFuncEos(OH_AVCodecBufferAttr attr, uint32_t index)
889 {
890     if (CheckAttrFlag(attr) == -1) {
891         if (queryInputBufferEOS) {
892             OH_VideoDecoder_QueryInputBuffer(vdec_, &index, 0);
893             OH_VideoDecoder_QueryInputBuffer(vdec_, &index, MILLION);
894             OH_VideoDecoder_QueryInputBuffer(vdec_, &index, -1);
895         }
896         if (queryOutputBufferEOS) {
897             OH_VideoDecoder_QueryOutputBuffer(vdec_, &index, 0);
898             OH_VideoDecoder_QueryOutputBuffer(vdec_, &index, MILLION);
899             OH_VideoDecoder_QueryOutputBuffer(vdec_, &index, -1);
900         }
901         return AV_ERR_UNKNOWN;
902     }
903     return AV_ERR_OK;
904 }
905 
ProcessOutputData(OH_AVBuffer * buffer,uint32_t index,int32_t size)906 void VDecAPI11Sample::ProcessOutputData(OH_AVBuffer *buffer, uint32_t index, int32_t size)
907 {
908     if (!SF_OUTPUT) {
909         if (size >= DEFAULT_WIDTH * DEFAULT_HEIGHT * THREE >> 1) {
910             uint8_t *cropBuffer = new uint8_t[size];
911             if (memcpy_s(cropBuffer, size, OH_AVBuffer_GetAddr(buffer),
912                          DEFAULT_WIDTH * DEFAULT_HEIGHT) != EOK) {
913                 cout << "Fatal: memory copy failed Y" << endl;
914             }
915             // copy UV
916             uint32_t uvSize = size - DEFAULT_WIDTH * DEFAULT_HEIGHT;
917             if (memcpy_s(cropBuffer + DEFAULT_WIDTH * DEFAULT_HEIGHT, uvSize,
918                          OH_AVBuffer_GetAddr(buffer) + DEFAULT_WIDTH * DEFAULT_HEIGHT, uvSize) != EOK) {
919                 cout << "Fatal: memory copy failed UV" << endl;
920             }
921             SHA512_Update(&g_c, cropBuffer, size);
922             delete[] cropBuffer;
923         }
924         if (OH_VideoDecoder_FreeOutputBuffer(vdec_, index) != AV_ERR_OK) {
925             cout << "Fatal: ReleaseOutputBuffer fail" << endl;
926             errCount = errCount + 1;
927         }
928     } else {
929         if (rsAtTime) {
930             int32_t usTimeNum = 1000;
931             int32_t msTimeNum = 1000000;
932             if (renderTimestampNs == 0) {
933                 renderTimestampNs = GetSystemTimeUs() * usTimeNum;
934             }
935             renderTimestampNs = renderTimestampNs + (usTimeNum / DEFAULT_FRAME_RATE * msTimeNum);
936             if (OH_VideoDecoder_RenderOutputBufferAtTime(vdec_, index, renderTimestampNs) != AV_ERR_OK) {
937                 cout << "Fatal: RenderOutputBufferAtTime fail" << endl;
938                 errCount = errCount + 1;
939             }
940         } else {
941             if (OH_VideoDecoder_RenderOutputBuffer(vdec_, index) != AV_ERR_OK) {
942                 cout << "Fatal: RenderOutputBuffer fail" << endl;
943                 errCount = errCount + 1;
944             }
945         }
946     }
947 }
948 
state_EOS()949 int32_t VDecAPI11Sample::state_EOS()
950 {
951     uint32_t index;
952     unique_lock<mutex> lock(signal_->inMutex_);
953     signal_->inCond_.wait(lock, [this]() { return signal_->inIdxQueue_.size() > 0; });
954     index = signal_->inIdxQueue_.front();
955     signal_->inIdxQueue_.pop();
956     signal_->inBufferQueue_.pop();
957     lock.unlock();
958     return OH_VideoDecoder_PushInputBuffer(vdec_, index);
959 }
960 
SetEOS(uint32_t index,OH_AVBuffer * buffer)961 void VDecAPI11Sample::SetEOS(uint32_t index, OH_AVBuffer *buffer)
962 {
963     OH_AVCodecBufferAttr attr;
964     attr.pts = 0;
965     attr.size = 0;
966     attr.offset = 0;
967     attr.flags = AVCODEC_BUFFER_FLAGS_EOS;
968     OH_AVBuffer_SetBufferAttr(buffer, &attr);
969     int32_t res = OH_VideoDecoder_PushInputBuffer(vdec_, index);
970     cout << "OH_VideoDecoder_PushInputBuffer    EOS   res: " << res << endl;
971 }
972 
Flush()973 int32_t VDecAPI11Sample::Flush()
974 {
975     unique_lock<mutex> inLock(signal_->inMutex_);
976     clearIntqueue(signal_->inIdxQueue_);
977     signal_->inCond_.notify_all();
978     inLock.unlock();
979     unique_lock<mutex> outLock(signal_->outMutex_);
980     clearIntqueue(signal_->outIdxQueue_);
981     clearBufferqueue(signal_->attrQueue_);
982     signal_->outCond_.notify_all();
983     outLock.unlock();
984     isRunning_.store(false);
985     return OH_VideoDecoder_Flush(vdec_);
986 }
987 
Reset()988 int32_t VDecAPI11Sample::Reset()
989 {
990     isRunning_.store(false);
991     StopInloop();
992     StopOutloop();
993     ReleaseInFile();
994     return OH_VideoDecoder_Reset(vdec_);
995 }
996 
Release()997 int32_t VDecAPI11Sample::Release()
998 {
999     int ret = 0;
1000     if (vdec_ != nullptr) {
1001         ret = OH_VideoDecoder_Destroy(vdec_);
1002         vdec_ = nullptr;
1003     }
1004 
1005     if (signal_ != nullptr) {
1006         delete signal_;
1007         signal_ = nullptr;
1008     }
1009     return ret;
1010 }
1011 
Stop()1012 int32_t VDecAPI11Sample::Stop()
1013 {
1014     StopInloop();
1015     StopOutloop();
1016     ReleaseInFile();
1017     return OH_VideoDecoder_Stop(vdec_);
1018 }
1019 
Start()1020 int32_t VDecAPI11Sample::Start()
1021 {
1022     isRunning_.store(true);
1023     return OH_VideoDecoder_Start(vdec_);
1024 }
1025 
StopOutloop()1026 void VDecAPI11Sample::StopOutloop()
1027 {
1028     if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1029         unique_lock<mutex> lock(signal_->outMutex_);
1030         clearIntqueue(signal_->outIdxQueue_);
1031         clearBufferqueue(signal_->attrQueue_);
1032         isRunning_.store(false);
1033         signal_->outCond_.notify_all();
1034         lock.unlock();
1035         outputLoop_->join();
1036         outputLoop_.reset();
1037     }
1038 }
1039 
SetParameter(OH_AVFormat * format)1040 int32_t VDecAPI11Sample::SetParameter(OH_AVFormat *format)
1041 {
1042     return OH_VideoDecoder_SetParameter(vdec_, format);
1043 }
1044 
SwitchSurface()1045 int32_t VDecAPI11Sample::SwitchSurface()
1046 {
1047     int32_t ret = OH_VideoDecoder_SetSurface(vdec_, nativeWindow[switchSurfaceFlag]);
1048     switchSurfaceFlag = (switchSurfaceFlag == 1) ? 0 : 1;
1049     cout << "manual switch surf "<< switchSurfaceFlag << endl;
1050     return ret;
1051 }
1052 
RepeatCallSetSurface()1053 int32_t VDecAPI11Sample::RepeatCallSetSurface()
1054 {
1055     for (int i = 0; i < REPEAT_CALL_TIME; i++) {
1056         switchSurfaceFlag = (switchSurfaceFlag == 1) ? 0 : 1;
1057         int32_t ret = OH_VideoDecoder_SetSurface(vdec_, nativeWindow[switchSurfaceFlag]);
1058         if (ret != AV_ERR_OK && ret != AV_ERR_OPERATE_NOT_PERMIT && ret != AV_ERR_INVALID_STATE) {
1059             return AV_ERR_OPERATE_NOT_PERMIT;
1060         }
1061     }
1062     return AV_ERR_OK;
1063 }
1064 
DecodeSetSurface()1065 int32_t VDecAPI11Sample::DecodeSetSurface()
1066 {
1067     CreateSurface();
1068     return OH_VideoDecoder_SetSurface(vdec_, nativeWindow[0]);
1069 }