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_api11_sample.h"
23 #include <fuzzer/FuzzedDataProvider.h>
24 #include <fstream>
25 using namespace std;
26 using namespace OHOS;
27 using namespace OHOS::Media;
28 #define FUZZ_PROJECT_NAME "hevchwencodersqr_fuzzer"
29
30 VEncAPI11FuzzSample *g_vEncSample = nullptr;
31
SaveCorpus(const uint8_t * data,size_t size,const std::string & filename)32 void SaveCorpus(const uint8_t *data, size_t size, const std::string& filename)
33 {
34 std::ofstream file(filename, std::ios::out | std::ios::binary);
35 if (file.is_open()) {
36 file.write(reinterpret_cast<const char*>(data), size);
37 file.close();
38 }
39 }
40
ReleaseSample()41 bool ReleaseSample()
42 {
43 delete g_vEncSample;
44 g_vEncSample = nullptr;
45 return true;
46 }
47
48 bool g_needRunNormalEncoder = true;
49 namespace OHOS {
HevcHwEncoderApiFuzzTest(const uint8_t * data,size_t size)50 bool HevcHwEncoderApiFuzzTest(const uint8_t *data, size_t size)
51 {
52 if (size < sizeof(int32_t)) {
53 return false;
54 }
55 std::string filename = "/data/test/corpus-EncoderAPI11FuzzTest";
56 SaveCorpus(data, size, filename);
57 FuzzedDataProvider fdp(data, size);
58 bool data2 = fdp.ConsumeBool();
59 int64_t maxBite = 100000000;
60 int32_t sqrFactor = 32;
61 g_vEncSample = new VEncAPI11FuzzSample();
62 g_vEncSample->fuzzData = data;
63 g_vEncSample->fuzzSize = size;
64 g_vEncSample->surfInput = data2;
65 g_vEncSample->fuzzMode = true;
66 g_vEncSample->bEnable = true;
67 g_vEncSample->enablePTSBasedRateControl = true;
68 g_vEncSample->defaultBitrateMode = SQR;
69 g_vEncSample->factorEnable = true;
70 g_vEncSample->maxbiteEnable = true;
71 g_vEncSample->defaultSqrFactor = sqrFactor;
72 g_vEncSample->defaultMaxBitrate = maxBite;
73 OH_AVCapability *cap = OH_AVCodec_GetCapabilityByCategory("video/hevc", true, HARDWARE);
74 string tmpCodecName = OH_AVCapability_GetName(cap);
75 if (g_vEncSample->CreateVideoEncoder(tmpCodecName.c_str()) != AV_ERR_OK) {
76 return ReleaseSample();
77 }
78 if (g_vEncSample->SetVideoEncoderCallback() != AV_ERR_OK) {
79 return ReleaseSample();
80 }
81 if (g_vEncSample->ConfigureVideoEncoder() != AV_ERR_OK) {
82 return ReleaseSample();
83 }
84 if (g_vEncSample->StartVideoEncoder() != AV_ERR_OK) {
85 return ReleaseSample();
86 }
87 g_vEncSample->WaitForEOS();
88 return ReleaseSample();
89 }
90
HevcHwEncoderRandRoiFuzzTest(const uint8_t * data,size_t size)91 bool HevcHwEncoderRandRoiFuzzTest(const uint8_t *data, size_t size)
92 {
93 if (size < sizeof(int32_t)) {
94 return false;
95 }
96 std::string filename = "/data/test/corpus-EncoderAPI11FuzzTest";
97 SaveCorpus(data, size, filename);
98 FuzzedDataProvider fdp(data, size);
99 string data1 = fdp.ConsumeRandomLengthString(10);
100 bool data2 = fdp.ConsumeBool();
101 g_vEncSample = new VEncAPI11FuzzSample();
102 g_vEncSample->fuzzData = data;
103 g_vEncSample->fuzzSize = size;
104 g_vEncSample->surfInput = data2;
105 g_vEncSample->fuzzMode = true;
106 g_vEncSample->roiInfo = data1.c_str();
107 OH_AVCapability *cap = OH_AVCodec_GetCapabilityByCategory("video/hevc", true, HARDWARE);
108 string tmpCodecName = OH_AVCapability_GetName(cap);
109 if (g_vEncSample->CreateVideoEncoder(tmpCodecName.c_str()) != AV_ERR_OK) {
110 return ReleaseSample();
111 }
112 if (g_vEncSample->SetVideoEncoderCallback() != AV_ERR_OK) {
113 return ReleaseSample();
114 }
115 if (g_vEncSample->ConfigureVideoEncoder() != AV_ERR_OK) {
116 return ReleaseSample();
117 }
118 if (g_vEncSample->StartVideoEncoder() != AV_ERR_OK) {
119 return ReleaseSample();
120 }
121 g_vEncSample->WaitForEOS();
122 return ReleaseSample();
123 }
124 } // namespace OHOS
125
126 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)127 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
128 {
129 /* Run your code on data */
130 OHOS::HevcHwEncoderApiFuzzTest(data, size);
131 OHOS::HevcHwEncoderRandRoiFuzzTest(data, size);
132 return 0;
133 }
134