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