1 /*
2  * Copyright (c) 2024 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 "videodec_sample.h"
20 #define FUZZ_PROJECT_NAME "hevcswdecodersetparameter_fuzzer"
21 using namespace std;
22 using namespace OHOS;
23 using namespace OHOS::Media;
24 static VDecFuzzSample *vDecSample = nullptr;
25 constexpr uint32_t DEFAULT_WIDTH = 1920;
26 constexpr uint32_t DEFAULT_HEIGHT = 1080;
27 constexpr double DEFAULT_FRAME_RATE = 30.0;
28 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)29 bool DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
30 {
31     if (size < sizeof(int64_t)) {
32         return false;
33     }
34     if (!vDecSample) {
35         vDecSample = new VDecFuzzSample();
36         vDecSample->defaultWidth = DEFAULT_WIDTH;
37         vDecSample->defaultHeight = DEFAULT_HEIGHT;
38         vDecSample->defaultFrameRate = DEFAULT_FRAME_RATE;
39         vDecSample->CreateVideoDecoder("OH.Media.Codec.Decoder.Video.HEVC");
40         vDecSample->ConfigureVideoDecoder();
41         vDecSample->SetVideoDecoderCallback();
42         vDecSample->Start();
43     }
44     OH_AVFormat *format = OH_AVFormat_CreateVideoFormat("video/hevc", DEFAULT_WIDTH, DEFAULT_HEIGHT);
45     int32_t intData = *reinterpret_cast<const int32_t *>(data);
46     int64_t longData = *reinterpret_cast<const int64_t *>(data);
47     double doubleData = *reinterpret_cast<const double *>(data);
48     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, intData);
49     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, intData);
50     OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, intData);
51     OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, intData);
52     OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, intData);
53     OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, intData);
54     OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, intData);
55     OH_AVFormat_SetIntValue(format, OH_MD_KEY_I_FRAME_INTERVAL, intData);
56     OH_AVFormat_SetIntValue(format, OH_MD_KEY_ROTATION, intData);
57     OH_AVFormat_SetLongValue(format, OH_MD_KEY_DURATION, longData);
58     OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, doubleData);
59 
60     vDecSample->SetParameter(format);
61 
62     OH_AVFormat_Destroy(format);
63     return true;
64 }
65 } // namespace OHOS
66 
67 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)68 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
69 {
70     /* Run your code on data */
71     OHOS::DoSomethingInterestingWithMyAPI(data, size);
72     return 0;
73 }
74