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