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 #define FUZZ_PROJECT_NAME "encodersetparameter_fuzzer"
22 using namespace std;
23 using namespace OHOS;
24 using namespace OHOS::Media;
25 static VEncFuzzSample *vEncSample = nullptr;
26 constexpr uint32_t DEFAULT_WIDTH = 1280;
27 constexpr uint32_t DEFAULT_HEIGHT = 720;
28 constexpr double DEFAULT_FRAME_RATE = 30.0;
29 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)30 bool DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
31 {
32 if (size < sizeof(int64_t)) {
33 return false;
34 }
35 if (!vEncSample) {
36 vEncSample = new VEncFuzzSample();
37 vEncSample->defaultWidth = DEFAULT_WIDTH;
38 vEncSample->defaultHeight = DEFAULT_HEIGHT;
39 vEncSample->defaultFrameRate = DEFAULT_FRAME_RATE;
40 vEncSample->fuzzMode = true;
41 OH_AVCapability *cap = OH_AVCodec_GetCapabilityByCategory("video/avc", true, HARDWARE);
42 string tmpCodecName = OH_AVCapability_GetName(cap);
43 vEncSample->CreateVideoEncoder(tmpCodecName.c_str());
44 vEncSample->SetVideoEncoderCallback();
45 vEncSample->ConfigureVideoEncoder();
46 vEncSample->Start();
47 }
48
49 OH_AVFormat *format = OH_AVFormat_CreateVideoFormat("video/avc", DEFAULT_WIDTH, DEFAULT_HEIGHT);
50 int32_t intData = *reinterpret_cast<const int32_t *>(data);
51 int64_t longData = *reinterpret_cast<const int64_t *>(data);
52 double doubleData = *reinterpret_cast<const double *>(data);
53 OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, intData);
54 OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, intData);
55 OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, intData);
56 OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, intData);
57 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, intData);
58 OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, intData);
59 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, intData);
60 OH_AVFormat_SetIntValue(format, OH_MD_KEY_I_FRAME_INTERVAL, intData);
61 OH_AVFormat_SetIntValue(format, OH_MD_KEY_ROTATION, intData);
62 OH_AVFormat_SetLongValue(format, OH_MD_KEY_DURATION, longData);
63 OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, doubleData);
64
65 vEncSample->SetParameter(format);
66 OH_AVFormat_Destroy(format);
67 return true;
68 }
69 } // namespace OHOS
70
71 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)72 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
73 {
74 /* Run your code on data */
75 OHOS::DoSomethingInterestingWithMyAPI(data, size);
76 return 0;
77 }
78