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