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 namespace OHOS {
EncoderInnerFuzzTest(const uint8_t * data,size_t size)30 bool EncoderInnerFuzzTest(const uint8_t *data, size_t size)
31 {
32 if (size < sizeof(int32_t)) {
33 return false;
34 }
35 VEncNdkInnerFuzzSample *vEncSample = new VEncNdkInnerFuzzSample();
36 string gCodecMime = "video/avc";
37 string gCodecName = "";
38 OH_AVCapability *cap = OH_AVCodec_GetCapabilityByCategory(gCodecMime.c_str(), true, HARDWARE);
39 if (cap == nullptr) {
40 delete vEncSample;
41 return false;
42 }
43 const char *tmpCodecName = OH_AVCapability_GetName(cap);
44 if (strcmp(tmpCodecName, "") == 0) {
45 delete vEncSample;
46 return false;
47 }
48 gCodecName = tmpCodecName;
49 if (!vEncSample->GetWaterMarkCapability(gCodecMime)) {
50 delete vEncSample;
51 return false;
52 }
53 BufferRequestConfig bufferConfig = {
54 .width = 128,
55 .height = 72,
56 .strideAlignment = 0x8,
57 .format = GraphicPixelFormat::GRAPHIC_PIXEL_FMT_RGBA_8888,
58 .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
59 .timeout = 0,
60 };
61 vEncSample->inpDir = "/data/test/corpus/1280_720_nv.yuv";
62 vEncSample->surfaceInput = true;
63 vEncSample->enableWaterMark = true;
64 constexpr uint32_t doubleValue = 2;
65 vEncSample->videoCoordinateX = (vEncSample->defaultWidth - bufferConfig.width) / doubleValue;
66 vEncSample->videoCoordinateY = (vEncSample->defaultHeight - bufferConfig.height) / doubleValue;
67 vEncSample->videoCoordinateWidth = bufferConfig.width;
68 vEncSample->videoCoordinateHeight = bufferConfig.height;
69
70 vEncSample->CreateByName(gCodecName);
71 vEncSample->SetCallback();
72 vEncSample->Configure();
73 vEncSample->SetCustomBuffer(bufferConfig, const_cast<uint8_t*>(data));
74 vEncSample->StartVideoEncoder();
75 vEncSample->WaitForEOS();
76 delete vEncSample;
77 return true;
78 }
79 } // namespace OHOS
80
81 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)82 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
83 {
84 /* Run your code on data */
85 OHOS::EncoderInnerFuzzTest(data, size);
86 return 0;
87 }
88