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