• 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 #include <fuzzer/FuzzedDataProvider.h>
18 #include <fstream>
19 
20 #include "native_avcodec_videoencoder.h"
21 #include "native_averrors.h"
22 #include "native_avcodec_base.h"
23 #include "native_avcapability.h"
24 #include "videoenc_api11_sample.h"
25 
26 using namespace std;
27 using namespace OHOS;
28 using namespace OHOS::Media;
29 #define FUZZ_PROJECT_NAME "hwencodersetparam_fuzzer"
30 
31 VEncAPI11FuzzSample *g_vEncSample = nullptr;
32 
SaveCorpus(const uint8_t * data,size_t size,const std::string & filename)33 void SaveCorpus(const uint8_t *data, size_t size, const std::string& filename)
34 {
35     std::ofstream file(filename, std::ios::out | std::ios::binary);
36     if (file.is_open()) {
37         file.write(reinterpret_cast<const char*>(data), size);
38         file.close();
39     }
40 }
41 
ReleaseSample()42 bool ReleaseSample()
43 {
44     delete g_vEncSample;
45     g_vEncSample = nullptr;
46     return true;
47 }
48 
49 namespace OHOS {
HwEncoderSetParamFuzzTest(const uint8_t * data,size_t size)50 bool HwEncoderSetParamFuzzTest(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->defaultBitrateMode = SQR;
67     g_vEncSample->factorEnable = true;
68     g_vEncSample->maxbiteEnable = true;
69     g_vEncSample->defaultSqrFactor = sqrFactor;
70     g_vEncSample->defaultMaxBitrate = maxBite;
71     OH_AVCapability *cap = OH_AVCodec_GetCapabilityByCategory("video/hevc", true, HARDWARE);
72     string tmpCodecName = OH_AVCapability_GetName(cap);
73     if (g_vEncSample->CreateVideoEncoder(tmpCodecName.c_str()) != AV_ERR_OK) {
74         return ReleaseSample();
75     }
76     if (g_vEncSample->SetVideoEncoderCallback() != AV_ERR_OK) {
77         return ReleaseSample();
78     }
79     if (g_vEncSample->ConfigureVideoEncoder() != AV_ERR_OK) {
80         return ReleaseSample();
81     }
82     if (g_vEncSample->StartVideoEncoder() != AV_ERR_OK) {
83         return ReleaseSample();
84     }
85     g_vEncSample->SetParameter();
86     g_vEncSample->WaitForEOS();
87     return ReleaseSample();
88 }
89 } // namespace OHOS
90 
91 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)92 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
93 {
94     /* Run your code on data */
95     OHOS::HwEncoderSetParamFuzzTest(data, size);
96     return 0;
97 }
98