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