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 "native_avcodec_base.h"
18 #include "native_avformat.h"
19 #include "videodec_sample.h"
20 #include <fuzzer/FuzzedDataProvider.h>
21 #define FUZZ_PROJECT_NAME "hevcswdecodersetparameter_fuzzer"
22 using namespace std;
23 using namespace OHOS;
24 using namespace OHOS::Media;
25 static VDecFuzzSample *g_vDecSample = nullptr;
26 constexpr uint32_t DEFAULT_WIDTH = 1920;
27 constexpr uint32_t DEFAULT_HEIGHT = 1080;
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 (!g_vDecSample) {
36 g_vDecSample = new VDecFuzzSample();
37 g_vDecSample->fuzzData = data;
38 g_vDecSample->fuzzSize = size;
39 g_vDecSample->defaultWidth = DEFAULT_WIDTH;
40 g_vDecSample->defaultHeight = DEFAULT_HEIGHT;
41 g_vDecSample->defaultFrameRate = DEFAULT_FRAME_RATE;
42 g_vDecSample->CreateVideoDecoder("OH.Media.Codec.Decoder.Video.HEVC");
43 g_vDecSample->ConfigureVideoDecoder();
44 g_vDecSample->SetVideoDecoderCallback();
45 g_vDecSample->StartVideoDecoder();
46
47 OH_AVFormat *format = OH_AVFormat_CreateVideoFormat("video/hevc", DEFAULT_WIDTH, DEFAULT_HEIGHT);
48 FuzzedDataProvider fdp(data, size);
49 OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, fdp.ConsumeIntegral<int32_t>());
50 OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, fdp.ConsumeIntegral<int32_t>());
51 OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, fdp.ConsumeIntegral<int32_t>());
52 OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, fdp.ConsumeIntegral<int32_t>());
53 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, fdp.ConsumeIntegral<int32_t>());
54 OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, fdp.ConsumeIntegral<int32_t>());
55 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, fdp.ConsumeIntegral<int32_t>());
56 OH_AVFormat_SetIntValue(format, OH_MD_KEY_I_FRAME_INTERVAL, fdp.ConsumeIntegral<int32_t>());
57 OH_AVFormat_SetIntValue(format, OH_MD_KEY_ROTATION, fdp.ConsumeIntegral<int32_t>());
58 OH_AVFormat_SetLongValue(format, OH_MD_KEY_DURATION, fdp.ConsumeIntegral<int64_t>());
59 OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, fdp.ConsumeFloatingPoint<double>());
60
61 g_vDecSample->SetParameter(format);
62 OH_AVFormat_Destroy(format);
63
64 g_vDecSample->WaitForEOS();
65 delete g_vDecSample;
66 g_vDecSample = nullptr;
67 }
68 return true;
69 }
70 } // namespace OHOS
71
72 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)73 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
74 {
75 /* Run your code on data */
76 OHOS::DoSomethingInterestingWithMyAPI(data, size);
77 return 0;
78 }
79