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 "swdecodersetparametermpeg2_fuzzer"
22 using namespace std;
23 using namespace OHOS;
24 using namespace OHOS::Media;
25 static VDecFuzzSample *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 {
ChangeBinaryInData(const uint8_t * data,size_t size)30 bool ChangeBinaryInData(const uint8_t *data, size_t size)
31 {
32 if (size < sizeof(int64_t)) {
33 return false;
34 }
35 if (!vDecSample) {
36 vDecSample = new VDecFuzzSample();
37 vDecSample->defaultWidth = DEFAULT_WIDTH;
38 vDecSample->defaultHeight = DEFAULT_HEIGHT;
39 vDecSample->defaultFrameRate = DEFAULT_FRAME_RATE;
40 vDecSample->isSurfMode = true;
41 if (vDecSample->CreateVideoDecoder("OH.Media.Codec.Decoder.Video.MPEG2") != AV_ERR_OK) {
42 delete vDecSample;
43 vDecSample = nullptr;
44 return false;
45 }
46 if (vDecSample->ConfigureVideoDecoder() != AV_ERR_OK) {
47 delete vDecSample;
48 vDecSample = nullptr;
49 return false;
50 }
51 if (vDecSample->SetVideoDecoderCallback() != AV_ERR_OK) {
52 delete vDecSample;
53 vDecSample = nullptr;
54 return false;
55 }
56 if (vDecSample->Start() != AV_ERR_OK) {
57 delete vDecSample;
58 vDecSample = nullptr;
59 return false;
60 }
61 }
62 OH_AVFormat *format = OH_AVFormat_CreateVideoFormat("avdec_mpeg2", DEFAULT_WIDTH, DEFAULT_HEIGHT);
63 FuzzedDataProvider fdp(data, size);
64 OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, fdp.ConsumeIntegral<int32_t>());
65 OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, fdp.ConsumeIntegral<int32_t>());
66 OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, fdp.ConsumeIntegral<int32_t>());
67 OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, fdp.ConsumeIntegral<int32_t>());
68 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, fdp.ConsumeIntegral<int32_t>());
69 OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, fdp.ConsumeIntegral<int32_t>());
70 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, fdp.ConsumeIntegral<int32_t>());
71 OH_AVFormat_SetIntValue(format, OH_MD_KEY_I_FRAME_INTERVAL, fdp.ConsumeIntegral<int32_t>());
72 OH_AVFormat_SetIntValue(format, OH_MD_KEY_ROTATION, fdp.ConsumeIntegral<int32_t>());
73 OH_AVFormat_SetLongValue(format, OH_MD_KEY_DURATION, fdp.ConsumeIntegral<int64_t>());
74 OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, fdp.ConsumeFloatingPoint<double>());
75
76 vDecSample->SetParameter(format);
77
78 OH_AVFormat_Destroy(format);
79 delete vDecSample;
80 vDecSample = nullptr;
81 return true;
82 }
83 } // namespace OHOS
84
85 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)86 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
87 {
88 /* Run your code on data */
89 OHOS::ChangeBinaryInData(data, size);
90 return 0;
91 }
92