• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstddef>
16 #include <cstdint>
17 
18 #include "native_avcodec_videoencoder.h"
19 #include "native_averrors.h"
20 #include "native_avcodec_base.h"
21 #include "native_avcapability.h"
22 #include "videoenc_inner_sample.h"
23 #include <fuzzer/FuzzedDataProvider.h>
24 using namespace std;
25 using namespace OHOS;
26 using namespace OHOS::MediaAVCodec;
27 #define FUZZ_PROJECT_NAME "encoderinner_fuzzer"
28 
29 VEncNdkInnerFuzzSample *g_vEncSample = nullptr;
30 
ReleaseSample()31 bool ReleaseSample()
32 {
33     delete g_vEncSample;
34     g_vEncSample = nullptr;
35     return false;
36 }
37 
38 namespace OHOS {
39 BufferRequestConfig bufferConfig = {
40     .width = 128,
41     .height = 72,
42     .strideAlignment = 0x8,
43     .format = GraphicPixelFormat::GRAPHIC_PIXEL_FMT_RGBA_8888,
44     .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
45     .timeout = 0,
46 };
47 
EncoderInnerFuzzTest(const uint8_t * data,size_t size)48 bool EncoderInnerFuzzTest(const uint8_t *data, size_t size)
49 {
50     if (size < sizeof(int32_t)) {
51         return false;
52     }
53     g_vEncSample = new VEncNdkInnerFuzzSample();
54     string gCodecMime = "video/avc";
55     string gCodecName = "";
56     OH_AVCapability *cap = OH_AVCodec_GetCapabilityByCategory(gCodecMime.c_str(), true, HARDWARE);
57     if (cap == nullptr) {
58         return ReleaseSample();
59     }
60     const char *tmpCodecName = OH_AVCapability_GetName(cap);
61     if (strcmp(tmpCodecName, "") == 0) {
62         return ReleaseSample();
63     }
64     gCodecName = tmpCodecName;
65     if (!g_vEncSample->GetWaterMarkCapability(gCodecMime)) {
66         return ReleaseSample();
67     }
68     g_vEncSample->inpDir = "/data/test/corpus/1280_720_nv.yuv";
69     g_vEncSample->surfaceInput = true;
70     g_vEncSample->enableWaterMark = true;
71     constexpr uint32_t doubleValue = 2;
72     g_vEncSample->videoCoordinateX = (g_vEncSample->defaultWidth - bufferConfig.width) / doubleValue;
73     g_vEncSample->videoCoordinateY = (g_vEncSample->defaultHeight - bufferConfig.height) / doubleValue;
74     g_vEncSample->videoCoordinateWidth = bufferConfig.width;
75     g_vEncSample->videoCoordinateHeight = bufferConfig.height;
76     if (g_vEncSample->CreateByName(gCodecName) != AV_ERR_OK) {
77         return ReleaseSample();
78     }
79     if (g_vEncSample->SetCallback() != AV_ERR_OK) {
80         return ReleaseSample();
81     }
82     if (g_vEncSample->Configure() != AV_ERR_OK) {
83         return ReleaseSample();
84     }
85     if (g_vEncSample->SetCustomBuffer(bufferConfig, const_cast<uint8_t*>(data), size) != AV_ERR_OK) {
86         return ReleaseSample();
87     }
88     if (g_vEncSample->StartVideoEncoder() != AV_ERR_OK) {
89         return ReleaseSample();
90     }
91     g_vEncSample->WaitForEOS();
92     return ReleaseSample();
93 }
94 } // namespace OHOS
95 
96 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)97 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
98 {
99     /* Run your code on data */
100     OHOS::EncoderInnerFuzzTest(data, size);
101     return 0;
102 }
103