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 "videodec_api11_sample.h"
19 #include "native_avcapability.h"
20 using namespace OHOS;
21 using namespace OHOS::Media;
22 using namespace std;
23 namespace {
24 const string MIME_TYPE = "video/avc";
25 constexpr int64_t NANOS_IN_SECOND = 1000000000L;
26 constexpr int64_t NANOS_IN_MICRO = 1000L;
27
28 constexpr uint32_t START_CODE_SIZE = 4;
29 constexpr uint8_t SPS = 7;
30 constexpr uint8_t PPS = 8;
31 constexpr int32_t EIGHT = 8;
32 constexpr int32_t SIXTEEN = 16;
33 constexpr int32_t TWENTY_FOUR = 24;
34 constexpr uint8_t H264_NALU_TYPE = 0x1f;
35
36 constexpr uint8_t START_CODE[START_CODE_SIZE] = {0, 0, 0, 1};
37 VDecApi11FuzzSample *g_decSample = nullptr;
38
clearIntqueue(std::queue<uint32_t> & q)39 void clearIntqueue(std::queue<uint32_t> &q)
40 {
41 std::queue<uint32_t> empty;
42 swap(empty, q);
43 }
44
clearAvBufferQueue(std::queue<OH_AVBuffer * > & q)45 void clearAvBufferQueue(std::queue<OH_AVBuffer *> &q)
46 {
47 std::queue<OH_AVBuffer *> empty;
48 swap(empty, q);
49 }
50 } // namespace
51
52 class TestConsumerListener : public IBufferConsumerListener {
53 public:
TestConsumerListener(sptr<Surface> cs)54 TestConsumerListener(sptr<Surface> cs) : cs(cs) {};
~TestConsumerListener()55 ~TestConsumerListener() {}
OnBufferAvailable()56 void OnBufferAvailable() override
57 {
58 sptr<SurfaceBuffer> buffer;
59 int32_t flushFence;
60 cs->AcquireBuffer(buffer, flushFence, timestamp, damage);
61
62 cs->ReleaseBuffer(buffer, -1);
63 }
64
65 private:
66 int64_t timestamp = 0;
67 Rect damage = {};
68 sptr<Surface> cs {nullptr};
69 };
70
~VDecApi11FuzzSample()71 VDecApi11FuzzSample::~VDecApi11FuzzSample()
72 {
73 Release();
74 }
75
VdecError(OH_AVCodec * codec,int32_t errorCode,void * userData)76 void VdecError(OH_AVCodec *codec, int32_t errorCode, void *userData)
77 {
78 cout << "Error errorCode=" << errorCode << endl;
79 g_decSample->isRunning_.store(false);
80 g_decSample->signal_->inCond_.notify_all();
81 }
82
VdecFormatChanged(OH_AVCodec * codec,OH_AVFormat * format,void * userData)83 void VdecFormatChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData)
84 {
85 cout << "Format Changed" << endl;
86 int32_t currentWidth = 0;
87 int32_t currentHeight = 0;
88 OH_AVFormat_GetIntValue(format, OH_MD_KEY_WIDTH, ¤tWidth);
89 OH_AVFormat_GetIntValue(format, OH_MD_KEY_HEIGHT, ¤tHeight);
90 g_decSample->defaultWidth = currentWidth;
91 g_decSample->defaultHeight = currentHeight;
92 }
93
VdecInputDataReady(OH_AVCodec * codec,uint32_t index,OH_AVBuffer * buffer,void * userData)94 void VdecInputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *buffer, void *userData)
95 {
96 VDecSignal *signal = static_cast<VDecSignal *>(userData);
97 if (signal == nullptr) {
98 return;
99 }
100 unique_lock<mutex> lock(signal->inMutex_);
101 signal->inIdxQueue_.push(index);
102 signal->inBufferQueue_.push(buffer);
103 signal->inCond_.notify_all();
104 }
105
VdecOutputDataReady(OH_AVCodec * codec,uint32_t index,OH_AVBuffer * buffer,void * userData)106 void VdecOutputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *buffer, void *userData)
107 {
108 int32_t ret = 0;
109 if (g_decSample->isSurfMode) {
110 if (g_decSample->isRenderAttime) {
111 ret = OH_VideoDecoder_RenderOutputBufferAtTime(codec, index, g_decSample->renderTimestampNs);
112 } else {
113 ret = OH_VideoDecoder_RenderOutputBuffer(codec, index);
114 }
115 } else {
116 ret = OH_VideoDecoder_FreeOutputBuffer(codec, index);
117 }
118 if (ret != AV_ERR_OK) {
119 g_decSample->Flush();
120 g_decSample->Start();
121 }
122 }
123
GetSystemTimeUs()124 int64_t VDecApi11FuzzSample::GetSystemTimeUs()
125 {
126 struct timespec now;
127 (void)clock_gettime(CLOCK_BOOTTIME, &now);
128 int64_t nanoTime = static_cast<int64_t>(now.tv_sec) * NANOS_IN_SECOND + now.tv_nsec;
129 return nanoTime / NANOS_IN_MICRO;
130 }
131
ConfigureVideoDecoder()132 int32_t VDecApi11FuzzSample::ConfigureVideoDecoder()
133 {
134 OH_AVFormat *format = OH_AVFormat_Create();
135 if (format == nullptr) {
136 cout << "Fatal: Failed to create format" << endl;
137 return AV_ERR_UNKNOWN;
138 }
139 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, HEVC_PROFILE_MAIN_10);
140 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, defaultWidth);
141 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, defaultHeight);
142 (void)OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, defaultFrameRate);
143 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_ROTATION, 0);
144 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
145 int ret = OH_VideoDecoder_Configure(vdec_, format);
146 OH_AVFormat_Destroy(format);
147 if (isSurfMode) {
148 cs = Surface::CreateSurfaceAsConsumer();
149 sptr<IBufferConsumerListener> listener = new TestConsumerListener(cs);
150 cs->RegisterConsumerListener(listener);
151 auto p = cs->GetProducer();
152 ps = Surface::CreateSurfaceAsProducer(p);
153 nativeWindow = CreateNativeWindowFromSurface(&ps);
154 OH_VideoDecoder_SetSurface(vdec_, nativeWindow);
155 }
156 return ret;
157 }
158
SetVideoDecoderCallback()159 int32_t VDecApi11FuzzSample::SetVideoDecoderCallback()
160 {
161 signal_ = new VDecSignal();
162 if (signal_ == nullptr) {
163 cout << "Failed to new VDecSignal" << endl;
164 return AV_ERR_UNKNOWN;
165 }
166
167 cb_.onError = VdecError;
168 cb_.onStreamChanged = VdecFormatChanged;
169 cb_.onNeedInputBuffer = VdecInputDataReady;
170 cb_.onNewOutputBuffer = VdecOutputDataReady;
171 OH_VideoDecoder_RegisterCallback(vdec_, cb_, static_cast<void *>(signal_));
172 return OH_VideoDecoder_RegisterCallback(vdec_, cb_, static_cast<void *>(signal_));
173 }
174
CreateVideoDecoder()175 int32_t VDecApi11FuzzSample::CreateVideoDecoder()
176 {
177 OH_AVCapability *cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_HEVC, false, HARDWARE);
178 string codecName = OH_AVCapability_GetName(cap);
179 vdec_ = OH_VideoDecoder_CreateByName("aabbcc");
180 if (vdec_) {
181 OH_VideoDecoder_Destroy(vdec_);
182 vdec_ = nullptr;
183 }
184 OH_AVCodec *tmpDec = OH_VideoDecoder_CreateByMime("aabbcc");
185 if (tmpDec) {
186 OH_VideoDecoder_Destroy(tmpDec);
187 tmpDec = nullptr;
188 }
189 tmpDec = OH_VideoDecoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_HEVC);
190 if (tmpDec) {
191 OH_VideoDecoder_Destroy(tmpDec);
192 tmpDec = nullptr;
193 }
194 vdec_ = OH_VideoDecoder_CreateByName(codecName.c_str());
195 g_decSample = this;
196 return vdec_ == nullptr ? AV_ERR_UNKNOWN : AV_ERR_OK;
197 }
198
WaitForEOS()199 void VDecApi11FuzzSample::WaitForEOS()
200 {
201 if (inputLoop_ && inputLoop_->joinable()) {
202 inputLoop_->join();
203 }
204 }
205
InputFuncFUZZ(const uint8_t * data,size_t size)206 OH_AVErrCode VDecApi11FuzzSample::InputFuncFUZZ(const uint8_t *data, size_t size)
207 {
208 uint32_t index;
209 unique_lock<mutex> lock(signal_->inMutex_);
210 if (!isRunning_.load()) {
211 return AV_ERR_NO_MEMORY;
212 }
213 signal_->inCond_.wait(lock, [this]() {
214 if (!isRunning_.load()) {
215 return true;
216 }
217 return signal_->inIdxQueue_.size() > 0;
218 });
219 if (!isRunning_.load()) {
220 return AV_ERR_NO_MEMORY;
221 }
222 index = signal_->inIdxQueue_.front();
223 auto buffer = signal_->inBufferQueue_.front();
224 signal_->inIdxQueue_.pop();
225 signal_->inBufferQueue_.pop();
226 lock.unlock();
227 int32_t bufferSize = OH_AVBuffer_GetCapacity(buffer);
228 uint8_t *bufferAddr = OH_AVBuffer_GetAddr(buffer);
229 if (size > bufferSize - START_CODE_SIZE) {
230 OH_VideoDecoder_PushInputBuffer(vdec_, index);
231 return AV_ERR_NO_MEMORY;
232 }
233 if (memcpy_s(bufferAddr, bufferSize, START_CODE, START_CODE_SIZE) != EOK) {
234 OH_VideoDecoder_PushInputBuffer(vdec_, index);
235 return AV_ERR_NO_MEMORY;
236 }
237 if (memcpy_s(bufferAddr + START_CODE_SIZE, bufferSize - START_CODE_SIZE, data, size) != EOK) {
238 OH_VideoDecoder_PushInputBuffer(vdec_, index);
239 cout << "Fatal: memcpy fail" << endl;
240 return AV_ERR_NO_MEMORY;
241 }
242 OH_AVCodecBufferAttr attr;
243 attr.pts = GetSystemTimeUs();
244 attr.size = size;
245 attr.offset = 0;
246 attr.flags = AVCODEC_BUFFER_FLAGS_NONE;
247 OH_AVBuffer_SetBufferAttr(buffer, &attr);
248 OH_AVErrCode ret = OH_VideoDecoder_PushInputBuffer(vdec_, index);
249 return ret;
250 }
251
SetEOS(OH_AVBuffer * buffer,uint32_t index)252 void VDecApi11FuzzSample::SetEOS(OH_AVBuffer *buffer, uint32_t index)
253 {
254 OH_AVCodecBufferAttr attr;
255 attr.pts = 0;
256 attr.size = 0;
257 attr.offset = 0;
258 attr.flags = AVCODEC_BUFFER_FLAGS_EOS;
259 OH_AVBuffer_SetBufferAttr(buffer, &attr);
260 int32_t res = OH_VideoDecoder_PushInputBuffer(vdec_, index);
261 cout << "OH_VideoDecoder_PushInputData EOS res: " << res << endl;
262 }
263
Flush()264 int32_t VDecApi11FuzzSample::Flush()
265 {
266 unique_lock<mutex> inLock(signal_->inMutex_);
267 clearIntqueue(signal_->inIdxQueue_);
268 clearAvBufferQueue(signal_->inBufferQueue_);
269 signal_->inCond_.notify_all();
270 inLock.unlock();
271 unique_lock<mutex> outLock(signal_->outMutex_);
272 clearIntqueue(signal_->outIdxQueue_);
273 signal_->outCond_.notify_all();
274 isRunning_.store(false);
275 outLock.unlock();
276
277 return OH_VideoDecoder_Flush(vdec_);
278 }
279
Reset()280 int32_t VDecApi11FuzzSample::Reset()
281 {
282 isRunning_.store(false);
283 return OH_VideoDecoder_Reset(vdec_);
284 }
285
Release()286 int32_t VDecApi11FuzzSample::Release()
287 {
288 int ret = 0;
289 if (vdec_ != nullptr) {
290 ret = OH_VideoDecoder_Destroy(vdec_);
291 vdec_ = nullptr;
292 }
293 if (signal_ != nullptr) {
294 clearAvBufferQueue(signal_->inBufferQueue_);
295 delete signal_;
296 signal_ = nullptr;
297 }
298 return ret;
299 }
300
Stop()301 int32_t VDecApi11FuzzSample::Stop()
302 {
303 clearIntqueue(signal_->outIdxQueue_);
304 isRunning_.store(false);
305 return OH_VideoDecoder_Stop(vdec_);
306 }
307
Start()308 int32_t VDecApi11FuzzSample::Start()
309 {
310 int32_t ret = OH_VideoDecoder_Start(vdec_);
311 if (ret == AV_ERR_OK) {
312 isRunning_.store(true);
313 }
314 return ret;
315 }
316
SetParameter(int32_t data)317 void VDecApi11FuzzSample::SetParameter(int32_t data)
318 {
319 OH_AVFormat *format = OH_AVFormat_Create();
320 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, data);
321 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, data);
322 OH_VideoDecoder_SetParameter(vdec_, format);
323 OH_AVFormat_Destroy(format);
324 }
325
StopInloop()326 void VDecApi11FuzzSample::StopInloop()
327 {
328 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
329 unique_lock<mutex> lock(signal_->inMutex_);
330 clearIntqueue(signal_->inIdxQueue_);
331 isRunning_.store(false);
332 signal_->inCond_.notify_all();
333 lock.unlock();
334
335 inputLoop_->join();
336 inputLoop_.reset();
337 }
338 }
339
ReleaseInFile()340 void VDecApi11FuzzSample::ReleaseInFile()
341 {
342 if (inFile_ != nullptr) {
343 if (inFile_->is_open()) {
344 inFile_->close();
345 }
346 inFile_.reset();
347 inFile_ = nullptr;
348 }
349 }
350
StartVideoDecoder()351 int32_t VDecApi11FuzzSample::StartVideoDecoder()
352 {
353 isRunning_.store(true);
354 int ret = OH_VideoDecoder_Start(vdec_);
355 if (ret != AV_ERR_OK) {
356 isRunning_.store(false);
357 ReleaseInFile();
358 Release();
359 cout << "Failed to start codec" << endl;
360 return ret;
361 }
362 inFile_ = make_unique<ifstream>();
363 if (inFile_ == nullptr) {
364 isRunning_.store(false);
365 (void)OH_VideoDecoder_Stop(vdec_);
366 return AV_ERR_UNKNOWN;
367 }
368 inFile_->open(inpDir, ios::in | ios::binary);
369 if (!inFile_->is_open()) {
370 cout << "open input file failed" << endl;
371 isRunning_.store(false);
372 (void)OH_VideoDecoder_Stop(vdec_);
373 inFile_->close();
374 inFile_.reset();
375 inFile_ = nullptr;
376 return AV_ERR_UNKNOWN;
377 }
378
379 inputLoop_ = make_unique<thread>(&VDecApi11FuzzSample::InputFuncTest, this);
380 if (inputLoop_ == nullptr) {
381 cout << "Failed to create input loop" << endl;
382 isRunning_.store(false);
383 (void)OH_VideoDecoder_Stop(vdec_);
384 ReleaseInFile();
385 return AV_ERR_UNKNOWN;
386 }
387 return AV_ERR_OK;
388 }
389
InputFuncTest()390 void VDecApi11FuzzSample::InputFuncTest()
391 {
392 while (isRunning_.load()) {
393 uint32_t index;
394 unique_lock<mutex> lock(signal_->inMutex_);
395 signal_->inCond_.wait(lock, [this]() {
396 if (!isRunning_.load()) {
397 return true;
398 }
399 return signal_->inIdxQueue_.size() > 0;
400 });
401 if (!isRunning_.load()) {
402 break;
403 }
404 index = signal_->inIdxQueue_.front();
405 auto buffer = signal_->inBufferQueue_.front();
406
407 signal_->inIdxQueue_.pop();
408 signal_->inBufferQueue_.pop();
409 lock.unlock();
410 if (!inFile_->eof()) {
411 int ret = PushData(index, buffer);
412 if (ret == 1) {
413 break;
414 }
415 }
416 }
417 }
418
PushData(uint32_t index,OH_AVBuffer * buffer)419 int32_t VDecApi11FuzzSample::PushData(uint32_t index, OH_AVBuffer *buffer)
420 {
421 char ch[4] = {};
422 (void)inFile_->read(ch, START_CODE_SIZE);
423 if (inFile_->eof()) {
424 SetEOS(buffer, index);
425 return 1;
426 }
427 uint32_t bufferSize = static_cast<uint32_t>(((ch[3] & 0xFF)) | ((ch[2] & 0xFF) << EIGHT) |
428 ((ch[1] & 0xFF) << SIXTEEN) | ((ch[0] & 0xFF) << TWENTY_FOUR));
429
430 return SendData(bufferSize, index, buffer);
431 }
432
433
SendData(uint32_t bufferSize,uint32_t index,OH_AVBuffer * buffer)434 uint32_t VDecApi11FuzzSample::SendData(uint32_t bufferSize, uint32_t index, OH_AVBuffer *buffer)
435 {
436 OH_AVCodecBufferAttr attr;
437 uint8_t *fileBuffer = new uint8_t[bufferSize + START_CODE_SIZE];
438 if (fileBuffer == nullptr) {
439 delete[] fileBuffer;
440 return 0;
441 }
442 if (memcpy_s(fileBuffer, bufferSize + START_CODE_SIZE, START_CODE, START_CODE_SIZE) != EOK) {
443 cout << "Fatal: memory copy failed" << endl;
444 }
445 (void)inFile_->read((char *)fileBuffer + START_CODE_SIZE, bufferSize);
446 if ((fileBuffer[START_CODE_SIZE] & H264_NALU_TYPE) == SPS ||
447 (fileBuffer[START_CODE_SIZE] & H264_NALU_TYPE) == PPS) {
448 attr.flags = AVCODEC_BUFFER_FLAGS_CODEC_DATA;
449 } else {
450 attr.flags = AVCODEC_BUFFER_FLAGS_NONE;
451 }
452 int32_t size = OH_AVBuffer_GetCapacity(buffer);
453 if (size < bufferSize + START_CODE_SIZE) {
454 delete[] fileBuffer;
455 return 0;
456 }
457 uint8_t *avBuffer = OH_AVBuffer_GetAddr(buffer);
458 if (avBuffer == nullptr) {
459 cout << "avBuffer == nullptr" << endl;
460 inFile_->clear();
461 inFile_->seekg(0, ios::beg);
462 delete[] fileBuffer;
463 return 0;
464 }
465 if (memcpy_s(avBuffer, size, fileBuffer, bufferSize + START_CODE_SIZE) != EOK) {
466 delete[] fileBuffer;
467 return 0;
468 }
469 int64_t startPts = GetSystemTimeUs();
470 attr.pts = startPts;
471 attr.size = bufferSize + START_CODE_SIZE;
472 attr.offset = 0;
473 OH_AVBuffer_SetBufferAttr(buffer, &attr);
474 if (isRunning_.load()) {
475 OH_VideoDecoder_PushInputBuffer(vdec_, index) == AV_ERR_OK ? (0) : (errCount++);
476 frameCount_ = frameCount_ + 1;
477 }
478 delete[] fileBuffer;
479 return 0;
480 }