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 "h263swdecodersetparameter_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
30 OH_AVFormat *format = nullptr;
31
SetFormatValue(const uint8_t * data,size_t size)32 void SetFormatValue(const uint8_t *data, size_t size)
33 {
34 format = OH_AVFormat_CreateVideoFormat("video/h263", DEFAULT_WIDTH, DEFAULT_HEIGHT);
35 FuzzedDataProvider fdp(data, size);
36 int intData0 = fdp.ConsumeIntegral<int32_t>();
37 int intData1 = fdp.ConsumeIntegral<int32_t>();
38 int intData2 = fdp.ConsumeIntegral<int32_t>();
39 int intData3 = fdp.ConsumeIntegral<int32_t>();
40 int intData4 = fdp.ConsumeIntegral<int32_t>();
41 int intData5 = fdp.ConsumeIntegral<int32_t>();
42 int intData6 = fdp.ConsumeIntegral<int32_t>();
43 int intData7 = fdp.ConsumeIntegral<int32_t>();
44 int intData8 = fdp.ConsumeIntegral<int32_t>();
45 int longData = fdp.ConsumeIntegral<int64_t>();
46 double doubleData = fdp.ConsumeFloatingPoint<double>();
47 OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, intData0);
48 OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, intData1);
49 OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, intData2);
50 OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, intData3);
51 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, intData4);
52 OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, intData5);
53 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, intData6);
54 OH_AVFormat_SetIntValue(format, OH_MD_KEY_I_FRAME_INTERVAL, intData7);
55 OH_AVFormat_SetIntValue(format, OH_MD_KEY_ROTATION, intData8);
56 OH_AVFormat_SetLongValue(format, OH_MD_KEY_DURATION, longData);
57 OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, doubleData);
58 }
59 namespace OHOS {
H263SwdecoderFuzzTest(const uint8_t * data,size_t size)60 bool H263SwdecoderFuzzTest(const uint8_t *data, size_t size)
61 {
62 if (size < sizeof(int64_t)) {
63 return false;
64 }
65 if (!vDecSample) {
66 vDecSample = new VDecFuzzSample();
67 vDecSample->defaultWidth = DEFAULT_WIDTH;
68 vDecSample->defaultHeight = DEFAULT_HEIGHT;
69 vDecSample->defaultFrameRate = DEFAULT_FRAME_RATE;
70 vDecSample->isSurfMode = true;
71 if (vDecSample->CreateVideoDecoder("OH.Media.Codec.Decoder.Video.H263") != AV_ERR_OK) {
72 delete vDecSample;
73 vDecSample = nullptr;
74 return false;
75 }
76 if (vDecSample->ConfigureVideoDecoder() != AV_ERR_OK) {
77 delete vDecSample;
78 vDecSample = nullptr;
79 return false;
80 }
81 if (vDecSample->SetVideoDecoderCallback() != AV_ERR_OK) {
82 delete vDecSample;
83 vDecSample = nullptr;
84 return false;
85 }
86 if (vDecSample->Start() != AV_ERR_OK) {
87 delete vDecSample;
88 vDecSample = nullptr;
89 return false;
90 }
91 }
92
93 SetFormatValue(data, size);
94 vDecSample->SetParameter(format);
95 OH_AVFormat_Destroy(format);
96 delete vDecSample;
97 vDecSample = nullptr;
98 return true;
99 }
100 } // namespace OHOS
101
102 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)103 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
104 {
105 /* Run your code on data */
106 OHOS::H263SwdecoderFuzzTest(data, size);
107 return 0;
108 }
109