1 /*
2 * Copyright (c) 2023 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 "native_avcodec_base.h"
18 #include "native_avformat.h"
19 #include "videoenc_sample.h"
20 #include "native_avcapability.h"
21 #include <fuzzer/FuzzedDataProvider.h>
22 #define FUZZ_PROJECT_NAME "encodersetparameter_fuzzer"
23 using namespace std;
24 using namespace OHOS;
25 using namespace OHOS::Media;
26 static VEncFuzzSample *vEncSample = nullptr;
27 constexpr uint32_t DEFAULT_WIDTH = 1280;
28 constexpr uint32_t DEFAULT_HEIGHT = 720;
29 constexpr double DEFAULT_FRAME_RATE = 30.0;
30 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)31 bool DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
32 {
33 if (size < sizeof(int64_t)) {
34 return false;
35 }
36 if (!vEncSample) {
37 vEncSample = new VEncFuzzSample();
38 vEncSample->defaultWidth = DEFAULT_WIDTH;
39 vEncSample->defaultHeight = DEFAULT_HEIGHT;
40 vEncSample->defaultFrameRate = DEFAULT_FRAME_RATE;
41 vEncSample->fuzzMode = true;
42 OH_AVCapability *cap = OH_AVCodec_GetCapabilityByCategory("video/avc", true, HARDWARE);
43 string tmpCodecName = OH_AVCapability_GetName(cap);
44 vEncSample->CreateVideoEncoder(tmpCodecName.c_str());
45 vEncSample->SetVideoEncoderCallback();
46 vEncSample->ConfigureVideoEncoder();
47 vEncSample->Start();
48 }
49
50 OH_AVFormat *format = OH_AVFormat_CreateVideoFormat("video/avc", DEFAULT_WIDTH, DEFAULT_HEIGHT);
51 FuzzedDataProvider fdp(data, size);
52 int intData0 = fdp.ConsumeIntegral<int32_t>();
53 int intData1 = fdp.ConsumeIntegral<int32_t>();
54 int intData2 = fdp.ConsumeIntegral<int32_t>();
55 int intData3 = fdp.ConsumeIntegral<int32_t>();
56 int intData4 = fdp.ConsumeIntegral<int32_t>();
57 int intData5 = fdp.ConsumeIntegral<int32_t>();
58 int intData6 = fdp.ConsumeIntegral<int32_t>();
59 int intData7 = fdp.ConsumeIntegral<int32_t>();
60 int intData8 = fdp.ConsumeIntegral<int32_t>();
61 int longData = fdp.ConsumeIntegral<int64_t>();
62 double doubleData = fdp.ConsumeFloatingPoint<double>();
63 OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, intData0);
64 OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, intData1);
65 OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, intData2);
66 OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, intData3);
67 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, intData4);
68 OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, intData5);
69 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, intData6);
70 OH_AVFormat_SetIntValue(format, OH_MD_KEY_I_FRAME_INTERVAL, intData7);
71 OH_AVFormat_SetIntValue(format, OH_MD_KEY_ROTATION, intData8);
72 OH_AVFormat_SetLongValue(format, OH_MD_KEY_DURATION, longData);
73 OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, doubleData);
74
75 vEncSample->SetParameter(format);
76 OH_AVFormat_Destroy(format);
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::DoSomethingInterestingWithMyAPI(data, size);
86 return 0;
87 }
88