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 {
SetRandomValue(const uint8_t * data,size_t size)31 void SetRandomValue(const uint8_t *data, size_t size)
32 {
33 OH_AVFormat *format = OH_AVFormat_CreateVideoFormat("video/avc", DEFAULT_WIDTH, DEFAULT_HEIGHT);
34 FuzzedDataProvider fdp(data, size);
35 int intData0 = fdp.ConsumeIntegral<int32_t>();
36 int intData1 = fdp.ConsumeIntegral<int32_t>();
37 int intData2 = fdp.ConsumeIntegral<int32_t>();
38 int intData3 = fdp.ConsumeIntegral<int32_t>();
39 int intData4 = fdp.ConsumeIntegral<int32_t>();
40 int intData5 = fdp.ConsumeIntegral<int32_t>();
41 int intData6 = fdp.ConsumeIntegral<int32_t>();
42 int intData7 = fdp.ConsumeIntegral<int32_t>();
43 int intData8 = fdp.ConsumeIntegral<int32_t>();
44 int longData = fdp.ConsumeIntegral<int64_t>();
45 double doubleData = fdp.ConsumeFloatingPoint<double>();
46
47 int intData9 = fdp.ConsumeIntegral<int32_t>();
48 int longData1 = fdp.ConsumeIntegral<int64_t>();
49 double doubleData1 = fdp.ConsumeFloatingPoint<double>();
50 std::string randomIntKey = fdp.ConsumeRandomLengthString();
51 std::string randomLongKey = fdp.ConsumeRandomLengthString();
52 std::string randomDoubleKey = fdp.ConsumeRandomLengthString();
53
54 OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, intData0);
55 OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, intData1);
56 OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, intData2);
57 OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, intData3);
58 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, intData4);
59 OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, intData5);
60 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, intData6);
61 OH_AVFormat_SetIntValue(format, OH_MD_KEY_I_FRAME_INTERVAL, intData7);
62 OH_AVFormat_SetIntValue(format, OH_MD_KEY_ROTATION, intData8);
63 OH_AVFormat_SetLongValue(format, OH_MD_KEY_DURATION, longData);
64 OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, doubleData);
65
66 OH_AVFormat_SetIntValue(format, randomIntKey.c_str(), intData9);
67 OH_AVFormat_SetLongValue(format, randomLongKey.c_str(), longData1);
68 OH_AVFormat_SetDoubleValue(format, randomDoubleKey.c_str(), doubleData1);
69
70 vEncSample->SetParameter(format);
71 OH_AVFormat_Destroy(format);
72 }
EncoderSetparameterFuzzTest(const uint8_t * data,size_t size)73 bool EncoderSetparameterFuzzTest(const uint8_t *data, size_t size)
74 {
75 if (size < sizeof(int64_t)) {
76 return false;
77 }
78 if (!vEncSample) {
79 vEncSample = new VEncFuzzSample();
80 vEncSample->defaultWidth = DEFAULT_WIDTH;
81 vEncSample->defaultHeight = DEFAULT_HEIGHT;
82 vEncSample->defaultFrameRate = DEFAULT_FRAME_RATE;
83 vEncSample->fuzzMode = true;
84 OH_AVCapability *cap = OH_AVCodec_GetCapabilityByCategory("video/avc", true, HARDWARE);
85 string tmpCodecName = OH_AVCapability_GetName(cap);
86 if (vEncSample->CreateVideoEncoder(tmpCodecName.c_str()) != AV_ERR_OK) {
87 delete vEncSample;
88 vEncSample = nullptr;
89 return false;
90 }
91 if (vEncSample->SetVideoEncoderCallback() != AV_ERR_OK) {
92 delete vEncSample;
93 vEncSample = nullptr;
94 return false;
95 }
96 if (vEncSample->ConfigureVideoEncoder() != AV_ERR_OK) {
97 delete vEncSample;
98 vEncSample = nullptr;
99 return false;
100 }
101 if (vEncSample->Start() != AV_ERR_OK) {
102 delete vEncSample;
103 vEncSample = nullptr;
104 return false;
105 }
106 }
107 SetRandomValue(data, size);
108 delete vEncSample;
109 vEncSample = nullptr;
110 return true;
111 }
112 } // namespace OHOS
113
114 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)115 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
116 {
117 /* Run your code on data */
118 OHOS::EncoderSetparameterFuzzTest(data, size);
119 return 0;
120 }
121