• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 <vector>
19 #include <string>
20 #include <sstream>
21 #include "openssl/crypto.h"
22 #include "openssl/sha.h"
23 #include "videodec_api11_sample.h"
24 #include "nlohmann/json.hpp"
25 
26 using namespace OHOS;
27 using namespace OHOS::Media;
28 using namespace std;
29 using namespace nlohmann;
30 namespace {
31 constexpr int64_t NANOS_IN_SECOND = 1000000000L;
32 constexpr int64_t MICRO_IN_SECOND = 1000000L;
33 constexpr int64_t NANOS_IN_MICRO = 1000L;
34 constexpr int32_t THREE = 3;
35 constexpr int32_t EIGHT = 8;
36 constexpr int32_t TEN = 10;
37 constexpr int32_t SIXTEEN = 16;
38 constexpr int32_t TWENTY_FOUR = 24;
39 constexpr uint8_t H264_NALU_TYPE = 0x1f;
40 constexpr uint32_t START_CODE_SIZE = 4;
41 constexpr uint32_t MILLION = 1000000;
42 constexpr uint32_t HUNDRED = 100;
43 constexpr uint8_t START_CODE[START_CODE_SIZE] = {0, 0, 0, 1};
44 constexpr uint8_t SPS = 7;
45 constexpr uint8_t PPS = 8;
46 constexpr int32_t RES_CHANGE_TIME = 4;
47 constexpr int32_t CROP_INFO_SIZE = 2;
48 constexpr int32_t CROP_INFO[RES_CHANGE_TIME][CROP_INFO_SIZE] = {{621, 1103},
49     {1079, 1919}, {719, 1279}, {855, 1919}};
50 
51 constexpr int32_t CROP_BOTTOM = 0;
52 constexpr int32_t CROP_RIGHT = 1;
53 constexpr int32_t DEFAULT_ANGLE = 90;
54 int32_t g_strideSurface = 0;
55 int32_t g_sliceSurface = 0;
56 bool g_yuvSurface = false;
57 SHA512_CTX g_c;
58 uint8_t g_md[SHA512_DIGEST_LENGTH];
59 VDecAPI11Sample *dec_sample = nullptr;
60 
clearIntqueue(std::queue<uint32_t> & q)61 void clearIntqueue(std::queue<uint32_t> &q)
62 {
63     std::queue<uint32_t> empty;
64     swap(empty, q);
65 }
66 
clearBufferqueue(std::queue<OH_AVCodecBufferAttr> & q)67 void clearBufferqueue(std::queue<OH_AVCodecBufferAttr> &q)
68 {
69     std::queue<OH_AVCodecBufferAttr> empty;
70     swap(empty, q);
71 }
72 } // namespace
73 
74 class ConsumerListenerBuffer : public IBufferConsumerListener {
75 public:
ConsumerListenerBuffer(sptr<Surface> cs,std::string_view name)76     ConsumerListenerBuffer(sptr<Surface> cs, std::string_view name) : cs(cs)
77     {
78         outFile_ = std::make_unique<std::ofstream>();
79         outFile_->open(name.data(), std::ios::out | std::ios::binary);
80     };
~ConsumerListenerBuffer()81     ~ConsumerListenerBuffer()
82     {
83         if (outFile_ != nullptr) {
84             outFile_->close();
85         }
86     }
OnBufferAvailable()87     void OnBufferAvailable() override
88     {
89         sptr<SurfaceBuffer> buffer;
90         int32_t flushFence;
91         cs->AcquireBuffer(buffer, flushFence, timestamp, damage);
92         if (buffer == nullptr) {
93             cout << "surface is nullptr" << endl;
94             return;
95         } else {
96             int32_t frameSize = (g_strideSurface * g_sliceSurface * THREE) >> 1;
97             if (g_yuvSurface && outFile_ != nullptr && frameSize <= buffer->GetSize()) {
98                 outFile_->write(reinterpret_cast<char *>(buffer->GetVirAddr()), frameSize);
99             }
100         }
101         cs->ReleaseBuffer(buffer, -1);
102     }
103 
104 private:
105     int64_t timestamp = 0;
106     Rect damage = {};
107     sptr<Surface> cs {nullptr};
108     std::unique_ptr<std::ofstream> outFile_;
109 };
110 
~VDecAPI11Sample()111 VDecAPI11Sample::~VDecAPI11Sample()
112 {
113     for (int i = 0; i < MAX_SURF_NUM; i++) {
114         if (nativeWindow[i]) {
115             OH_NativeWindow_DestroyNativeWindow(nativeWindow[i]);
116             nativeWindow[i] = nullptr;
117         }
118     }
119     g_yuvSurface = false;
120     abnormalIndexValue = false;
121     Stop();
122     Release();
123 }
124 
VdecAPI11Error(OH_AVCodec * codec,int32_t errorCode,void * userData)125 void VdecAPI11Error(OH_AVCodec *codec, int32_t errorCode, void *userData)
126 {
127     if (dec_sample == nullptr) {
128         return;
129     }
130     if ((errorCode == AV_ERR_VIDEO_UNSUPPORTED_COLOR_SPACE_CONVERSION) || (errorCode == AV_ERR_UNSUPPORT)) {
131         dec_sample->isRunning_.store(false);
132         dec_sample->signal_->inCond_.notify_all();
133         dec_sample->signal_->outCond_.notify_all();
134     }
135     cout << "Error errorCode=" << errorCode << endl;
136 }
137 
VdecAPI11FormatChanged(OH_AVCodec * codec,OH_AVFormat * format,void * userData)138 void VdecAPI11FormatChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData)
139 {
140     if (dec_sample == nullptr) {
141         return;
142     }
143     int32_t currentWidth = 0;
144     int32_t currentHeight = 0;
145     int32_t stride = 0;
146     int32_t sliceHeight = 0;
147     int32_t picWidth = 0;
148     int32_t picHeight = 0;
149     OH_AVFormat_GetIntValue(format, OH_MD_KEY_WIDTH, &currentWidth);
150     OH_AVFormat_GetIntValue(format, OH_MD_KEY_HEIGHT, &currentHeight);
151     OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_STRIDE, &stride);
152     OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_SLICE_HEIGHT, &sliceHeight);
153     OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_PIC_WIDTH, &picWidth);
154     OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_PIC_HEIGHT, &picHeight);
155     dec_sample->DEFAULT_WIDTH = currentWidth;
156     dec_sample->DEFAULT_HEIGHT = currentHeight;
157     dec_sample->stride_ = stride;
158     dec_sample->sliceHeight_ = sliceHeight;
159     dec_sample->picWidth_ = picWidth;
160     dec_sample->picHeight_ = picHeight;
161     g_strideSurface = stride;
162     g_sliceSurface = sliceHeight;
163     if (dec_sample->isResChangeStream) {
164         static int32_t resCount = 0;
165         int32_t cropBottom = 0;
166         int32_t cropRight = 0;
167         OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_CROP_BOTTOM, &cropBottom);
168         OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_CROP_RIGHT, &cropRight);
169         if (cropBottom != CROP_INFO[resCount][CROP_BOTTOM] || cropRight != CROP_INFO[resCount][CROP_RIGHT]) {
170             dec_sample->errCount++;
171         }
172         if (stride <= 0 || sliceHeight <= 0) {
173             dec_sample->errCount++;
174         }
175         resCount++;
176     }
177 }
178 
VdecAPI11InputDataReady(OH_AVCodec * codec,uint32_t index,OH_AVBuffer * data,void * userData)179 void VdecAPI11InputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *data, void *userData)
180 {
181     if (dec_sample == nullptr) {
182         return;
183     }
184     if (dec_sample->isFlushing_) {
185         return;
186     }
187     if (dec_sample->inputCallbackFlush && dec_sample->outCount > 1) {
188         dec_sample->Flush();
189         cout << "OH_VideoDecoder_Flush end" << endl;
190         dec_sample->isRunning_.store(false);
191         dec_sample->signal_->inCond_.notify_all();
192         dec_sample->signal_->outCond_.notify_all();
193         return;
194     }
195     if (dec_sample->inputCallbackStop && dec_sample->outCount > 1) {
196         OH_VideoDecoder_Stop(codec);
197         cout << "OH_VideoDecoder_Stop end" << endl;
198         dec_sample->isRunning_.store(false);
199         dec_sample->signal_->inCond_.notify_all();
200         dec_sample->signal_->outCond_.notify_all();
201         return;
202     }
203     VDecAPI11Signal *signal = static_cast<VDecAPI11Signal *>(userData);
204     unique_lock<mutex> lock(signal->inMutex_);
205     signal->inIdxQueue_.push(index);
206     signal->inBufferQueue_.push(data);
207     signal->inCond_.notify_all();
208 }
209 
VdecAPI11OutputDataReady(OH_AVCodec * codec,uint32_t index,OH_AVBuffer * data,void * userData)210 void VdecAPI11OutputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *data, void *userData)
211 {
212     if (dec_sample == nullptr) {
213         return;
214     }
215     if (dec_sample->isFlushing_) {
216         return;
217     }
218     if (dec_sample->outputCallbackFlush && dec_sample->outCount > 1) {
219         dec_sample->Flush();
220         cout << "OH_VideoDecoder_Flush end" << endl;
221         dec_sample->isRunning_.store(false);
222         dec_sample->signal_->inCond_.notify_all();
223         dec_sample->signal_->outCond_.notify_all();
224         return;
225     }
226     if (dec_sample->outputCallbackStop && dec_sample->outCount > 1) {
227         OH_VideoDecoder_Stop(codec);
228         cout << "OH_VideoDecoder_Stop end" << endl;
229         dec_sample->isRunning_.store(false);
230         dec_sample->signal_->inCond_.notify_all();
231         dec_sample->signal_->outCond_.notify_all();
232         return;
233     }
234     VDecAPI11Signal *signal = static_cast<VDecAPI11Signal *>(userData);
235     unique_lock<mutex> lock(signal->outMutex_);
236     signal->outIdxQueue_.push(index);
237     signal->outBufferQueue_.push(data);
238     signal->outCond_.notify_all();
239 }
240 
Flush_buffer()241 void VDecAPI11Sample::Flush_buffer()
242 {
243     unique_lock<mutex> inLock(signal_->inMutex_);
244     clearIntqueue(signal_->inIdxQueue_);
245     std::queue<OH_AVBuffer *> empty;
246     swap(empty, signal_->inBufferQueue_);
247     signal_->inCond_.notify_all();
248     inLock.unlock();
249     unique_lock<mutex> outLock(signal_->outMutex_);
250     clearIntqueue(signal_->outIdxQueue_);
251     clearBufferqueue(signal_->attrQueue_);
252     signal_->outCond_.notify_all();
253     outLock.unlock();
254 }
255 
LoadHashFile()256 std::vector<uint8_t> VDecAPI11Sample::LoadHashFile()
257 {
258     std::ifstream f("/data/test/media/hash_val.json", ios::in);
259     std::vector<uint8_t> ret;
260     if (f) {
261         json data = json::parse(f);
262         filesystem::path filePath = INP_DIR;
263         std::string pixFmt = defualtPixelFormat == AV_PIXEL_FORMAT_NV12 ? "nv12" : "nv21";
264         std::string fileName = filePath.filename();
265         std::string hashValue = data[fileName.c_str()][pixFmt];
266         std::stringstream ss(hashValue);
267         std::string item;
268         while (getline(ss, item, ',')) {
269             if (!item.empty()) {
270                 ret.push_back(stol(item, nullptr, SIXTEEN));
271             }
272         }
273     }
274     return ret;
275 }
276 
DumpHashValue(std::vector<uint8_t> & srcHashVal,uint8_t outputHashVal[])277 static void DumpHashValue(std::vector<uint8_t> &srcHashVal, uint8_t outputHashVal[])
278 {
279     printf("-----------output hash value-----------\n");
280     for (int i = 1; i < SHA512_DIGEST_LENGTH + 1; i++) {
281         printf("%02x,", outputHashVal[i - 1]);
282         if (i % SIXTEEN == 0) {
283             printf("\n");
284         }
285     }
286     printf("-----------standard hash value-----------\n");
287     for (int i = 1; i < SHA512_DIGEST_LENGTH + 1; i++) {
288         printf("%02x,", srcHashVal[i - 1]);
289         if (i % SIXTEEN == 0) {
290             printf("\n");
291         }
292     }
293 }
294 
MdCompare(uint8_t source[])295 bool VDecAPI11Sample::MdCompare(uint8_t source[])
296 {
297     std::vector<uint8_t> srcHashVal = LoadHashFile();
298     DumpHashValue(srcHashVal, source);
299     if (srcHashVal.size() != SHA512_DIGEST_LENGTH) {
300         cout << "get hash value failed, size " << srcHashVal.size() << endl;
301         return false;
302     }
303     for (int32_t i = 0; i < SHA512_DIGEST_LENGTH; i++) {
304         if (source[i] != srcHashVal[i]) {
305             cout << "decoded hash value mismatch" << endl;
306             return false;
307         }
308     }
309     return true;
310 }
311 
GetSystemTimeUs()312 int64_t VDecAPI11Sample::GetSystemTimeUs()
313 {
314     struct timespec now;
315     (void)clock_gettime(CLOCK_BOOTTIME, &now);
316     int64_t nanoTime = (int64_t)now.tv_sec * NANOS_IN_SECOND + now.tv_nsec;
317     return nanoTime / NANOS_IN_MICRO;
318 }
319 
ConfigureVideoDecoder()320 int32_t VDecAPI11Sample::ConfigureVideoDecoder()
321 {
322     if (autoSwitchSurface && enbleSyncMode == 0) {
323         switchSurfaceFlag = (switchSurfaceFlag == 1) ? 0 : 1;
324         if (OH_VideoDecoder_SetSurface(vdec_, nativeWindow[switchSurfaceFlag]) != AV_ERR_INVALID_STATE) {
325             errCount++;
326         }
327     }
328     OH_AVFormat *format = OH_AVFormat_Create();
329     if (format == nullptr) {
330         cout << "Fatal: Failed to create format" << endl;
331         return AV_ERR_UNKNOWN;
332     }
333     if (maxInputSize > 0) {
334         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, maxInputSize);
335     }
336     originalWidth = DEFAULT_WIDTH;
337     originalHeight = DEFAULT_HEIGHT;
338     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
339     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
340     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, defualtPixelFormat);
341     (void)OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, DEFAULT_FRAME_RATE);
342     if (useHDRSource) {
343         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, DEFAULT_PROFILE);
344     } else {
345         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, DEFAULT_PROFILE);
346     }
347 
348     if (TRANSFER_FLAG) {
349         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_DECODER_OUTPUT_COLOR_SPACE, OH_COLORSPACE_BT709_LIMIT);
350     }
351     if (NV21_FLAG) {
352         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV21);
353     }
354     if (enableVRR) {
355         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_DECODER_OUTPUT_ENABLE_VRR, 1);
356     }
357     if (enableLowLatency) {
358         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENABLE_LOW_LATENCY, 1);
359     }
360     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_ENABLE_SYNC_MODE, enbleSyncMode);
361     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_DECODER_BLANK_FRAME_ON_SHUTDOWN, enbleBlankFrame);
362     int ret = OH_VideoDecoder_Configure(vdec_, format);
363     OH_AVFormat_Destroy(format);
364     return ret;
365 }
366 
CreateSurface()367 void VDecAPI11Sample::CreateSurface()
368 {
369     cs[0] = Surface::CreateSurfaceAsConsumer();
370     if (cs[0] == nullptr) {
371         cout << "Create the surface consummer fail" << endl;
372         return;
373     }
374     GSError err = cs[0]->SetDefaultUsage(BUFFER_USAGE_MEM_DMA | BUFFER_USAGE_VIDEO_DECODER | BUFFER_USAGE_CPU_READ);
375     if (err == GSERROR_OK) {
376         cout << "set consumer usage succ" << endl;
377     } else {
378         cout << "set consumer usage failed" << endl;
379     }
380     sptr<IBufferConsumerListener> listener = new ConsumerListenerBuffer(cs[0], OUT_DIR);
381     cs[0]->RegisterConsumerListener(listener);
382     auto p = cs[0]->GetProducer();
383     ps[0] = Surface::CreateSurfaceAsProducer(p);
384     nativeWindow[0] = CreateNativeWindowFromSurface(&ps[0]);
385     if (autoSwitchSurface)  {
386         cs[1] = Surface::CreateSurfaceAsConsumer();
387         sptr<IBufferConsumerListener> listener2 = new ConsumerListenerBuffer(cs[1], OUT_DIR2);
388         cs[1]->RegisterConsumerListener(listener2);
389         auto p2 = cs[1]->GetProducer();
390         ps[1] = Surface::CreateSurfaceAsProducer(p2);
391         nativeWindow[1] = CreateNativeWindowFromSurface(&ps[1]);
392     }
393 }
394 
RunVideoDec_Surface(string codeName)395 int32_t VDecAPI11Sample::RunVideoDec_Surface(string codeName)
396 {
397     SF_OUTPUT = true;
398     int err = AV_ERR_OK;
399     CreateSurface();
400     if (!nativeWindow[0]) {
401         cout << "Failed to create surface" << endl;
402         return AV_ERR_UNKNOWN;
403     }
404 
405     err = CreateVideoDecoder(codeName);
406     if (err != AV_ERR_OK) {
407         cout << "Failed to create video decoder" << endl;
408         return err;
409     }
410 
411     err = SetVideoDecoderCallback();
412     if (err != AV_ERR_OK) {
413         cout << "Failed to setCallback" << endl;
414         Release();
415         return err;
416     }
417 
418     err = ConfigureVideoDecoder();
419     if (err != AV_ERR_OK) {
420         cout << "Failed to configure video decoder" << endl;
421         Release();
422         return err;
423     }
424 
425     err = OH_VideoDecoder_SetSurface(vdec_, nativeWindow[0]);
426     if (err != AV_ERR_OK) {
427         cout << "Failed to set surface" << endl;
428         return err;
429     }
430 
431     err = StartVideoDecoder();
432     if (err != AV_ERR_OK) {
433         cout << "Failed to start video decoder" << endl;
434         Release();
435         return err;
436     }
437     return err;
438 }
439 
RunVideoDec(string codeName)440 int32_t VDecAPI11Sample::RunVideoDec(string codeName)
441 {
442     SF_OUTPUT = false;
443     int err = CreateVideoDecoder(codeName);
444     if (err != AV_ERR_OK) {
445         cout << "Failed to create video decoder" << err << endl;
446         return err;
447     }
448 
449     err = ConfigureVideoDecoder();
450     if (err != AV_ERR_OK) {
451         cout << "Failed to configure video decoder" << err << endl;
452         Release();
453         return err;
454     }
455 
456     err = SetVideoDecoderCallback();
457     if (err != AV_ERR_OK) {
458         cout << "Failed to setCallback" << err << endl;
459         Release();
460         return err;
461     }
462 
463     err = StartVideoDecoder();
464     if (err != AV_ERR_OK) {
465         cout << "Failed to start video decoder" << err << endl;
466         Release();
467         return err;
468     }
469     return err;
470 }
471 
SetVideoDecoderCallback()472 int32_t VDecAPI11Sample::SetVideoDecoderCallback()
473 {
474     signal_ = new VDecAPI11Signal();
475     if (signal_ == nullptr) {
476         cout << "Failed to new VDecAPI11Signal" << endl;
477         return AV_ERR_UNKNOWN;
478     }
479 
480     cb_.onError = VdecAPI11Error;
481     cb_.onStreamChanged = VdecAPI11FormatChanged;
482     cb_.onNeedInputBuffer = VdecAPI11InputDataReady;
483     cb_.onNewOutputBuffer = VdecAPI11OutputDataReady;
484     return OH_VideoDecoder_RegisterCallback(vdec_, cb_, static_cast<void *>(signal_));
485 }
486 
ReleaseInFile()487 void VDecAPI11Sample::ReleaseInFile()
488 {
489     if (inFile_ != nullptr) {
490         if (inFile_->is_open()) {
491             inFile_->close();
492         }
493         inFile_.reset();
494         inFile_ = nullptr;
495     }
496 }
497 
StopInloop()498 void VDecAPI11Sample::StopInloop()
499 {
500     if (inputLoop_ != nullptr && inputLoop_->joinable()) {
501         unique_lock<mutex> lock(signal_->inMutex_);
502         clearIntqueue(signal_->inIdxQueue_);
503         isRunning_.store(false);
504         signal_->inCond_.notify_all();
505         lock.unlock();
506 
507         inputLoop_->join();
508         inputLoop_.reset();
509     }
510 }
511 
CreateVideoDecoder(string codeName)512 int32_t VDecAPI11Sample::CreateVideoDecoder(string codeName)
513 {
514     vdec_ = OH_VideoDecoder_CreateByName(codeName.c_str());
515     dec_sample = this;
516     return vdec_ == nullptr ? AV_ERR_UNKNOWN : AV_ERR_OK;
517 }
518 
StartDecoder()519 int32_t VDecAPI11Sample::StartDecoder()
520 {
521     isRunning_.store(true);
522     inFile_ = make_unique<ifstream>();
523     if (inFile_ == nullptr) {
524         isRunning_.store(false);
525         (void)OH_VideoDecoder_Stop(vdec_);
526         return AV_ERR_UNKNOWN;
527     }
528     inFile_->open(INP_DIR, ios::in | ios::binary);
529     if (!inFile_->is_open()) {
530         cout << "failed open file " << INP_DIR << endl;
531         isRunning_.store(false);
532         (void)OH_VideoDecoder_Stop(vdec_);
533         inFile_->close();
534         inFile_.reset();
535         inFile_ = nullptr;
536         return AV_ERR_UNKNOWN;
537     }
538     inputLoop_ = make_unique<thread>(&VDecAPI11Sample::InputFuncTest, this);
539     if (inputLoop_ == nullptr) {
540         cout << "Failed to create input loop" << endl;
541         isRunning_.store(false);
542         (void)OH_VideoDecoder_Stop(vdec_);
543         ReleaseInFile();
544         return AV_ERR_UNKNOWN;
545     }
546     outputLoop_ = make_unique<thread>(&VDecAPI11Sample::OutputFuncTest, this);
547     if (outputLoop_ == nullptr) {
548         cout << "Failed to create output loop" << endl;
549         isRunning_.store(false);
550         (void)OH_VideoDecoder_Stop(vdec_);
551         ReleaseInFile();
552         StopInloop();
553         Release();
554         return AV_ERR_UNKNOWN;
555     }
556 
557     return AV_ERR_OK;
558 }
559 
StartSyncDecoder()560 int32_t VDecAPI11Sample::StartSyncDecoder()
561 {
562     isRunning_.store(true);
563     inFile_ = make_unique<ifstream>();
564     if (inFile_ == nullptr) {
565         isRunning_.store(false);
566         (void)OH_VideoDecoder_Stop(vdec_);
567         return AV_ERR_UNKNOWN;
568     }
569     inFile_->open(INP_DIR, ios::in | ios::binary);
570     if (!inFile_->is_open()) {
571         cout << "failed open file " << INP_DIR << endl;
572         isRunning_.store(false);
573         (void)OH_VideoDecoder_Stop(vdec_);
574         inFile_->close();
575         inFile_.reset();
576         inFile_ = nullptr;
577         return AV_ERR_UNKNOWN;
578     }
579     signal_ = new VDecAPI11Signal();
580     inputLoop_ = make_unique<thread>(&VDecAPI11Sample::SyncInputFunc, this);
581     if (inputLoop_ == nullptr) {
582         cout << "Failed to create input loop" << endl;
583         isRunning_.store(false);
584         (void)OH_VideoDecoder_Stop(vdec_);
585         ReleaseInFile();
586         return AV_ERR_UNKNOWN;
587     }
588     outputLoop_ = make_unique<thread>(&VDecAPI11Sample::SyncOutputFunc, this);
589     if (outputLoop_ == nullptr) {
590         cout << "Failed to create output loop" << endl;
591         isRunning_.store(false);
592         (void)OH_VideoDecoder_Stop(vdec_);
593         ReleaseInFile();
594         StopInloop();
595         Release();
596         return AV_ERR_UNKNOWN;
597     }
598 
599     return AV_ERR_OK;
600 }
601 
StartVideoDecoder()602 int32_t VDecAPI11Sample::StartVideoDecoder()
603 {
604     isRunning_.store(true);
605     if (PREPARE_FLAG) {
606         int res = OH_VideoDecoder_Prepare(vdec_);
607         if (res != AV_ERR_OK) {
608             cout << "Failed to start codec, prepare failed!  " << res << endl;
609             isRunning_.store(false);
610             ReleaseInFile();
611             Release();
612             return res;
613         }
614     }
615     int ret = OH_VideoDecoder_Start(vdec_);
616     if (ret != AV_ERR_OK) {
617         cout << "Failed to start codec" << endl;
618         isRunning_.store(false);
619         ReleaseInFile();
620         Release();
621         return ret;
622     }
623     if (enbleSyncMode == 0) {
624         ret = StartDecoder();
625     } else {
626         ret = StartSyncDecoder();
627     }
628     return ret;
629 }
630 
testAPI()631 void VDecAPI11Sample::testAPI()
632 {
633     cs[0] = Surface::CreateSurfaceAsConsumer();
634     sptr<IBufferConsumerListener> listener = new ConsumerListenerBuffer(cs[0], OUT_DIR);
635     cs[0]->RegisterConsumerListener(listener);
636     auto p = cs[0]->GetProducer();
637     ps[0] = Surface::CreateSurfaceAsProducer(p);
638     nativeWindow[0] = CreateNativeWindowFromSurface(&ps[0]);
639     OH_VideoDecoder_SetSurface(vdec_, nativeWindow[0]);
640 
641     OH_VideoDecoder_Prepare(vdec_);
642     OH_VideoDecoder_Start(vdec_);
643 
644     OH_AVFormat *format = OH_AVFormat_Create();
645     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
646     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
647     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
648     (void)OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, DEFAULT_FRAME_RATE);
649     OH_VideoDecoder_SetParameter(vdec_, format);
650     OH_AVFormat_Destroy(format);
651     OH_VideoDecoder_GetOutputDescription(vdec_);
652     OH_VideoDecoder_Flush(vdec_);
653     OH_VideoDecoder_Stop(vdec_);
654     OH_VideoDecoder_Reset(vdec_);
655     bool isvalid = false;
656     OH_VideoDecoder_IsValid(vdec_, &isvalid);
657 }
658 
WaitForEOS()659 void VDecAPI11Sample::WaitForEOS()
660 {
661     if (!AFTER_EOS_DESTORY_CODEC && inputLoop_ && inputLoop_->joinable()) {
662         inputLoop_->join();
663     }
664 
665     if (outputLoop_ && outputLoop_->joinable()) {
666         outputLoop_->join();
667     }
668 }
669 
InFuncTest()670 void VDecAPI11Sample::InFuncTest()
671 {
672     if (REPEAT_START_FLUSH_BEFORE_EOS > 0) {
673         REPEAT_START_FLUSH_BEFORE_EOS--;
674         OH_VideoDecoder_Flush(vdec_);
675         Flush_buffer();
676         OH_VideoDecoder_Start(vdec_);
677     }
678     if (REPEAT_START_STOP_BEFORE_EOS > 0) {
679         REPEAT_START_STOP_BEFORE_EOS--;
680         OH_VideoDecoder_Stop(vdec_);
681         Flush_buffer();
682         inFile_->clear();
683         inFile_->seekg(0, ios::beg);
684         OH_VideoDecoder_Start(vdec_);
685     }
686 }
687 
SyncInputFunc()688 void VDecAPI11Sample::SyncInputFunc()
689 {
690     if (outputYuvSurface) {
691         g_yuvSurface = true;
692     }
693     bool flag = true;
694     uint32_t last_index = 0;
695     while (flag) {
696         if (!isRunning_.load()) {
697             flag = false;
698             break;
699         }
700         uint32_t index;
701         if (OH_VideoDecoder_QueryInputBuffer(vdec_, &index, syncInputWaitTime) != AV_ERR_OK) {
702             continue;
703         }
704         OH_AVBuffer *buffer = OH_VideoDecoder_GetInputBuffer(vdec_, index);
705         if (buffer == nullptr) {
706             cout << "OH_VideoDecoder_GetInputBuffer fail" << endl;
707             errCount = errCount + 1;
708             continue;
709         }
710         if (frameCount_ == 0 && getInputBufferIndexRepeat) {
711             last_index = index;
712         } else if (frameCount_ == 1 && getInputBufferIndexRepeat) {
713             cout << "getInputBufferIndexRepeat last_index: " << last_index << "index: " << index << endl;
714             if (OH_VideoDecoder_GetInputBuffer(vdec_, last_index) == nullptr) {
715                 cout << "getInputBufferIndexRepeat OH_VideoDecoder_GetInputBuffer nullptr" << endl;
716                 abnormalIndexValue = true;
717             }
718             break;
719         }
720         if (!inFile_->eof()) {
721             int ret = PushData(index, buffer);
722             if (ret == 1) {
723                 flag = false;
724                 break;
725             }
726         }
727         if (sleepOnFPS) {
728             usleep(MICRO_IN_SECOND / (int32_t)DEFAULT_FRAME_RATE);
729         }
730     }
731 }
732 
InputFuncTest()733 void VDecAPI11Sample::InputFuncTest()
734 {
735     if (outputYuvSurface) {
736         g_yuvSurface = true;
737     }
738     bool flag = true;
739     while (flag) {
740         if (!isRunning_.load()) {
741             flag = false;
742             break;
743         }
744         InFuncTest();
745         uint32_t index;
746         unique_lock<mutex> lock(signal_->inMutex_);
747         signal_->inCond_.wait(lock, [this]() {
748             if (!isRunning_.load()) {
749                 return true;
750             }
751             return signal_->inIdxQueue_.size() > 0 && !isFlushing_.load();
752         });
753         if (!isRunning_.load()) {
754             flag = false;
755             break;
756         }
757         index = signal_->inIdxQueue_.front();
758         auto buffer = signal_->inBufferQueue_.front();
759 
760         signal_->inIdxQueue_.pop();
761         signal_->inBufferQueue_.pop();
762         if (!inFile_->eof()) {
763             int ret = PushData(index, buffer);
764             if (ret == 1) {
765                 flag = false;
766                 break;
767             }
768         }
769         lock.unlock();
770         if (sleepOnFPS) {
771             usleep(MICRO_IN_SECOND / (int32_t)DEFAULT_FRAME_RATE);
772         }
773     }
774 }
775 
PushData(uint32_t index,OH_AVBuffer * buffer)776 int32_t VDecAPI11Sample::PushData(uint32_t index, OH_AVBuffer *buffer)
777 {
778     OH_AVCodecBufferAttr attr;
779     if (BEFORE_EOS_INPUT && frameCount_ > TEN) {
780         SetEOS(index, buffer);
781         return 1;
782     }
783     if (BEFORE_EOS_INPUT_INPUT && frameCount_ > TEN) {
784         memset_s(&attr, sizeof(OH_AVCodecBufferAttr), 0, sizeof(OH_AVCodecBufferAttr));
785         attr.flags = AVCODEC_BUFFER_FLAGS_EOS;
786         BEFORE_EOS_INPUT_INPUT = false;
787     }
788     char ch[4] = {};
789     (void)inFile_->read(ch, START_CODE_SIZE);
790     if (repeatRun && inFile_->eof()) {
791         static uint32_t repeat_count = 0;
792         inFile_->clear();
793         inFile_->seekg(0, ios::beg);
794         cout << "repeat run " << repeat_count << endl;
795         repeat_count++;
796         return 0;
797     }
798     if (inFile_->eof()) {
799         SetEOS(index, buffer);
800         return 1;
801     }
802     uint32_t bufferSize = (uint32_t)(((ch[3] & 0xFF)) | ((ch[2] & 0xFF) << EIGHT) | ((ch[1] & 0xFF) << SIXTEEN) |
803                                      ((ch[0] & 0xFF) << TWENTY_FOUR));
804     if (useHDRSource) {
805         uint32_t zero = 0;
806         uint32_t one = 1;
807         uint32_t two = 2;
808         uint32_t three = 3;
809         bufferSize = (uint32_t)(((ch[zero] & 0xFF)) | ((ch[one] & 0xFF) << EIGHT) | ((ch[two] & 0xFF) << SIXTEEN) |
810                                      ((ch[three] & 0xFF) << TWENTY_FOUR));
811     }
812     if (bufferSize >= DEFAULT_WIDTH * DEFAULT_HEIGHT * THREE >> 1) {
813         cout << "read bufferSize abnormal. buffersize = " << bufferSize << endl;
814     }
815     return SendData(bufferSize, index, buffer);
816 }
817 
CheckAndReturnBufferSize(OH_AVBuffer * buffer)818 int32_t VDecAPI11Sample::CheckAndReturnBufferSize(OH_AVBuffer *buffer)
819 {
820     int32_t size = OH_AVBuffer_GetCapacity(buffer);
821     if (maxInputSize > 0 && (size > maxInputSize)) {
822         errCount++;
823     }
824     return size;
825 }
826 
SendData(uint32_t bufferSize,uint32_t index,OH_AVBuffer * buffer)827 uint32_t VDecAPI11Sample::SendData(uint32_t bufferSize, uint32_t index, OH_AVBuffer *buffer)
828 {
829     OH_AVCodecBufferAttr attr;
830     uint8_t *fileBuffer = new uint8_t[bufferSize + START_CODE_SIZE];
831     if (fileBuffer == nullptr) {
832         delete[] fileBuffer;
833         return 0;
834     }
835     if (memcpy_s(fileBuffer, bufferSize + START_CODE_SIZE, START_CODE, START_CODE_SIZE) != EOK) {
836         cout << "Fatal: memory copy failed" << endl;
837     }
838     (void)inFile_->read(reinterpret_cast<char *>(fileBuffer) + START_CODE_SIZE, bufferSize);
839     if ((fileBuffer[START_CODE_SIZE] & H264_NALU_TYPE) == SPS ||
840         (fileBuffer[START_CODE_SIZE] & H264_NALU_TYPE) == PPS) {
841         attr.flags = AVCODEC_BUFFER_FLAGS_CODEC_DATA;
842     } else {
843         attr.flags = AVCODEC_BUFFER_FLAGS_NONE;
844     }
845     int32_t size = CheckAndReturnBufferSize(buffer);
846     if (size < bufferSize + START_CODE_SIZE) {
847         delete[] fileBuffer;
848         return 0;
849     }
850     uint8_t *avBuffer = OH_AVBuffer_GetAddr(buffer);
851     if (avBuffer == nullptr) {
852         inFile_->clear();
853         inFile_->seekg(0, ios::beg);
854         delete[] fileBuffer;
855         return 0;
856     }
857     if (memcpy_s(avBuffer, size, fileBuffer, bufferSize + START_CODE_SIZE) != EOK) {
858         delete[] fileBuffer;
859         return 0;
860     }
861     int64_t startPts = GetSystemTimeUs();
862     attr.pts = startPts;
863     attr.size = bufferSize + START_CODE_SIZE;
864     attr.offset = 0;
865     if (isRunning_.load()) {
866         OH_AVBuffer_SetBufferAttr(buffer, &attr);
867         OH_VideoDecoder_PushInputBuffer(vdec_, index) == AV_ERR_OK ? (0) : (errCount++);
868         frameCount_ = frameCount_ + 1;
869         outCount = outCount + 1;
870         if (autoSwitchSurface && (frameCount_ % (int32_t)DEFAULT_FRAME_RATE == 0)) {
871             switchSurfaceFlag = (switchSurfaceFlag == 1) ? 0 : 1;
872             OH_VideoDecoder_SetSurface(vdec_, nativeWindow[switchSurfaceFlag]) == AV_ERR_OK ? (0) : (errCount++);
873         }
874     }
875     delete[] fileBuffer;
876     return 0;
877 }
878 
CheckOutputDescription()879 void VDecAPI11Sample::CheckOutputDescription()
880 {
881     OH_AVFormat *newFormat = OH_VideoDecoder_GetOutputDescription(vdec_);
882     if (newFormat != nullptr) {
883         int32_t cropTop = 0;
884         int32_t cropBottom = 0;
885         int32_t cropLeft = 0;
886         int32_t cropRight = 0;
887         int32_t stride = 0;
888         int32_t sliceHeight = 0;
889         int32_t picWidth = 0;
890         int32_t picHeight = 0;
891         OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_CROP_TOP, &cropTop);
892         OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_CROP_BOTTOM, &cropBottom);
893         OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_CROP_LEFT, &cropLeft);
894         OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_CROP_RIGHT, &cropRight);
895         OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_STRIDE, &stride);
896         OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_SLICE_HEIGHT, &sliceHeight);
897         OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_PIC_WIDTH, &picWidth);
898         OH_AVFormat_GetIntValue(newFormat, OH_MD_KEY_VIDEO_PIC_HEIGHT, &picHeight);
899         if (cropTop != expectCropTop || cropBottom != expectCropBottom || cropLeft != expectCropLeft) {
900             std::cout << "cropTop:" << cropTop << " cropBottom:" << cropBottom << " cropLeft:" << cropLeft <<std::endl;
901             errCount++;
902         }
903         if (cropRight != expectCropRight || stride <= 0 || sliceHeight <= 0) {
904             std::cout << "cropRight:" << cropRight << std::endl;
905             std::cout << "stride:" << stride << " sliceHeight:" << sliceHeight << std::endl;
906             errCount++;
907         }
908         if (picWidth != originalWidth || picHeight != originalHeight) {
909             std::cout << "picWidth:" << picWidth << " picHeight:" << picHeight << std::endl;
910             errCount++;
911         }
912     } else {
913         errCount++;
914     }
915     OH_AVFormat_Destroy(newFormat);
916 }
917 
AutoSwitchSurface()918 void VDecAPI11Sample::AutoSwitchSurface()
919 {
920     if (autoSwitchSurface) {
921         switchSurfaceFlag = (switchSurfaceFlag == 1) ? 0 : 1;
922         if (OH_VideoDecoder_SetSurface(vdec_, nativeWindow[switchSurfaceFlag]) != AV_ERR_OK) {
923             errCount++;
924         }
925         OH_AVFormat *format = OH_AVFormat_Create();
926         int32_t angle = DEFAULT_ANGLE * reinterpret_cast<int32_t>(switchSurfaceFlag);
927         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_ROTATION, angle);
928         OH_VideoDecoder_SetParameter(vdec_, format);
929         OH_AVFormat_Destroy(format);
930     }
931 }
932 
CheckAttrFlag(OH_AVCodecBufferAttr attr)933 int32_t VDecAPI11Sample::CheckAttrFlag(OH_AVCodecBufferAttr attr)
934 {
935     if (IS_FIRST_FRAME) {
936         GetStride();
937         IS_FIRST_FRAME = false;
938     }
939     if (needCheckOutputDesc) {
940         CheckOutputDescription();
941         needCheckOutputDesc = false;
942     }
943     if (attr.flags & AVCODEC_BUFFER_FLAGS_EOS) {
944         cout << "AVCODEC_BUFFER_FLAGS_EOS" << endl;
945         AutoSwitchSurface();
946         SHA512_Final(g_md, &g_c);
947         OPENSSL_cleanse(&g_c, sizeof(g_c));
948         if (!SF_OUTPUT && !NocaleHash) {
949             if (!MdCompare(g_md)) {
950                 errCount++;
951             }
952         }
953         return -1;
954     }
955     if (attr.flags == AVCODEC_BUFFER_FLAGS_CODEC_DATA) {
956         cout << "enc AVCODEC_BUFFER_FLAGS_CODEC_DATA" << attr.pts << endl;
957         return 0;
958     }
959     outFrameCount = outFrameCount + 1;
960     return 0;
961 }
962 
GetStride()963 void VDecAPI11Sample::GetStride()
964 {
965     OH_AVFormat *format = OH_VideoDecoder_GetOutputDescription(vdec_);
966     int32_t currentWidth = 0;
967     int32_t currentHeight = 0;
968     int32_t stride = 0;
969     int32_t sliceHeight = 0;
970     int32_t picWidth = 0;
971     int32_t picHeight = 0;
972     OH_AVFormat_GetIntValue(format, OH_MD_KEY_WIDTH, &currentWidth);
973     OH_AVFormat_GetIntValue(format, OH_MD_KEY_HEIGHT, &currentHeight);
974     OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_STRIDE, &stride);
975     OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_SLICE_HEIGHT, &sliceHeight);
976     OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_PIC_WIDTH, &picWidth);
977     OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_PIC_HEIGHT, &picHeight);
978     dec_sample->DEFAULT_WIDTH = currentWidth;
979     dec_sample->DEFAULT_HEIGHT = currentHeight;
980     dec_sample->stride_ = stride;
981     dec_sample->sliceHeight_ = sliceHeight;
982     dec_sample->picWidth_ = picWidth;
983     dec_sample->picHeight_ = picHeight;
984     OH_AVFormat_Destroy(format);
985 }
986 
OutputFuncTest()987 void VDecAPI11Sample::OutputFuncTest()
988 {
989     FILE *outFile = nullptr;
990     if (outputYuvFlag) {
991         outFile = fopen(OUT_DIR, "wb");
992     }
993     SHA512_Init(&g_c);
994     bool flag = true;
995     while (flag) {
996         if (!isRunning_.load()) {
997             flag = false;
998             break;
999         }
1000         OH_AVCodecBufferAttr attr;
1001         unique_lock<mutex> lock(signal_->outMutex_);
1002         signal_->outCond_.wait(lock, [this]() {
1003             if (!isRunning_.load()) {
1004                 return true;
1005             }
1006             return signal_->outIdxQueue_.size() > 0 && !isFlushing_.load();
1007         });
1008         if (!isRunning_.load()) {
1009             flag = false;
1010             break;
1011         }
1012         uint32_t index = signal_->outIdxQueue_.front();
1013         OH_AVBuffer *buffer = signal_->outBufferQueue_.front();
1014         signal_->outBufferQueue_.pop();
1015         signal_->outIdxQueue_.pop();
1016         if (OH_AVBuffer_GetBufferAttr(buffer, &attr) != AV_ERR_OK) {
1017             errCount = errCount + 1;
1018         }
1019         if (CheckAttrFlag(attr) == -1) {
1020             flag = false;
1021             break;
1022         }
1023         if (outFile != nullptr) {
1024             fwrite(OH_AVBuffer_GetAddr(buffer), 1, attr.size, outFile);
1025         }
1026         ProcessOutputData(buffer, index);
1027         lock.unlock();
1028         if (errCount > 0) {
1029             flag = false;
1030             break;
1031         }
1032     }
1033     if (outFile) {
1034         (void)fclose(outFile);
1035     }
1036 }
1037 
SyncOutputFunc()1038 void VDecAPI11Sample::SyncOutputFunc()
1039 {
1040     uint32_t outFrames = 0;
1041     FILE *outFile = nullptr;
1042     if (outputYuvFlag) {
1043         outFile = fopen(OUT_DIR, "wb");
1044     }
1045     SHA512_Init(&g_c);
1046     uint32_t last_index = 0;
1047     while (isRunning_.load()) {
1048         OH_AVCodecBufferAttr attr;
1049         uint32_t index = 0;
1050         int32_t ret = OH_VideoDecoder_QueryOutputBuffer(vdec_, &index, syncOutputWaitTime);
1051         if (ret == AV_ERR_STREAM_CHANGED) {
1052             GetStride();
1053             cout << "stream changed" << ret << endl;
1054             continue;
1055         }
1056         if (ret != AV_ERR_OK) {
1057             continue;
1058         }
1059         OH_AVBuffer *buffer = OH_VideoDecoder_GetOutputBuffer(vdec_, index);
1060         if (buffer == nullptr) {
1061             cout << "OH_VideoDecoder_GetOutputBuffer fail" << endl;
1062             errCount = errCount + 1;
1063             continue;
1064         }
1065         if (SyncOutputFuncEos(last_index, outFrames, index, buffer, attr)) {
1066             isRunning_.store(false);
1067             break;
1068         }
1069         if (outFile != nullptr) {
1070             fwrite(OH_AVBuffer_GetAddr(buffer), 1, attr.size, outFile);
1071         }
1072         ProcessOutputData(buffer, index);
1073         outFrames = outFrames + 1;
1074         if (errCount > 0) {
1075             isRunning_.store(false);
1076             break;
1077         }
1078     }
1079     if (outFile) {
1080         (void)fclose(outFile);
1081     }
1082 }
1083 
SyncOutputFuncEos(uint32_t & last_index,uint32_t & outFrames,uint32_t & index,OH_AVBuffer * buffer,OH_AVCodecBufferAttr & attr)1084 int32_t VDecAPI11Sample::SyncOutputFuncEos(uint32_t &last_index, uint32_t &outFrames, uint32_t &index,
1085     OH_AVBuffer *buffer, OH_AVCodecBufferAttr &attr)
1086 {
1087     if (OH_AVBuffer_GetBufferAttr(buffer, &attr) != AV_ERR_OK) {
1088         errCount = errCount + 1;
1089     }
1090     if (CheckAttrFlag(attr) == -1) {
1091         if (queryInputBufferEOS) {
1092             OH_VideoDecoder_QueryInputBuffer(vdec_, &index, 0);
1093             OH_VideoDecoder_QueryInputBuffer(vdec_, &index, MILLION);
1094             OH_VideoDecoder_QueryInputBuffer(vdec_, &index, -1);
1095         }
1096         if (queryOutputBufferEOS) {
1097             OH_VideoDecoder_QueryOutputBuffer(vdec_, &index, 0);
1098             OH_VideoDecoder_QueryOutputBuffer(vdec_, &index, MILLION);
1099             OH_VideoDecoder_QueryOutputBuffer(vdec_, &index, -1);
1100         }
1101         return AV_ERR_UNKNOWN;
1102     }
1103     if (getOutputBufferIndexNoExisted) {
1104         buffer = OH_VideoDecoder_GetOutputBuffer(vdec_, index + HUNDRED);
1105         if (buffer == nullptr) {
1106             abnormalIndexValue = true;
1107         }
1108     }
1109     if (outFrames == 0 && getOutputBufferIndexRepeated) {
1110         last_index = index;
1111     } else if (outFrames == 1 && getOutputBufferIndexRepeated) {
1112         buffer = OH_VideoDecoder_GetOutputBuffer(vdec_, last_index);
1113         if (buffer == nullptr) {
1114             abnormalIndexValue = true;
1115         }
1116     }
1117     return AV_ERR_OK;
1118 }
1119 
ProcessOutputData(OH_AVBuffer * buffer,uint32_t index)1120 void VDecAPI11Sample::ProcessOutputData(OH_AVBuffer *buffer, uint32_t index)
1121 {
1122     if (!SF_OUTPUT) {
1123         uint8_t *bufferAddr = OH_AVBuffer_GetAddr(buffer);
1124         int32_t size = OH_AVBuffer_GetCapacity(buffer);
1125         uint32_t cropSize = (picWidth_ * picHeight_ * THREE) >> 1;
1126         uint8_t *cropBuffer = new uint8_t[cropSize];
1127         uint8_t *copyPos = cropBuffer;
1128         if (size >= cropSize) {
1129             //copy y
1130             for (int32_t i = 0; i < picHeight_; i++) {
1131                 memcpy_s(copyPos, picWidth_, bufferAddr, picWidth_);
1132                 bufferAddr += stride_;
1133                 copyPos += picWidth_;
1134             }
1135             bufferAddr += (sliceHeight_ - picHeight_) * stride_;
1136             //copy uv
1137             for (int32_t i = 0; i < picHeight_ >> 1; i++) {
1138                 memcpy_s(copyPos, picWidth_, bufferAddr, picWidth_);
1139                 bufferAddr += stride_;
1140                 copyPos += picWidth_;
1141             }
1142             SHA512_Update(&g_c, cropBuffer, cropSize);
1143             delete[] cropBuffer;
1144         }
1145         if (OH_VideoDecoder_FreeOutputBuffer(vdec_, index) != AV_ERR_OK) {
1146             cout << "Fatal: ReleaseOutputBuffer fail" << endl;
1147             errCount = errCount + 1;
1148         }
1149     } else {
1150         if (rsAtTime) {
1151             RenderOutAtTime(index);
1152         } else {
1153             if (OH_VideoDecoder_RenderOutputBuffer(vdec_, index) != AV_ERR_OK) {
1154                 cout << "Fatal: RenderOutputBuffer fail" << endl;
1155                 errCount = errCount + 1;
1156             }
1157         }
1158     }
1159 }
1160 
RenderOutAtTime(uint32_t index)1161 void VDecAPI11Sample::RenderOutAtTime(uint32_t index)
1162 {
1163     if (isAPI) {
1164         OH_AVErrCode code = OH_VideoDecoder_RenderOutputBufferAtTime(vdec_, index, -100000000);
1165         if (code != AV_ERR_OK) {
1166             cout << "Fatal: RenderOutputBufferAtTime fail" << endl;
1167             errCount = code;
1168         }
1169     } else {
1170         int32_t usTimeNum = 1000;
1171         int32_t msTimeNum = 1000000;
1172         if (renderTimestampNs == 0) {
1173             renderTimestampNs = GetSystemTimeUs() * usTimeNum;
1174         }
1175         renderTimestampNs = renderTimestampNs + (usTimeNum / DEFAULT_FRAME_RATE * msTimeNum);
1176         OH_AVErrCode code = OH_VideoDecoder_RenderOutputBufferAtTime(vdec_, index, renderTimestampNs);
1177         if (code != AV_ERR_OK) {
1178             cout << "Fatal: RenderOutputBufferAtTime fail" << endl;
1179             errCount = code;
1180         }
1181     }
1182 }
1183 
state_EOS()1184 int32_t VDecAPI11Sample::state_EOS()
1185 {
1186     uint32_t index;
1187     unique_lock<mutex> lock(signal_->inMutex_);
1188     signal_->inCond_.wait(lock, [this]() { return signal_->inIdxQueue_.size() > 0; });
1189     index = signal_->inIdxQueue_.front();
1190     signal_->inIdxQueue_.pop();
1191     signal_->inBufferQueue_.pop();
1192     lock.unlock();
1193     return OH_VideoDecoder_PushInputBuffer(vdec_, index);
1194 }
1195 
SetEOS(uint32_t index,OH_AVBuffer * buffer)1196 void VDecAPI11Sample::SetEOS(uint32_t index, OH_AVBuffer *buffer)
1197 {
1198     OH_AVCodecBufferAttr attr;
1199     attr.pts = 0;
1200     attr.size = 0;
1201     attr.offset = 0;
1202     attr.flags = AVCODEC_BUFFER_FLAGS_EOS;
1203     OH_AVBuffer_SetBufferAttr(buffer, &attr);
1204     int32_t res = OH_VideoDecoder_PushInputBuffer(vdec_, index);
1205     cout << "OH_VideoDecoder_PushInputBuffer    EOS   res: " << res << endl;
1206 }
1207 
Flush()1208 int32_t VDecAPI11Sample::Flush()
1209 {
1210     isFlushing_.store(true);
1211     if (enbleSyncMode == 0) {
1212         unique_lock<mutex> inLock(signal_->inMutex_);
1213         clearIntqueue(signal_->inIdxQueue_);
1214         signal_->inCond_.notify_all();
1215         inLock.unlock();
1216         unique_lock<mutex> outLock(signal_->outMutex_);
1217         clearIntqueue(signal_->outIdxQueue_);
1218         clearBufferqueue(signal_->attrQueue_);
1219         signal_->outCond_.notify_all();
1220         outLock.unlock();
1221     }
1222     isRunning_.store(false);
1223     int32_t ret = OH_VideoDecoder_Flush(vdec_);
1224     isFlushing_.store(false);
1225     return ret;
1226 }
1227 
Reset()1228 int32_t VDecAPI11Sample::Reset()
1229 {
1230     isRunning_.store(false);
1231     StopInloop();
1232     StopOutloop();
1233     ReleaseInFile();
1234     return OH_VideoDecoder_Reset(vdec_);
1235 }
1236 
Release()1237 int32_t VDecAPI11Sample::Release()
1238 {
1239     int ret = 0;
1240     if (vdec_ != nullptr) {
1241         ret = OH_VideoDecoder_Destroy(vdec_);
1242         vdec_ = nullptr;
1243     }
1244 
1245     if (signal_ != nullptr) {
1246         delete signal_;
1247         signal_ = nullptr;
1248     }
1249     return ret;
1250 }
1251 
Stop()1252 int32_t VDecAPI11Sample::Stop()
1253 {
1254     StopInloop();
1255     StopOutloop();
1256     ReleaseInFile();
1257     return OH_VideoDecoder_Stop(vdec_);
1258 }
1259 
Prepare()1260 int32_t VDecAPI11Sample::Prepare()
1261 {
1262     return OH_VideoDecoder_Prepare(vdec_);
1263 }
1264 
Start()1265 int32_t VDecAPI11Sample::Start()
1266 {
1267     isRunning_.store(true);
1268     return OH_VideoDecoder_Start(vdec_);
1269 }
1270 
QueryInputBuffer(uint32_t index,int64_t timeoutUs)1271 int32_t VDecAPI11Sample::QueryInputBuffer(uint32_t index, int64_t timeoutUs)
1272 {
1273     return OH_VideoDecoder_QueryInputBuffer(vdec_, &index, timeoutUs);
1274 }
1275 
GetInputBuffer(uint32_t index)1276 OH_AVBuffer *VDecAPI11Sample::GetInputBuffer(uint32_t index)
1277 {
1278     return OH_VideoDecoder_GetInputBuffer(vdec_, index);
1279 }
1280 
QueryOutputBuffer(uint32_t index,int64_t timeoutUs)1281 int32_t VDecAPI11Sample::QueryOutputBuffer(uint32_t index, int64_t timeoutUs)
1282 {
1283     return OH_VideoDecoder_QueryOutputBuffer(vdec_, &index, timeoutUs);
1284 }
1285 
GetOutputBuffer(uint32_t index)1286 OH_AVBuffer *VDecAPI11Sample::GetOutputBuffer(uint32_t index)
1287 {
1288     return OH_VideoDecoder_GetOutputBuffer(vdec_, index);
1289 }
1290 
PushInputBuffer(uint32_t index)1291 int32_t VDecAPI11Sample::PushInputBuffer(uint32_t index)
1292 {
1293     return OH_VideoDecoder_PushInputBuffer(vdec_, index);
1294 }
1295 
StopOutloop()1296 void VDecAPI11Sample::StopOutloop()
1297 {
1298     if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1299         unique_lock<mutex> lock(signal_->outMutex_);
1300         clearIntqueue(signal_->outIdxQueue_);
1301         clearBufferqueue(signal_->attrQueue_);
1302         isRunning_.store(false);
1303         signal_->outCond_.notify_all();
1304         lock.unlock();
1305         outputLoop_->join();
1306         outputLoop_.reset();
1307     }
1308 }
1309 
SetParameter(OH_AVFormat * format)1310 int32_t VDecAPI11Sample::SetParameter(OH_AVFormat *format)
1311 {
1312     return OH_VideoDecoder_SetParameter(vdec_, format);
1313 }
1314 
SwitchSurface()1315 int32_t VDecAPI11Sample::SwitchSurface()
1316 {
1317     int32_t ret = OH_VideoDecoder_SetSurface(vdec_, nativeWindow[switchSurfaceFlag]);
1318     switchSurfaceFlag = (switchSurfaceFlag == 1) ? 0 : 1;
1319     cout << "manual switch surf "<< switchSurfaceFlag << endl;
1320     return ret;
1321 }
1322 
RepeatCallSetSurface()1323 int32_t VDecAPI11Sample::RepeatCallSetSurface()
1324 {
1325     for (int i = 0; i < REPEAT_CALL_TIME; i++) {
1326         switchSurfaceFlag = (switchSurfaceFlag == 1) ? 0 : 1;
1327         int32_t ret = OH_VideoDecoder_SetSurface(vdec_, nativeWindow[switchSurfaceFlag]);
1328         if (ret != AV_ERR_OK && ret != AV_ERR_OPERATE_NOT_PERMIT && ret != AV_ERR_INVALID_STATE) {
1329             return AV_ERR_OPERATE_NOT_PERMIT;
1330         }
1331     }
1332     return AV_ERR_OK;
1333 }
1334 
DecodeSetSurface()1335 int32_t VDecAPI11Sample::DecodeSetSurface()
1336 {
1337     CreateSurface();
1338     return OH_VideoDecoder_SetSurface(vdec_, nativeWindow[0]);
1339 }
1340 
OpenFile()1341 int32_t VDecAPI11Sample::OpenFile()
1342 {
1343     inFile_ = make_unique<ifstream>();
1344     if (inFile_ == nullptr) {
1345         return AV_ERR_UNKNOWN;
1346     }
1347     inFile_->open(INP_DIR, ios::in | ios::binary);
1348     if (!inFile_->is_open()) {
1349         cout << "failed open file " << INP_DIR << endl;
1350         inFile_->close();
1351         inFile_.reset();
1352         inFile_ = nullptr;
1353         return AV_ERR_UNKNOWN;
1354     }
1355     return  AV_ERR_OK;
1356 }