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